我有以下格式的数据表
Id LocationId UserName Startdate
1 10 xz 2017-02-21 09:05:20
2 10 xz 2017-02-21 09:15:20
3 10 xz 2017-02-21 09:25:20
4 10 xz 2017-02-21 09:35:20
5 11 xy 2017-02-21 09:45:20
6 11 xy 2017-02-21 09:55:20
7 11 xy 2017-02-21 10:05:20
8 11 xy 2017-02-21 10:15:20
9 10 xz 2017-02-21 10:15:20
10 10 xz 2017-02-21 10:25:20
我想计算开始时间结束时间和时间差异,
并将节目记录为
Id locationId startdate starttime endtime timespan
1 10 2017-02-21 09:05 09:35:20 0hr 30min0sec
2 11 2017-02-21 09:05 09:35:20 0hr 30min0sec
3 10 2017-02-21 10:15 10:25 0hr 10min 0sec
答案 0 :(得分:-1)
如果您的意思是SQL,我希望以下内容可以帮助您:
WITH TEST AS(
SELECT *,
CASE WHEN LAG(LocationId,1) OVER(ORDER BY ID) != LocationId
OR LAG(LocationId,1) OVER(ORDER BY ID) IS NULL
THEN ID
ELSE 0 END FLAG
FROM [dbo].[DATATABLE]),`
`GroupData AS(
SELECT [LocationId],[UserName],[startdate],MAX(MI.FLAG) FLAG FROM TEST JOIN
(SELECT FLAG FROM TEST
WHERE FLAG != 0) MI ON TEST.ID >= MI.FLAG
GROUP BY [ID],[LocationId],[UserName],[startdate],TEST.FLAG)`
`SELECT LocationID,MIN([startdate]),MAX([startdate]) FROM GroupData
GROUP BY FLAG,LocationID