使用sum值从行到列的每月枢轴日期

时间:2016-09-30 04:04:25

标签: sql-server sql-server-2008-r2

我有一个带有sum值的数据,并映射到与此SQL匹配的那天

  

示例数据和代码

http://sqlfiddle.com/#!3/ca364/6

但我的问题是我无法将日期行转移到列 我需要转向(从上面的sqlfiddle查询)显示为此示例

Date                1   2   3   4   5   6   7   8   9 to end of the month
TAS0B065B999P999    0   0   0   0   0   4900    9900    0   -96600
TAS1B065B999P999    0   0   0   0   0   0   0   0   0
TAS2B065B999P999    0   0   0   0   0   0   0   0   0
TAS3B065B999P999    0   0   0   0   0   0   0   0   0
TAS4B065B999P999    0   0   0   0   0   0   0   0   0
TAS5B065B999P999    0   0   0   0   0   0   0   0   0
TAS6B065B999P999    0   0   0   0   0   0   0   0   0
TAS7B065B999P999    0   0   0   0   0   0   0   0   0
TAS8B065B999P999    0   0   0   0   0   0   0   0   0
TAS9B065B999P999    0   0   0   0   0   0   0   0   0
TASAB065B999P999    0   0   0   0   0   0   0   0   0
TDS1B065B999P999    0   0   0   0   0   0   0   0   0
TDS2B065B999P999    0   0   0   0   0   0   0   0   0
TDS3B065B999P999    0   0   0   0   0   0   0   0   0
TRS1B065B999P999    0   0   0   0   0   0   0   0   0
TRS2B065B999P999    0   0   0   -72900  0   74900   -74900  90900   0
TRS3B065B999P999    0   0   0   21100   0   0   0   0   0
  

示例代码

WITH t1
     AS (SELECT dt,TERM,
                SumCashOUT,
                DATEADD(MONTH, DATEDIFF(MONTH, '1900-01-01', dt), '1900-01-01') AS firstInMonth,
                DATEADD(DAY, -1, DATEADD(MONTH, DATEDIFF(MONTH, '1900-01-01', dt)+1, '1900-01-01')) AS lastInMonth
         FROM
         (
             SELECT CONVERT(  DATE, TA_DATE) AS dt,TERM,
                    DATENAME(dw, CONVERT( DATE, TA_DATE)) AS dn,
                    DATEPART(DW, CONVERT( DATE, TA_DATE)) AS dtdw,
                    OUT1 + OUT2 + OUT3 + OUT4 AS SumCashOUT
             FROM CASH1609

             GROUP BY TA_DATE,
                      TERM,
                      OUT1,
                      OUT2,
                      OUT3,
                      OUT4
         ) AS st
         GROUP BY dt,TERM,
                  dn,
                  dtdw,
                  SumCashOUT),
     Calendar
     AS (SELECT DISTINCT
                DATEADD(Day, c.number, t1.firstInMonth) AS d
         FROM t1
              JOIN master..spt_values AS c ON type = 'P'
                                              AND DATEADD(DAY, c.number, t1.firstInMonth) BETWEEN t1.firstInMonth AND t1.lastInMonth)
     SELECT DATEPART(dd, d) AS Date,TERM,

            CASE
                WHEN CONVERT( NUMERIC(16, 0), SumCashOUT) IS NULL
                THEN 0
                ELSE CONVERT(NUMERIC(16, 0), SumCashOUT)
            END AS SumCashOUT
     FROM calendar AS c
          LEFT JOIN t1 ON t1.dt = c.d
          LEFT JOIN holiday ON c.d = holiday.HDATE

     ORDER BY DAte ASC;

1 个答案:

答案 0 :(得分:2)

您必须在SQL Server中使用PIVOT Command。

这将提供所需的输出:

;WITH tab (TERM, DOM, CASH) AS
(
    SELECT  TERM,
            DAY(CAST(TA_DATE AS DATE)) AS DOM,
            OUT1 + OUT2 + OUT3 + OUT4 AS CASH
    FROM    CASH1609
)
SELECT  *
FROM    tab
PIVOT 
(
    SUM (CASH) 
    FOR DOM IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],
                [12],[13],[14],[15],[16],[17],[18],[19],[20],[21],
                [22],[23],[24],[25],[26],[27],[28],[29],[30],[31])
) AS pvt

“tab”CTE准备了Columns DOM(Day-Of-Month)和CASH。 PIVOT需要一个聚合函数(在您的情况下为SUM)和pivot-column的可能值。

注意:这将为每个不存在的DOM提供NULL,只需替换

SELECT * FROM tab

SELECT  TERM, COALESCE([1],0), COALESCE([2],0), etc... FROM tab...