计算查询结果中的关系

时间:2014-02-27 16:25:52

标签: sql sql-server reporting-services ssis

我目前遇到的结果集如下:

| Month   | LastWeekForMonth | ValueForLastWeekOfMonth |
| 2013-09 |     2013-40      |      981408,27          |
| 2013-10 |     2013-44      |      931209,12          |
| 2013-11 |     2013-48      |      1081302,00         |
| 2013-12 |     2013-52      |      935418,21          |
| 2014-01 |     2014-05      |      911402,11          |
| 2014-02 |     2014-09      |      991201,10          |

这是我使用以下查询计算:

  SELECT C.Month
        ,RT.[Weekstamp] as LastWeekForMonth
        ,Sum([RevenueValue]) as ValueForLastWeekOfMonth
  FROM [Database].[dbo].[fact_RevenueTable] RT
  INNER JOIN CalenderWeekTable CW on RT.Weekstamp = CW.Weekstamp
  INNER JOIN CalenderTable C on CW.KalenderID = C.KalenderID
  WHERE RT.Probability <= 1
        AND RT.Weekstamp IN ( 
                             SELECT max(CW.[Weekstamp])
                             FROM [Database].[dbo].[CalenderWeekTable] CW
                             INNER JOIN CalenderTable C on CW.KalenderID = C.KalenderID
                             WHERE C.Month >= @FromSQLMonth and C.Month < @ToSQLMonth
                             GROUP BY C.Month
                             )
  GROUP BY RT.Weekstamp,C.Month

我现在需要的是:

| Month   | LastWeekForMonth | ValueForLastWeekOfMonth | QuotientToPreviousMonth |
| 2013-09 |     2013-40      |      981408,27          |          1.04           |
| 2013-10 |     2013-44      |      931209,12          |     0.948849880794259   |
| 2013-11 |     2013-48      |      1081302,00         |     1.161180637921587   |
| 2013-12 |     2013-52      |      935418,21          |     0.8650850641171477  |
| 2014-01 |     2014-05      |      911402,11          |     0.9743258151880537  |
| 2014-02 |     2014-09      |      991201,10          |     1.087556292798137   |

所以我需要的是一个新的列,它将显示商数之间:

ValueForLastWeekOfMonth / ValueForLastWeekOf 上一页

现在问题对你们所有人来说都很有帮助人们:

有没有办法在SQL中完成此任务?

由于我想在SQL Server中(或者如果在SSIS中)计算它,这意味着可以使用SQL Server 2012中提供的所有工具。

感谢大家提前,我期待着你的聆听!

2 个答案:

答案 0 :(得分:2)

查询:

SELECT C.Month
        ,RT.[Weekstamp] as LastWeekForMonth
        ,Sum([RevenueValue]) as ValueForLastWeekOfMonth
        ,Sum([RevenueValue])/ 
         LAG(Sum([RevenueValue]))OVER(ORDER BY Month) AS QuotientToPreviousMonth 
  FROM [Database].[dbo].[fact_RevenueTable] RT
  INNER JOIN CalenderWeekTable CW on RT.Weekstamp = CW.Weekstamp
  INNER JOIN CalenderTable C on CW.KalenderID = C.KalenderID
  WHERE RT.Probability <= 1
        AND RT.Weekstamp IN ( 
                             SELECT max(CW.[Weekstamp])
                             FROM [Database].[dbo].[CalenderWeekTable] CW
                             INNER JOIN CalenderTable C on CW.KalenderID = C.KalenderID
                             WHERE C.Month >= @FromSQLMonth and C.Month < @ToSQLMonth
                             GROUP BY C.Month
                             )
  GROUP BY RT.Weekstamp,C.Month

结果:

|                            MONTH | LASTWEEKFORMONTH | VALUEFORLASTWEEKOFMONTH |       COLUMN_3 |
|----------------------------------|------------------|-------------------------|----------------|
| September, 01 2013 00:00:00+0000 |          2013-40 |               981408.27 |         (null) |
|   October, 01 2013 00:00:00+0000 |          2013-44 |               931209.12 | 0.948849880794 |
|  November, 01 2013 00:00:00+0000 |          2013-48 |                 1081302 | 1.161180637922 |
|  December, 01 2013 00:00:00+0000 |          2013-52 |               935418.21 | 0.865085064117 |
|   January, 01 2014 00:00:00+0000 |          2014-05 |               911402.11 | 0.974325815188 |
|  February, 01 2014 00:00:00+0000 |          2014-09 |                991201.1 | 1.087556292798 |

我认为您可以像这样重写您的查询:

  SELECT a.Month,
         a.LastWeekForMonth,
         a.ValueForLastWeekOfMonth,
         a.ValueForLastWeekOfMonth/ 
         LAG(ValueForLastWeekOfMonth)OVER(ORDER BY a.Month) AS QuotientToPreviousMonth 
  FROM(
  SELECT C.Month
        ,RT.[Weekstamp] as LastWeekForMonth
        ,Sum([RevenueValue]) as ValueForLastWeekOfMonth,
        ROW_NUMBER()OVER(PARTITION BY C.Month ORDER BY RT.Weekstamp DESC) AS rnk
  FROM [Database].[dbo].[fact_RevenueTable] RT
   INNER JOIN CalenderWeekTable CW on RT.Weekstamp = CW.Weekstamp
   INNER JOIN CalenderTable C on CW.KalenderID = C.KalenderID
  WHERE RT.Probability <= 1
   AND C.Month >= @FromSQLMonth and C.Month < @ToSQLMonth
  GROUP BY RT.Weekstamp,C.Month) a
  WHERE a.rnk = 1

答案 1 :(得分:1)

;WITH YourPostedCode AS
(
    SELECT C.Month
        ,RT.[Weekstamp] as LastWeekForMonth
        ,Sum([RevenueValue]) as ValueForLastWeekOfMonth
    FROM [Database].[dbo].[fact_RevenueTable] RT
    INNER JOIN CalenderWeekTable CW on RT.Weekstamp = CW.Weekstamp
    INNER JOIN CalenderTable C on CW.KalenderID = C.KalenderID
    WHERE RT.Probability <= 1
    AND RT.Weekstamp IN ( 
        SELECT max(CW.[Weekstamp])
        FROM [Database].[dbo].[CalenderWeekTable] CW
        INNER JOIN CalenderTable C on CW.KalenderID = C.KalenderID
        WHERE C.Month >= @FromSQLMonth and C.Month < @ToSQLMonth
        GROUP BY C.Month
        )
    GROUP BY RT.Weekstamp,C.Month
)
SELECT c.[Month], c.LastWeekForMonth, c.ValueForLastWeekOfMonth,
    ValueForLastWeekOfMonth / Lag(ValueForLastWeekOfMonth) OVER(PARTITION BY NULL
        ORDER BY c.[Month]) AS QuotientToPreviousMonth 
FROM YourPostedCode c