【问题标题】:Create group within one UNIQUE VALUE in teradata在 teradata 的一个 UNIQUE VALUE 内创建组
【发布时间】:2017-05-25 17:48:58
【问题描述】:

SESSION_ID---STATUS---Desired---

1C1A1111111---开始---Trip1---

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

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

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

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

1C1A1111111---停止---行程1---

1C1A1111111---?-----

1C1A1111111---?-----

1C1A1111111---?-----

1C1A1111111---?-----

1C1A1111111---开始---Trip2---

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

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

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

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

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

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

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

1C1A1111111---停止---Trip2---

【问题讨论】:

  • 数据有两列,我需要根据开始和停止创建 TRIP1 和 TRIP2,因此开始和停止之间的行是一趟
  • 我假设你有一个时间戳列?
  • 是的,我确实有时间戳列

标签: teradata


【解决方案1】:
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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-06
    • 2021-08-07
    • 1970-01-01
    • 2021-04-14
    • 2019-10-26
    • 2012-03-05
    相关资源
    最近更新 更多