如何PIVOT下一个ROW_NUMBER()

时间:2019-03-08 23:31:07

标签: sql sql-server sql-server-2016

我有一个临时表,由以下查询填充:

SELECT  att.property, att.stayDate, att.addedTimeStamp, att.rowNumber
    FROM
    (
    SELECT  RIGHT('000' + CAST(att.property AS VARCHAR(4)),4) AS property,
            CAST(att.stay_date AS DATE) AS stayDate,
            CAST(added_timestamp AS DATETIME) AS addedTimeStamp,
            ROW_NUMBER() OVER(PARTITION BY property, stay_date ORDER BY added_timestamp) AS rowNumber
        FROM dbo.tb_rm_portal_attention_days att
            WHERE att.revenue_initiative = 'Test'
    ) att

然后将以下内容带入临时表:

property   stayDate     addedTimeStamp             rowNumber
0053       2020-03-20   2019-03-04 17:10:32.837    1
0053       2020-03-20   2019-03-05 17:10:29.480    2
0053       2020-03-20   2019-03-06 17:10:25.940    3
0053       2020-03-20   2019-03-07 17:10:21.930    4
0100       2020-03-25   2019-03-04 17:10:32.837    1
0100       2020-03-25   2019-03-05 17:10:29.480    2
0100       2020-03-25   2019-03-06 17:10:25.940    3
0100       2020-03-25   2019-03-07 17:10:21.930    4

在这里,我想让propertystayDateaddedTimeStamp,然后是组中的下一个addedTimeStamp,如果这是最大返回NULL或其他...不确定是否有意义...

我的最终目标是实质上从临时表中获取以下内容:

property   stayDate      firstTimeStamp            secondTimeStamp
0053       2020-03-20    2019-03-04 17:10:32.837   2019-03-05 17:10:29.480
0053       2020-03-20    2019-03-05 17:10:29.480   2019-03-06 17:10:25.940
0053       2020-03-20    2019-03-06 17:10:25.940   2019-03-07 17:10:21.930
0053       2020-03-20    2019-03-06 17:10:25.940   NULL
0100       2020-03-25    2019-03-04 17:10:32.837   2019-03-05 17:10:29.480
0100       2020-03-25    2019-03-05 17:10:29.480   2019-03-06 17:10:25.940
0100       2020-03-25    2019-03-06 17:10:25.940   2019-03-07 17:10:21.930
0100       2020-03-25    2019-03-06 17:10:25.940   NULL

2 个答案:

答案 0 :(得分:1)

这是使用LEAD窗口功能的完美示例

LEAD(firstTimeStamp) OVER 
    (PARTITION BY property, stay_date ORDER BY added_timestamp) AS secondTimeStamp

您可以将LEAD放在主查询中,如果不需要其他地方也可以删除ROW_NUMBER

SELECT  att.property, att.stayDate, att.addedTimeStamp, att.rowNumber, att.secondTimeStamp
    FROM
    (
    SELECT  RIGHT('000' + CAST(att.property AS VARCHAR(4)),4) AS property,
            CAST(att.stay_date AS DATE) AS stayDate,
            CAST(added_timestamp AS DATETIME) AS addedTimeStamp,
            ROW_NUMBER() OVER
                (PARTITION BY property, stay_date ORDER BY added_timestamp) AS rowNumber,

            LEAD(added_timestamp) OVER 
                (PARTITION BY property, stay_date ORDER BY added_timestamp) AS secondTimeStamp

        FROM dbo.tb_rm_portal_attention_days att
            WHERE att.revenue_initiative = 'Test'
    ) att

答案 1 :(得分:1)

您需要lead(),但不需要子查询:

SELECT RIGHT('000' + CAST(att.property AS VARCHAR(4)), 4) AS property,
       CAST(att.stay_date AS DATE) AS stayDate,
       CAST(added_timestamp AS DATETIME) AS firstTimeStamp,
       LEAD(added_timestamp) OVER (PARTITION BY property, stay_date ORDER BY added_timestamp) AS secondTimeStamp
FROM dbo.tb_rm_portal_attention_days att
WHERE att.revenue_initiative = 'Test'