我有一个有两行的表,一个有一个开头,一个有一个具有相同id的结束时间。
例如
ID | Time
12345 | 12-12-18 12:00
12345 | 12-12-18 12:12
54321 | 12-12-18 11:30
54321 | 12-12-18 11:35
我如何获得输出
ID | Time
12345 | 12
54321 | 5
猜测滞后还是结束?
答案 0 :(得分:13)
您似乎想要聚合:
select id,
datediff(minute, min(time), max(time)) as diff_minutes
from t
group by id;
答案 1 :(得分:1)
你也可以自我加入,因为Ezlo也写道:
select a.ID, datediff(minute,MIN(a.time1),max(x.time1) ) as maxtime
from #test a inner join #test x on A.id = x.id group by a.id
但似乎戈登更简单:)