簡體   English   中英

獲取交易的滾動 Last_Date SQL Teradata

[英]Get the rolling Last_Date for a transactions SQL Teradata

請我有一個包含兩列的簡單表格,如下所示

|Connect_Date|TRX_Count|
|------------+---------|
|1-May       |         |
|2-May       |         |
|3-May       |        3|
|4-May       |         |
|5-May       |         |
|6-May       |        4|
|7-May       |         |
|8-May       |        7|
|9-May       |         |
|10-May      |       10|

我需要插入一個新列,其最后一個日期為 TRX_Count 大於 0 或 null,結果將如下所示

|Connect_Date|TRX_Count|Last_Date|
|------------+---------+---------|
|1-May       |         |3-May    |
|2-May       |         |3-May    |
|3-May       |        3|3-May    |
|4-May       |         |6-May    |
|5-May       |         |6-May    |
|6-May       |        4|6-May    |
|7-May       |         |8-May    |
|8-May       |        7|8-May    |
|9-May       |         |10-May   |
|10-May      |       10|10-May   |

使用累積最小值:

select t.*,
       min(case when trx_count is not null then connect_date end) over (order by connect_date rows between current row and unbounded following) as last_date
from t;

lead(ignore nulls)

select t.*
       lead(case when trx_count is not null then connect_date end ignore nulls) over (order by connect_date)
from t;

這會在開頭和結尾填充 NULL:

coalesce(min(case when TRX_Count is not null then Connect_Date end)
         over (order by Connect_Date desc
               rows unbounded preceding)
        ,max(case when TRX_Count is not null then Connect_Date end) over ()
        )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM