22,209
社区成员
发帖
与我相关
我的任务
分享
select a,max(b)-min(b)
from table1
group by a
---测试数据---
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([a] int,[b] datetime)
insert [tb]
select 1,'2009/6/1' union all
select 1,'2009/6/3' union all
select 1,'2009/6/5' union all
select 2,'2009/6/2' union all
select 2,'2009/6/2'
---查询---
select
a,
datediff(dd,min(b),max(b)) as [时间差(天)]
from
tb
group by
a
---结果---
a 时间差(天)
----------- -----------
1 4
2 0
(所影响的行数为 2 行)
select
a,
datediff(dd,min(b),max(b))
from
tb
group by
a
select 列a,datediff(day,min(列b),max(列b)) as [最大天差]
from tb
group by 列a