表A id b c
1 2 2010/01/02
5 3 2010/01/02
...表B
id a d
5 5 2010/03/02
1 9 2010/05/02
...求2010年1月份表A与表B相差时间的平均值
1 2 2010/01/02
5 3 2010/01/02
...表B
id a d
5 5 2010/03/02
1 9 2010/05/02
...求2010年1月份表A与表B相差时间的平均值
create table 表A
(
id int ,
b int ,
c datetime
)
create table 表B
(
id int,
a int,
d datetime
)insert into 表A values(1,2,'2010-01-02')
insert into 表A values(5,3,'2010-01-02')
insert into 表B values(5,5,'2010-03-02')
insert into 表B values(1,9,'2010-05-02')select avg(datediff(d,a.c,b.d))
from 表A as a join 表B as b on a.id = b.id
结果无列名
89
if object_id('表A') >0
drop table 表A
create table 表A
(
id int ,
b int ,
c datetime
)if object_id('表B')>0
drop table 表B
create table 表B
(
id int,
a int,
d datetime
)insert into 表A values(1,2,'2010-01-02')
insert into 表A values(5,3,'2010-01-02')
insert into 表B values(5,5,'2010-03-02')
insert into 表B values(1,9,'2010-05-02')select avg(datediff(d,a.c,b.d)) as 相差天数平均值
from 表A as a join 表B as b on a.id = b.iddrop table 表A
drop table 表B