t-sql pivot - 月收入

时间:2013-12-30 20:02:50

标签: sql-server tsql pivot

我无法确定如何为我的收入查询构建PIVOT

首先,这是我的问题:

SELECT  DATENAME(month,
             CONVERT(DATETIME, CONVERT(VARCHAR, TransactionMonth) + '/1'
             + '/2013')) AS MONTH ,
    RevenueGroup ,
    AMOUNT
FROM    ( SELECT    f.TransactionMonth ,
                f.RevenueGroupID ,
                '$'
                + CONVERT(VARCHAR, CAST(SUM(f.LineItemAmount) AS MONEY), -1) AS Revenue ,
                MIN(f.RevenueGroup) AS RevenueGroup
      FROM      Financials(@StartDate, @EndDate, @SiteID) f
      GROUP BY  f.TransactionMonth ,
                f.RevenueGroupID
    ) AS Rev
ORDER BY Rev.TransactionMonth DESC ,
        CAST(Revenue AS MONEY) DESC

这是我的结果集:

MONTH       RevenueGroup         AMOUNT
December    Dues - Collect       $425,725.71
December    NMS                  $78,444.17
December    Personal Training    $58,511.53
November    Dues - Collect       $425,683.75
November    NMS                  $114,710.25
November    Personal Training    $66,277.49
October     Dues - Collect       $419,250.09
October     NMS                  $96,098.86
October     Personal Training    $58,725.77

以下是结果集需要如何处理sql PIVOT:

RevenueGroup        DECEMBER      NOVEMBER      OCTOBER
Dues - Collect      $425,725.71   $425,683.75   $419,250.09
NMS                 $78,444.17    $114,710.25   $96,098.86
Personal Training   $58,511.53    $66,277.49    $58,725.77

如何使用T-SQL PIVOT实现这一目标?

2 个答案:

答案 0 :(得分:7)

测试数据

CREATE TABLE #MyTable ([MONTH] NVARCHAR(20),RevenueGroup NVARCHAR(100),AMOUNT NUMERIC(20,2))
GO
INSERT INTO #MyTable
VALUES
('December','Dues - Collect',       425725.71),
('December','NMS',                  78444.17),
('December','Personal Training',    58511.53),
('November','Dues - Collect',       425683.75),
('November','NMS',                  114710.25),
('November','Personal Training',    66277.49),
('October','Dues - Collect',       419250.09),
('October','NMS',                  96098.86),
('October','Personal Training',    58725.77)

<强>查询

SELECT RevenueGroup, [October],[November],[December]
FROM (SELECT * FROM #MyTable)t   --<-- Your Existing Query can go in here but do not
  PIVOT                             -- not do any formating like Dollar sign as you want
      (                             -- sum the results by month later on    
      SUM(AMOUNT)
      FOR [MONTH]
      IN ([October],[November],[December])
      )P

<强>结果

╔═══════════════════╦═══════════╦═══════════╦═══════════╗
║   RevenueGroup    ║  October  ║ November  ║ December  ║
╠═══════════════════╬═══════════╬═══════════╬═══════════╣
║ Dues - Collect    ║ 419250.09 ║ 425683.75 ║ 425725.71 ║
║ NMS               ║ 96098.86  ║ 114710.25 ║ 78444.17  ║
║ Personal Training ║ 58725.77  ║ 66277.49  ║ 58511.53  ║
╚═══════════════════╩═══════════╩═══════════╩═══════════╝

答案 1 :(得分:1)

这已经有了最佳答案,这并不是一个答案,而是一个附录提示如何捕捉动态SQL的动态月份...

Declare @dynamicColumns Nvarchar(64)

Select  @dynamicColumns = Coalesce(@dynamicColumns + ',','') + '[' + m.months + ']'
From   (Select  0 As monthSubtract
        Union
        Select  1
        Union
        Select  2) As ms
Cross   Apply (Select DateName(MONTH,DATEADD(MONTH, -ms.monthSubtract, GETDATE())) As months) m
Order   By ms.monthSubtract Desc

Select  @dynamicColumns