SQL lead function SQL Netezza

Ahmed Mohammed Abdel Kader

I have a table which has the below sample for only one 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    |

What I need is to group DSL_CARD_TYPE and create a new column called Next_COLLECTION_DATE to get the next DSL_CARD_TYPE and return the first date in each Next_group but like below

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    | 

I have the below code and it returns the last date in each group

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);```
Gordon Linoff

You can do this using lead() and 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;

Voila! No aggregation is needed.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

SQLラグとLEADクエリ

分類Dev

SQL SERVER COUNT LEAD 条件 グループ BY

分類Dev

Netezza SQLは、昨年のデータのみを表示します

分類Dev

Netezza SQLは、昨年のデータのみを表示します

分類Dev

値の言語の決定:アラビア語英語SQL netezza

分類Dev

SQLでの日付範囲の折りたたみ(Netezza)

分類Dev

How do I get the aggregate of last n values in SQL using lead/lag

分類Dev

SQL構文でのLAGおよびLEADの代替

分類Dev

LEAD / LAG SQL Server 2012 /ギャップと島

分類Dev

SQL Query for a search function

分類Dev

CEILING Function SQL server

分類Dev

Custom function for linq to sql

分類Dev

SQL update function

分類Dev

Replace function in Oracle SQL

分類Dev

SQL Function with variable parameter

分類Dev

substr SQl function

分類Dev

Dynamic Pivot Function - SQL

分類Dev

Netezza / SQL ..ランダムな文字を使用した一時テーブルの作成

分類Dev

SQL-LEADを使用して特定の条件の行をスキップする

分類Dev

SQL Serverの各行の違いを1列取得する(LEAD関数の代替)

分類Dev

Oracle SQL - using the coalesce function

分類Dev

Quantiles function in BigQuery Standard SQL

分類Dev

ansi SQL date function in Oracle

分類Dev

Create a recursive function in SQL Server

分類Dev

Having trouble with Group By function in SQL

分類Dev

Problems with SQL sum aggregate function

分類Dev

SQL window function with date filter

分類Dev

Writing sql without aggregation function

分類Dev

SQL order by a function with literal values