【问题标题】:How would I generate a unique ID to a set of rows based on a timestamp column?如何根据时间戳列为一组行生成唯一 ID?
【发布时间】:2021-08-12 23:33:50
【问题描述】:

我有下表:

Prospect_ID Channel Timestamp
23455 Digital 01/01/2020 10:00:00
23455 Digital 01/01/2020 10:14:10
23455 Call 01/01/2020 10:30:10
55869 Digital 02/01/2020 09:00:01
55869 Digital 02/01/2020 14:00:00
55869 Call 02/01/2020 14:10:00

我想生成一个填充有唯一 ID 的列,该 ID 将每个潜在客户 ID 的时间戳在 30 分钟内分组在一起。

输出应如下所示:

Prospect_ID Channel Timestamp Timestamp_id
23455 Digital 01/01/2020 10:00:00 abc1
23455 Digital 01/01/2020 10:14:10 abc1
23455 Call 01/01/2020 10:30:10 abc1
55869 Digital 01/01/2020 10:31:01 abc2
55869 Digital 02/01/2020 14:00:00 abc3
55869 Call 02/01/2020 14:10:00 abc3

我为此使用 Snowflake DB。

提前致谢!

【问题讨论】:

    标签: sql snowflake-cloud-data-platform


    【解决方案1】:

    您可以使用lag() 和累积和:

    select t.*,
           sum(case when prev_timestamp >= timestamp - interval '30 minute' then 0 else 1 end) over (partition by prospectid order by timestamp) as timestamp_id
    from (select t.*,
                 lag(timestamp) over (partition by prospectid order by timestamp) as prev_timestamp
          from t
         ) t;
    

    如果您希望所有用户都有一个唯一编号,您可以调整逻辑:

    select t.*,
           sum(case when prev_timestamp >= timestamp - interval '30 minute' then 0 else 1 end) over (order by prospectid, timestamp) as timestamp_id
    

    【讨论】:

    • 感谢 Gordon - 我将如何生成唯一的时间戳 ID?
    • 抱歉好像没用。它在 timestamp_id 列中将 every 设置为 1。
    • 我想知道我是否再做一个查询来将潜在客户 id 与时间戳 id 连接起来?
    • 谢谢 - 我稍微调整了一下,它现在可以工作了 :)
    【解决方案2】:

    您可以将分析函数 lag() 和 sum() 与 UDF 一起使用。

    CREATE OR REPLACE FUNCTION timestamp_UDF() 
    RETURNS table(PROSPECT_ID INTEGER, CHANNEL VARCHAR2(10),
    MY_TIMESTAMP timestamp, timestamp_id VARCHAR2(10))
    AS  
    $$
      select t.prospect_id,t.channel, t.my_timestamp,
      'abc '||sum(case when prev_timestamp >= my_timestamp - interval '30 minute' then 0 else 1 end) over (order by my_timestamp) as timestamp_id
       from (select t.*,
       lag(my_timestamp) over (order by my_timestamp) as prev_timestamp
        from table_name as t
       ) t
    $$
    ;
    
    select * from table(timestamp_UDF());

    【讨论】:

    猜你喜欢
    • 2017-01-23
    • 2020-06-09
    • 2014-04-17
    • 1970-01-01
    • 2013-12-11
    • 2019-03-08
    • 1970-01-01
    • 2016-04-21
    • 1970-01-01
    相关资源
    最近更新 更多