I have a table T:
Entity type starttime sequence duration
1 A 2017010101 1 12
1 A 2017010102 2 11
1 A 2017010103 3 3
1 A 2017010104 4 1
1 A 2017010105 1 19
1 A 2017010106 2 18
2 A 2017010101 1 18
2 A 2017010102 1 100
3 A 2017010101 1 120
I need to aggregate the data so that each run of sequence has a total duration and the first starttime:
Entity type starttime sequence duration
1 A 2017010101 1 27
1 A 2017010105 1 37
2 A 2017010101 1 18
2 A 2017010102 1 100
3 A 2017010101 1 120
I believe this is a gaps-and-islands problem, but I can't quite figure it out...
I have tried to use a lead() over (partition by entity order by sequence) but this keeps grabbing the next run of sequence.