【问题标题】:SQL lead function SQL NetezzaSQL 引导函数 S​​QL Netezza
【发布时间】:2020-12-28 00:13:10
【问题描述】:

我有一张表,其中只有一个 line_id 的以下示例。

LINE_ID|COLLECTION_DATE    |DSL_CARD_TYPE|
-------|-------------------|-------------|
1234567|2020-03-25 08:46:08|ADSL_PORT    |
1234567|2020-03-26 08:31:48|ADSL_PORT    |
1234567|2020-03-27 08:42:40|VDSL_PORT    |
1234567|2020-03-28 08:36:32|VDSL_PORT    |
1234567|2020-03-29 08:31:33|VDSL_PORT    |
1234567|2020-03-30 08:50:15|VDSL_PORT    |
1234567|2020-04-31 08:44:33|ADSL_PORT    |
1234567|2020-03-01 08:34:53|ADSL_PORT    |
1234567|2020-04-02 08:44:11|ADSL_PORT    |
1234567|2020-04-03 08:43:51|VDSL_PORT    |
1234567|2020-04-04 08:54:33|ADSL_PORT    |
1234567|2020-04-05 09:06:47|ADSL_PORT    |
1234567|2020-04-06 09:06:57|VDSL_PORT    |
1234567|2020-04-07 09:13:32|VDSL_PORT    |

我需要对 DSL_CARD_TYPE 进行分组并创建一个名为 Next_COLLECTION_DATE 的新列,以获取下一个 DSL_CARD_TYPE 并返回每个 Next_group 中的第一个日期,但如下所示

LINE_ID|COLLECTION_DATE    |Next_COLLECTION_DATE  |DSL_CARD_TYPE|
-------|-------------------|----------------------|-------------|
1234567|2020-03-25 08:46:08|2020-03-27 08:42:40   |ADSL_PORT    |  
1234567|2020-03-27 08:42:40|2020-03-31 08:34:53   |VDSL_PORT    |
1234567|2020-03-31 08:34:53|2020-04-03 08:43:51   |ADSL_PORT    |
1234567|2020-04-03 08:43:51|2020-04-04 08:54:33   |VDSL_PORT    |   
1234567|2020-04-04 08:54:33|2020-04-06 09:06:57   |ADSL_PORT    |  
1234567|2020-04-06 09:06:57|2020-04-07 09:13:32   |VDSL_PORT    | 

我有下面的代码,它返回每个组中的最后一个日期

select line_id, dsl_card_type, min(collection_date), max(collection_date)
from (select v.*,
             row_number() over (partition by line_id order by collection_date) as seqnum,
             row_number() over (partition by line_id, dsl_card_type order by collection_date) as seqnum_2
      from ANALYTICS.tmp.V_PORTS_LINE_CARD_DATA_ALL v
      where collection_date >= '2020-07-27 00:00:00'
     ) v
group by line_id, dsl_card_type, (seqnum - seqnum_2);```

【问题讨论】:

    标签: sql datetime window-functions netezza gaps-and-islands


    【解决方案1】:

    您可以使用lead()lag() 来做到这一点:

    select line_id, dsl_card_type, collection_date, 
           lead(collection_date) over (partition by line_id order by collection_date)
    from (select v.*,
                 lag(dsl_card_type) over (partition by line_id order by collection_date) as prev_dsl_card_type
          from ANALYTICS.tmp.V_PORTS_LINE_CARD_DATA_ALL v
          where collection_date >= '2020-07-27 00:00:00'
         ) v
    where prev_dsl_card_type is null or prev_dsl_card_type <> dsl_card_type;
    

    瞧!不需要聚合。

    【讨论】:

    • 非常感谢,@Gordon,它工作得很好,我学到了一种新技术来应对这种复杂的情况。
    【解决方案2】:

    您可以在外部查询中使用lead()

    select 
        line_id, 
        dsl_card_type, 
        min(collection_date) as first_collection_date, 
        lead(min(collection_date)) over(partition by line_id order by min(collection_date)) next_collection_date
    from (select v.*,
                 row_number() over (partition by line_id order by collection_date) as seqnum,
                 row_number() over (partition by line_id, dsl_card_type order by collection_date) as seqnum_2
          from ANALYTICS.tmp.V_PORTS_LINE_CARD_DATA_ALL v
          where collection_date >= '2020-07-27 00:00:00'
         ) v
    group by line_id, dsl_card_type, (seqnum - seqnum_2)
    

    【讨论】:

      猜你喜欢
      • 2016-11-17
      • 2020-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-11
      • 2016-04-07
      相关资源
      最近更新 更多