I have an SQL Server table of the following structure:
id TransDate PartType
======================================
1 2016-06-29 10:23:00 A1
2 2016-06-29 10:30:00 A1
3 2016-06-29 10:32:00 A2
4 2016-06-29 10:33:00 A2
5 2016-06-29 10:35:00 A2
6 2016-06-29 10:39:00 A3
7 2016-06-29 10:41:00 A4
I need a SELECT
statement that will output a table that finds the changes in PartType
, and that looks like this (for SSRS purposes):
PartType StartTime EndTime
=======================================================
A1 2016-06-29 10:23:00 2016-06-29 10:32:00
A2 2016-06-29 10:32:00 2016-06-29 10:39:00
A3 2016-06-29 10:39:00 2016-06-29 10:41:00
A4 2016-06-29 10:41:00 NULL
Note that the StartTime
always picks up from the last EndTime
, unless it's the first record in the table.
What should my SELECT
statement be? I can't seem to get the intended results.
EDIT: I'm using SQL Server 2008 R2; I should've specified that.