I have this table:.
Name StartTime EndTime Incidence
--------------------------------------------------------
Peter 6:00 8:00 1
Steven 6:05 7:50 3
Peter 8:00 15:00 2
Steven 7:50 16:00 1
I need to get a summary with the total time worked and the incidence related to the last record (Peter 2, Steven 1). I was trying to get the incidence with the over () clause, partitioning by name and ordering by the StartTime, but it doesn't work.
How do I get the last incidence in the most elegant and fastest way?
IF OBJECT_ID('tempdb..#Temp') IS NOT NULL
DROP TABLE #Temp
CREATE TABLE #Temp (Name NVARCHAR(10), StartTime Time, EndTime Time, Incidence int)
INSERT INTO #Temp (Name,StartTime, EndTime, Incidence) values ('Peter','06:00','08:00',1)
INSERT INTO #Temp (Name,StartTime, EndTime, Incidence) values ('Steven','06:05','07:50',3)
INSERT INTO #Temp (Name,StartTime, EndTime, Incidence) values ('Peter','08:00','15:00',2)
INSERT INTO #Temp (Name,StartTime, EndTime, Incidence) values ('Steven','07:50','16:00',1)
select Name, Sum(DATEDIFF(minute, StartTime, EndTime)) as Minutes, last-incidence-of-day???
from #Temp
Group by Name