在teradata中的一个UNIQUE VALUE中创建组

时间:2017-05-24 21:30:33

标签: teradata

SESSION_ID --- --- STATUS期望---

1C1A1111111 --- ---启动--- TRIP1

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

1 个答案:

答案 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