Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MsgID | DateTime | State | TimeDiff
- ---------------------------------------------
- 387 | 2014-09-06 21:06:27 | 2 | 43
- 387 | 2014-09-06 21:06:28 | 3 | 44
- 212 | 2014-09-06 22:16:27 | 1 | 0
- 212 | 2014-09-06 22:16:38 | 3 | 11
- 532 | 2014-09-06 23:26:27 | 1 | 0
- 532 | 2014-09-06 23:27:27 | 3 | 60
- 532 | 2014-09-06 23:27:28 | 2 | 61
- 215 | 2014-09-06 23:46:27 | 1 | 0
- 212 | 2014-09-07 22:16:27 | 1 | 0
- 212 | 2014-09-07 22:16:37 | 2 | 10
- 212 | 2014-09-07 22:16:38 | 3 | 11
- MsgID | State 1 | State 2 | State 3 |
- -------------------------------------------------------------------------
- 212 | 2014-09-06 22:16:27 | null | 2014-09-06 22:16:38 |
- 532 | 2014-09-06 23:26:27 | 2014-09-06 23:27:28 | 2014-09-06 23:27:27 |
- 215 | 2014-09-06 23:46:27 | null | null |
- 212 | 2014-09-07 22:16:27 | 2014-09-07 22:16:37 | 2014-09-07 22:16:38 |
- select
- MsgID,
- State1= [1],
- State2= [2],
- State3=[3]
- from
- (
- select
- t1.MsgID,
- t1.DateTime,
- T1.State,
- r=row_number() over ( order by t1.[DateTime])- row_number() over( partition by t1.MsgID order by t1.[Datetime])
- from t t1
- join
- (
- select MsgId
- from t
- group by MsgID
- having min(State)=1
- ) t2
- on t1.MsgId=t2.MsgId
- ) src
- PIVOT
- (
- max(DateTime) for State in ([1],[2],[3])
- )p
Add Comment
Please, Sign In to add comment