【问题标题】:Lag Lead to calculate the beginning and ending number of subscriptions in a periodLag Lead 计算一个期间的开始和结束订阅数
【发布时间】:2018-04-25 05:49:20
【问题描述】:

我正在尝试构建如下所示的输出表。有人可以帮助我如何使用滞后时间或其他一些窗口函数来解决它吗?

Start= End from previous month
Net= new- churn
End = Start + Net

INPUT 

month   year    new churn   
2      2012     114     0   
3      2012     143     20  
4      2012     221     43  
5      2012     197     74  
6      2012     234     122 
7      2012     276     138 
8      2012     278     200 


OUTPUT


month   year    Start   new churn   Net End
2      2012     0       114     0   114 114
3      2012     114     143     20  123 237
4      2012     237     221     43  181 418
5      2012     418     197     74  125 543
6      2012     543     234     122 116 659
7      2012     659     276     138 150 809
8      2012     809     278     200 90  899

【问题讨论】:

  • net 的值是 New-churn 对吧?
  • 221 - 43 = 178,而不是181。我认为你的计算可能有误。

标签: sql lag window-functions lead


【解决方案1】:

不确定开始是如何计算的。您说开始将是上个月底,但没有提到它需要是哪一列。但我认为您需要以下一些东西。

select [month],[YEAR],LAG (new,1) OVER (ORDER BY [Month]) AS Start,
    new, churn,new-chrun as Net, start+net as end
From TableA

【讨论】:

  • 开始 = 上个月结束。对于第一个月,Start =0,因为第一行没有滞后
【解决方案2】:

试试这个:

DECLARE @tab1 TABLE(month INT, year INT,new INT, churn INT)

INSERT INTO @tab1 VALUES
(2,2012,114,0  )
,(3,2012,143,20 )
,(4,2012,221,43 )
,(5,2012,197,74 )
,(6,2012,234,122)
,(7,2012,276,138)
,(8,2012,278,200)

SELECT [T1].[month],[T1].[Year]
    ,ISNULL(SUM([T2].new-[T2].churn) OVER(ORDER BY [T1].[Year], [T1].[month]),0)Start
    ,[T1].new, [T1].churn, [T1].new-[T1].churn AS net
    ,([T1].new-[T1].churn)+ISNULL(SUM([T2].new-[T2].churn) OVER(ORDER BY [T1].[Year], [T1].[month]),0) [End]
From @tab1 T1
LEFT JOIN @tab1 T2 ON T1.month = t2.month+1

输出:

month   Year    Start   new churn   net End
2       2012    0       114 0       114 114
3       2012    114     143 20      123 237
4       2012    237     221 43      178 415
5       2012    415     197 74      123 538
6       2012    538     234 122     112 650
7       2012    650     276 138     138 788
8       2012    788     278 200     78  866

【讨论】:

    【解决方案3】:

    你可以试试这个查询。

    ;WITH CTE AS(
        SELECT *,(new- churn) as 'Net' 
        from T1
    )
    SELECT t1.[month],
           t1.[Year],
           coalesce(lag([End]) OVER(order by [year],[month]),0) 'Start',
           t1.new,
           t1.churn,
           t1.Net,
           t1.[End]
    FROM (
        SELECT t1.*,coalesce(SUM(t2.Net) over(order by t2.[year],t2.[month]),0) +t1.net as 'End'
        From  CTE t1
        LEFT JOIN CTE t2 ON T1.[month] = t2.[month]+1
    ) t1
    

    sqlfiddle:http://sqlfiddle.com/#!18/6a544/1

    【讨论】:

      【解决方案4】:

      我想你想要:

      select i.*,
             sum(new) over (order by year, month) as start,
             (new - churn) as net,
             sum(new - churn) over (order by year, month) as end
      from input i;
      

      Here 是一个 SQL Fiddle。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多