用变量 - SSIS计算不同月份

时间:2018-05-18 12:10:20

标签: sql-server ssis

我需要进行计算,我从列#34; B"得到总和值。列C =" Y"并将其除以列#34; A"的总和值。列C =" X",与值相差1个月。

示例:

Date            A                B            C
2015-02-31     796.92           885.47        X
2015-02-31     932.2900         932.29        Y
2015-02-31     803.29           1147.56       X
2015-02-31     839.1800         839.18        Y
2015-02-31     139.20           143.50        Y
2015-02-31     299.64           308.91        X
2015-02-31     125.47           129.35        X
2015-02-31      117.98         393.27         Y
2015-02-31     0.00             747.68        X
2015-01-31     735.29           758.03        Y
2015-01-31     0.00             683.24        Y
2015-01-31     0.00             734.25        X
2015-01-31     323.64           333.65        Y
2015-01-31     442.36           456.04        X
2015-03-31    52.84             54.47         X
2015-03-31     0.00             549.96        Y
2015-03-31     462.24           476.54        X

所以我需要使用B(C = Y)中的值除以一个月前的A(C = X)值...

                  A                B             C
2015-02-31     442.36           456.04           X
2015-03-31     52.84             54.47           Y
2015-03-28     75.90             17.19           Y

在上面的这种情况下,它将是(54.47 + 17.19)/ 442.36。

因此,我需要使用SQL中的表来将结果显示在仪表板中。

有人可以帮我吗?

谢谢!

2 个答案:

答案 0 :(得分:2)

如果我理解你的问题,那么这应该有效。这也假设您的样本数据中的月份之间没有差距。如果可能存在间隙,则必须在结果集的顶部覆盖日历表。

DECLARE @T TABLE(Date DATETIME,A DECIMAL(18,4),B  DECIMAL(18,4),C NVARCHAR(10))

INSERT @T (Date,A,B,C) VALUES 
('02/15/2015',5,5,'X'),('02/15/2015',5,5,'Y'),('02/15/2015',5,5,'X'),('02/15/2015',5,5,'Y'),('02/15/2015',5,5,'Y'),
('02/15/2015',5,5,'X'),('02/15/2015',5,5,'X'),('02/15/2015',5,5,'Y'),('02/15/2015',5,5,'X'),('01/15/2015',5,5,'Y'),
('01/15/2015',5,5,'Y'),('01/15/2015',5,5,'X'),('01/15/2015',5,5,'Y'),('01/15/2015',5,5,'X'),('03/15/2015',5,5,'X'),
('03/15/2015',5,5,'Y'),('03/15/2015',5,5,'X')

;WITH Detail AS
(
    SELECT 
        A=CASE WHEN C='X' THEN A ELSE NULL END,
        B=CASE WHEN C='Y' THEN B ELSE NULL END,
        C,
        Month = DATEPART(MONTH,Date),
        Year = DATEPART(YEAR,Date)
    FROM @T
)
,GroupedByMonth AS
(
    SELECT
        RowNumber = ROW_NUMBER() OVER(PARTITION BY 1 ORDER BY Year,Month),
        SumA = SUM(A),
        SumB = SUM(B),
        Month,
        Year 
    FROM 
        Detail
    GROUP BY
        Year,Month
)

SELECT
    This.Year,
    This.Month,
    ThisMonthSumB = This.SumB,
    NextMonthSumA = Next.SumA,
    Result = CASE WHEN Next.SumA = 0 THEN 0 ELSE This.SumB / Next.SumA END
FROM 
    GroupedByMonth This
    LEFT OUTER JOIN GroupedByMonth Next ON Next.RowNumber = This.RowNUmber+1

或使用前一个月的SUM(A)

SELECT
    This.Year,
    This.Month,
    ThisMonthSumB = This.SumB,
    PriorMonthSumA = Prior.SumA,
    Result = CASE WHEN Prior.SumA = 0 THEN 0 ELSE This.SumB / Prior.SumA END
FROM 
    GroupedByMonth This
    LEFT OUTER JOIN GroupedByMonth Prior ON Prior.RowNumber = This.RowNUmber-1

答案 1 :(得分:1)

使用CASE表达式或子查询并对它们进行数学运算。

Psuedo示例:

SELECT 
  SUM(CASE WHEN Condition for B is true THEN B ELSE 0 END)/SUM(CASE WHEN Condition for A is true THEN A ELSE 0 END)
FROM ...

SELECT
  (Subquery that gets the Bs) / (Subquery that gets the As)