滚动平均值(T-SQL) - 基于白天的销售额

时间:2017-01-24 16:44:22

标签: sql tsql sql-server-2012 moving-average windowed

我有这个问题:

Select 
    ISNULL(CAST((H.ORDDAT_0) as VARCHAR(11)),'[Totals]') as 'OrderDate',
    SUM(CAST(((P.NETPRINOT_0 * Q.QTY_0 * H.CHGRAT_0)) as MONEY)) as 'TotalSalesEntered',
    SUM(CASE 
           WHEN MONTH(H.SHIDAT_0) = MONTH(GETDATE()) 
              THEN (CAST(((P.NETPRINOT_0 * Q.QTY_0 * H.CHGRAT_0)) AS MONEY))  
              ELSE 0 
        END) AS 'SalesToShipThisMonth' ,
    SUM(CASE WHEN  MONTH(H.SHIDAT_0) = MONTH(GETDATE()) THEN (CAST(((P.NETPRINOT_0 * Q.QTY_0 * H.CHGRAT_0))  as MONEY)) else 0 end) / 
      nullif(SUM(CAST(((P.NETPRINOT_0 * Q.QTY_0 * H.CHGRAT_0)) as MONEY)),0) AS '%SalestoShipThisMonth',
    AVG(SUM(CASE WHEN  MONTH(H.SHIDAT_0) = MONTH(GETDATE()) THEN (CAST(((P.NETPRINOT_0 * Q.QTY_0 * H.CHGRAT_0))  as MONEY)) else 0 end) / 
      nullif(SUM(CAST(((P.NETPRINOT_0 * Q.QTY_0 * H.CHGRAT_0)) as MONEY)),0)) over (order by ISNULL(CAST((H.ORDDAT_0) as VARCHAR(11)),'[Totals]') ) as 'RollingAvgSalesToShip'
FROM 
    x3v6.CICPROD.SORDER H
LEFT OUTER JOIN 
    x3v6.CICPROD.SORDERQ Q ON H.SOHNUM_0 = Q.SOHNUM_0
LEFT OUTER JOIN 
    x3v6.CICPROD.SORDERP P ON Q.SOHNUM_0 = P.SOHNUM_0 
                           AND Q.SOPLIN_0 = P.SOPLIN_0 
                           AND Q.SOQSEQ_0 = P.SOPSEQ_0
WHERE
    MONTH(H.ORDDAT_0) = MONTH(CAST(GETDATE() as DATE)) 
    AND YEAR(H.ORDDAT_0) = YEAR(GETDATE()) 
    AND H.SOHCAT_0 = 1
GROUP BY
    H.ORDDAT_0 WITH ROLLUP
ORDER BY 
    H.ORDDAT_0 Desc 

返回以下结果集:

OrderDate   TotalSalesEntered     SalesToShipThisMonth  %SalestoShipThisMonth RollingAvgSalesToShip
----------- --------------------- --------------------- --------------------- ---------------------
Jan 24 2017 21421.3756            19792.0256            0.9239                0.5505
Jan 23 2017 143374.856            63420.7183            0.4423                0.5256
Jan 20 2017 166438.6412           58726.3705            0.3528                0.5316
Jan 19 2017 121885.8537           15869.2483            0.1301                0.5453
Jan 18 2017 97076.5584            28272.884             0.2912                0.5799
Jan 17 2017 161553.0361           62826.0752            0.3888                0.6062
Jan 16 2017 151009.424            108613.0565           0.7192                0.6279
Jan 13 2017 189772.7102           81768.742             0.4308                0.6178
Jan 12 2017 259163.0642           131812.8829           0.5086                0.6411
Jan 11 2017 233858.2092           157041.418            0.6715                0.6601
Jan 10 2017 209945.101            76734.7709            0.3654                0.6582
Jan  9 2017 209248.8475           148410.3441           0.7092                0.7168
Jan  6 2017 182705.7833           89876.8759            0.4919                0.7187
Jan  5 2017 143773.0053           99891.9947            0.6947                0.7943
Jan  4 2017 244474.1372           168264.5745           0.6882                0.8441
Jan  3 2017 143069.9108           143069.9108           1.00                  1.00
[Totals]    2678770.5137          1454391.8922          0.5429                0.55

但是,平均而言正在进行的是如何推动“销售额”排名第一的'%Salestoshipcolumn'按行和平均值(而不是平均值'总销售额和'以及'销售额本月发货'按每天划分)。

我想要的结果集是:

Jan  5 2017 143773.0053 99891.9947  0.6947  0.7943  531317.0533 411226.48   0.773975684 (Jan5+Jan4+Jan3)
Jan  4 2017 244474.1372 168264.5745 0.6882  0.8441  387544.048  311334.4853 0.803352514 (Jan4+Jan3)
Jan  3 2017 143069.9108 143069.9108 1          1    143069.9108 143069.9108 1           (Jan3)

__最后4列是__

的主要内容

我想要的平均值位于倒数第二列(最后一列显示正在发生的情况,即总和是平均而不是平均%SalestoShip列)

这是否有意义?如何使用窗口函数完成此任务?有任何想法吗?感谢。

0 个答案:

没有答案