如何在查询中计算去年的收入?

时间:2017-02-16 10:39:18

标签: sql sql-server-2012

我需要计算当年和上一年的收入,我希望的输出是:

Date            revenue_current   revenue_previous

January-2017        350                450

December-2016       750                250

November-2016       4550               4230

(去年收入与上一年度相同)

数据:

SaleDate_Yr | SaleDate_Pd | Revenue

2017               1         100

2017               1         200

2017               1         300

2016               11        100

2016               11         50

2017               1         50

2016               12        50

2016               12        50

2016               1         100

2015               11        300

2015               12        520

2016               1         100

(收入数字不会在所需的输出中加起来,或者样本数据请假装他们这样做。)

到目前为止我的查询:

SELECT (CONVERT(varchar(10), SaleDate_Pd) + '-' + '2017') AS sale_pd, 

SUM(CASE WHEN SaleDate_Yr IN ('2017') THEN Total_Revenue ELSE 0 END)
as revenue_ty, 

SUM(CASE WHEN SaleDate_Yr IN ('2016') THEN Total_Revenue ELSE 0 END) as revenue_ly

FROM

[sales].[dbo].[mycompany]

WHERE

SaleDate_Yr IN ('2016', '2017')
AND
SaleDate_Pd IN ('11','12','1')

GROUP BY (CONVERT(varchar(10), SaleDate_Pd) + '-' + '2017')

如果我看一下2017年1月到2017年12月的销售情况,这似乎工作正常。但有时我想查看2016年11月,2016年12月,2017年1月等具体时间段,因此上一年的数字不会工作在11月和12月的日期,并且也会出现在2017年11月的日期栏中,我将年份追加到最后。我手动添加了年份,因为如果我没有每年都有单独的行,而且当前年份和上一年的数字不会出现在同一行。

2 个答案:

答案 0 :(得分:1)

您可以访问最后一行并使用子查询,但我不能告诉您它是如何工作的。

如果年份是数字(转换的使用实际上暗示了这一点),你可以做一个奇特的外部联接。

创建一个包含每年和每月一行的视图可能是有意义的。如果您不想要视图,可以将其替换为相应的子查询。

CREATE VIEW revenue_per_month AS
select 
  SaleDate_Pd,
  SaleDate_Yr,
  /* Probably it's not summed, you need to write the query the
     way that you know the monthly revenue */
  SUM(Revenue)
from mycompany
group by SaleDate_Pd, SaleDate_Yr

select
  CONVERT(varchar(10), thisYear.SaleDate_Pd)
     + '-' 
     + CONVERT(varchar(10), thisYear.SaleDate_Yr) as Date,
  thisYear.Revenue as revenue_current,
  lastYear.Revenue as revenue_previous
FROM revenue_per_month thisYear 
  left outer join revenue_per_month lastYear 
    on (
      thisYear.SaleDate_Yr - 1 = lastYear.SaleDate_Yr 
      AND thisYear.SaleDate_Pd = lastYear.SaleDate_Pd)

答案 1 :(得分:0)

Create Table R
    (y Int, m Int, r int)

Insert Into R Values
    (2017, 1, 11000)
    ,(2016, 12, 1200)
    ,(2016, 11, 1100)
    ,(2016, 10, 500)
    ,(2016, 10, 500)
    ,(2016, 9, 900)
    ,(2016, 8, 800)
    ,(2016, 7, 700)
    ,(2016, 6, 600)
    ,(2016, 5, 250)
    ,(2016, 5, 250)
    ,(2016, 4, 300)
    ,(2016, 4, 100)
    ,(2016, 3, 300)
    ,(2016, 2, 200)
    ,(2016, 1, 100)
    ,(2015, 12, 12)
    ,(2015, 11, 5)
    ,(2015, 11, 6)
    ,(2015, 10, 10)
    ,(2015, 9, 9)
;

With _r(y ,m, r, D)
    As
        (
            Select   y, m, Sum(r), Cast(Cast(y As Char(4)) + Right('0' + Cast(m AS VarChar(2)), 2) + '01' As Date)
              From   r
              Group
              By     y, m
    )
Select   pc.y As Year, pc.m As Month, pc.r As Revenue_Current, IsNull(pp.r, 0) As Revenue_Previous
  From   _r As pc
  Left
  Join   _r As pp
    On      pp.d = DateAdd(Year, -1, pc.d)
  Where  pc.D Between '20161001' And  '20170101'
  Order
     By Pc.d;


(21 row(s) affected)
Year        Month       Revenue_Current Revenue_Previous
----------- ----------- --------------- ----------------
2016        10          1000            10
2016        11          1100            11
2016        12          1200            12
2017        1           11000           100

(4 row(s) affected)