I have the following table. The startdatetime column is repeated when the duration is over a day. What I want is to write a query which skips the first row and count the difference of the second row whenever a duplicate cell in a startdatetime column come. E.g. In the case of following sample table the output I want is given in the table.
CREATE TABLE TEST
([Name] varchar(50), [StartDateTime1] datetime, [EndDateTime2] datetime, Diffy int)
;
INSERT INTO Test
([Name], [StartDateTime], [EndDateTime2], [Diffy])
VALUES
('ABC', '2015-07-21 16:08:02.000', '2015-07-21 16:18:10.000', '608' ),
('ABC', '2015-07-21 16:18:10.000', '2015-07-21 23:06:46.000', '24516' ),
('ABC', '2015-07-21 16:18:10.000', '2015-07-23 12:37:35.000', '159565' ),
('ABC', '2015-07-23 17:33:35.000', '2015-07-24 11:07:00.000', '63205' )
;
╔══════╦════════╗
║ Name ║ Diffy ║
╠══════╬════════╣
║ ABC ║ 608 ║
║ ABC ║ 159565 ║
║ ABC ║ 63205 ║
╚══════╩════════╝
The simple query I used is
Select
Name,
DateDiff(SECOND, StartDateTime, EndDateTime) As Diffy
From
Test