SQL Server:查找所选月份和上个月销售额之间的变化

时间:2017-10-31 09:41:37

标签: sql sql-server

我有以下表格。我想计算从上个月开始增加或减少的变化。这将显示与上个月相比的变化百分比。

Location    Month   Sales
A           Jan     1753
B           Jan     32130
C           Jan     71353
D           Jan     74885
E           Jan     50241
F           Jan     66393
A           Feb     80633
B           Feb     67918
C           Feb     73330
D           Feb     33269
E           Feb     78915
F           Feb     98817
A           Mar     80633
B           Mar     67918
C           Mar     73330
D           Mar     33269
E           Mar     78915
F           Mar     98817

我想创建一个像下面这样的表。我搜索了堆栈溢出但无法得到表。

Location    Selected    Current_Month   Prvisous_Month  Change
A           Feb         80633           1753            4500%
B           Feb         67918           32130           111%
C           Feb         73330           71353           3%
D           Feb         33269           74885           -56%
E           Feb         78915           50241           57%
F           Feb         98817           66393           49%

4 个答案:

答案 0 :(得分:1)

如果您无法更改"月"的数据类型。无论出于何种原因,该列都可以使用此解决方案

DECLARE @Table TABLE ([Location] CHAR(1), [Month] NVARCHAR(3), Sales INT )
INSERT INTO @Table
    ([Location], [Month], Sales)
VALUES
    ('A',N'Jan',1753),
    ('B',N'Jan',32130),
    ('C',N'Jan',71353),
    ('D',N'Jan',74885),
    ('E',N'Jan',50241),
    ('F',N'Jan',66393),
    ('A',N'Feb',80633),
    ('B',N'Feb',67918),
    ('C',N'Feb',73330),
    ('D',N'Feb',33269),
    ('E',N'Feb',78915),
    ('F',N'Feb',98817),
    ('A',N'Mar',80633),
    ('B',N'Mar',67918),
    ('C',N'Mar',73330),
    ('D',N'Mar',33269),
    ('E',N'Mar',78915),
    ('F',N'Mar',98817)

DECLARE @Selection NVARCHAR(3) = N'Feb' -- Enter Selected Month here

;WITH cteX
AS(
    SELECT
          T.[Location]
        , T.[Month]
        , MonthNum = MONTH([T].[Month] + ' 1 1900') --Use some dummy date here
        , T.Sales
    FROM @Table T
)
SELECT
      T.[Location]
    , Selected      = T.Month
    , CurrentMonth  = T.Sales
    , PreviousMonth = T1.Sales
    , Change        = CAST((T.Sales - T1.Sales) / (T1.Sales * 1.0) * 100.0 AS DECIMAL)
FROM cteX   T
INNER JOIN
    cteX    T1  ON  T1.MonthNum = T.MonthNum - 1
                AND T1.[Location] = T.[Location]
WHERE
    T.[Month] = @Selection

输出

enter image description here

答案 1 :(得分:0)

这样的事情应该是一个好的开始。正如Cool_Br33ze所指出的,你应该重新考虑这个表的日期列。

SELECT 
  *,
  referenceTimePeriod / NULLIF(comparisonTimePeriod, 0) -- avoid DIV0 errors
FROM (
  SELECT Location, Sales
  FROM myTable
  WHERE month = 'jan'
) AS referenceTimePeriod
FULL JOIN(
  SELECT Location, Sales
  FROM myTable
  WHERE month = 'feb'
) AS comparisonTimePeriod ON referenceTimePeriod.Location = comparisonTimePeriod .Location

答案 2 :(得分:0)

假设您将Month属性更改为日期,那么您可以像这样轻松使用LAG窗口函数

SELECT location,
       month,
       sales,
       lag(sales) over (order by month) previous, 
       (sales/lag(sales) over (order by month) - 1) * 100 as change
FROM your_table
WHERE month = 'feb'

您的任务中现在的主要问题是Month的正确排序,这对日期或数字来说会更容易。

编辑:您可以使用Cool_Br33ze的订购解决方案获取当前数据:

SELECT location,
       month,
       sales,
       lag(sales) over (order by MONTH([T].[Month] + ' 1 1900')) previous, -- taken from Cool_Br33ze solution
       (sales/lag(sales) over (order by MONTH([T].[Month] + ' 1 1900')) - 1) * 100 as change
FROM your_table
WHERE month = 'feb'

但是,最好的选择是更改Month ...

的数据类型

答案 3 :(得分:0)

SELECT A.Location, 'Feb' AS Selected, A.Sales AS Current_Month 
                   , B.Sales AS Prvisous_Month, (A.Sales - B.Sales)/ B.Sales AS Change 
FROM YourTable A JOIN YourTable B
                  ON A.Month = B.Month + 1   -- You will have to represent Months by numbers
WHERE A.Month = 2                        -- Selected month