22,209
社区成员
发帖
与我相关
我的任务
分享
date id start end num
2018-02-12 6 08:30 09:30 1
2018-02-26 6 08:30 09:30 1
2018-03-02 6 09:00 11:00 2
2018-03-02 6 13:30 15:30 2
2018-03-12 6 08:30 09:30 1
2018-11-09 3 09:00 10:00 1
2018-11-09 3 11:00 12:00 1
2018-11-09 3 14:30 16:00 1.5
date id start end num
2018-02-12 6 08:30 09:30 1
2018-02-26 6 08:30 09:30 1
2018-03-02 6 09:00 15:30 4
2018-03-12 6 08:30 09:30 1
2018-11-09 3 09:00 16:00 3.5
--测试数据
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([date] Date,[id] int,[start] TIME,[end] TIME,[num] decimal(18,7))
Insert #T
select '2018-02-12',6,'08:30','09:30',1 union all
select '2018-02-26',6,'08:30','09:30',1 union all
select '2018-03-02',6,'09:00','11:00',2 union all
select '2018-03-02',6,'13:30','15:30',2 union all
select '2018-03-12',6,'08:30','09:30',1 union all
select '2018-11-09',3,'09:00','10:00',1 union all
select '2018-11-09',3,'11:00','12:00',1 union all
select '2018-11-09',3,'14:30','16:00',1.5
Go
--测试数据结束
SELECT [date],
id,
MIN([start]) AS [start],
MAX([end]) AS [end],
SUM([num]) AS [num]
FROM #T
GROUP BY [date],
id
ORDER BY date