2017-05-24 53 views
0

一個獨特的價值中創建組SESSION_ID --- ---狀態所需---

1C1A1111111 ---開始--- TRIP1 ---在Teradata的

1C1A1111111 ---? - -Trip1 ---

1C1A1111111 ---?--- TRIP1 ---

1C1A1111111 ---?--- TRIP1 ---

1C1A1111111 ---?--- TRIP1 ---

1C1A1111111 ---停止--- TRIP1 ---

1C1A1111111 ---?------

1C1A1111111 ---?------

1C1A1111111 ---?------

1C1A1111111 ---?------

1C1A1111111 ---開始--- TRIP2 ---

1C1A1111111 ---? --- Trip2- - ???

1C1A1111111 --- --- --- TRIP2

1C1A1111111 --- --- --- TRIP2

1C1A1111111 --- --- --- TRIP2

1C1A1111111 ---?--- TRIP2 ---

1C1A1111111 ---?--- TRIP2 ---

1C1A1111111 ---?--- TRIP2 ---

1C1A1111111 ---停止--- TRIP2 ---

+0

數據有兩列,我需要根據開始創建TRIP1&TRIP2和停止,從而啓動和停止之間的行一趟 –

+0

我假設你有一個時間戳列? –

+0

是的,我有時間戳列 –

回答

0
CASE -- check if current row is between start and stop 
    WHEN Last_Value(status IGNORE NULLS) 
     Over (PARTITION BY session_id 
       ORDER BY ts 
       ROWS Unbounded Preceding) = 'start' 
    OR status = 'stop' 

     -- increase trip number whenever status = 'start' 
    THEN Sum(CASE WHEN status = 'start' THEN 1 END) 
     Over (PARTITION BY session_id 
       ORDER BY ts 
       ROWS Unbounded Preceding) 
END 
+0

謝謝,這是偉大的,解決了我的問題 –