I have a table as below
ID | DateTime | Summary |
---|---|---|
1 | 2022-06-21 19:03:30.783 | XSR |
1 | 2022-06-21 19:04:40.763 | GKE |
1 | 2022-06-21 19:05:35.483 | ERE |
2 | 2022-07-20 11:01:20.783 | BMR |
2 | 2022-07-20 12:03:39.142 | PER |
It should produce an output as below. ie start datetime and end datetime of every ID. Also output the end time's Summary value.
Expected Result :
ID | Start DateTime | End DateTime | Summary |
---|---|---|---|
1 | 2022-06-21 19:03:30.783 | 2022-06-21 19:05:35.483 | ERE |
2 | 2022-07-20 11:01:20.783 | 2022-07-20 12:03:39.142 | PER |
Code I tried
select MIN (t1.DateTime) as StartTime, MAX (t1.DateTime) as EndTime, datediff(MINUTE, min(t1.DateTime), max(t1.DateTime)) as 'RunTime (Mins)'
,max (t1.Summary)
from table t1
Try this:
SELECT
t1.id,
MIN(t1.[datetime]) AS [Starttime],
MAX(t1.[datetime]) AS [Endtime],
MAX(t2.summary) AS [Summary]
FROM Table t1, Table t2
WHERE t1.id = t2.id
AND t2.[datetime] = (SELECT
MAX([datetime])
FROM Table t3
WHERE t2.id = t3.id)
GROUP BY t1.id
;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.