SQL Server: Lead/Lag analytic function across groups (and not within groups)

ぃ、小莉子 提交于 2019-12-05 03:53:51

Try this query:

select cd.Dt
    , t.Dt_next
    , cd.customer_id
    , cd.buy_time
    , cd.money_spent
from (
    select Dt
        , LEAD(Dt) OVER (PARTITION BY customer_id ORDER BY Dt) AS Dt_next
        , customer_id
    from (
        select distinct Dt, customer_id
        from #customer_data
    ) t
) t
inner join #customer_data cd on t.customer_id = cd.customer_id and t.Dt = cd.Dt

Why field money_spent has float type? You may have problems with calculations. Convert it to decimal type.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!