T-SQL Pivot具有多个列基础枢轴

时间:2017-08-24 12:33:59

标签: sql-server pivot-table

有什么方法可以转动2个基于列的列?我使用1列作为枢轴的基础进行了动态调整,但这次我无法弄明白。

这是我的表架构:

CREATE TABLE [dbo].[ProdOrders]
(
    [ItemCode] [nvarchar](50) NULL,
    [ReleasedDate] [date] NULL,
    [PlanQty] [float] NULL,
    [ActualQty] [float] NULL
)

我填写表格:

INSERT INTO [dbo].[ProdOrders]([ItemCode], [ReleasedDate], [PlanQty], [ActualQty])
VALUES
  ('0D203-22882-079','2017-08-18',12654,4218),
  ('0D203-22882-079','2017-08-23',15758,5253),
  ('0D203-22882-079','2017-08-27',26263,8754),
  ('0D203-22882-079','2017-09-02',7354,2451),
  ('0D301-05422-079','2017-08-18',31144,10381),
  ('0D301-05422-079','2017-08-18',20612,6871),
  ('0D301-05422-079','2017-08-23',25765,8588),
  ('0D301-05422-079','2017-08-29',19582,6527),
  ('0D301-05422-079','2017-09-04',15459,5153),
  ('0D203-22882-079','2017-09-22',5232,1744),
  ('0D203-22882-079','2017-09-28',13236,4412),
  ('0D203-22882-079','2017-10-03',7693,2564),
  ('0D301-05422-079','2017-09-23',24735,8245),
  ('0D301-05422-079','2017-09-27',19561,6520),
  ('0D301-05422-079','2017-09-06',23755,7918),
  ('0D301-05422-079','2017-09-14',23755,7918),
  ('0D301-05422-079','2017-09-17',29694,9898),
  ('0D203-22882-079','2017-11-01',2263,754),
  ('0D203-22882-079','2017-10-21',15693,5231),
  ('0D203-22882-079','2017-10-20',15968,5323),
  ('0D203-22882-079','2017-10-25',10521,3507),
  ('0D301-05422-079','2017-10-21',23755,7918),
  ('0D301-05422-079','2017-10-29',17816,5939),
  ('0D301-05422-079','2017-11-01',15612,5204),
  ('0D301-05422-079','2017-10-03',20816,6939),
  ('0D301-05422-079','2017-10-11',15612,5204),
  ('0D301-05422-079','2017-10-18',26020,8673)

这是预期的输出:

    ItemCode    201708-Plan 201708-Actual   201709-Plan 201709-Actual   201710-Plan 201710-Actual   201711-Plan 201711-Actual
    --------------- ----------- -------------   ----------- -------------   ----------- -------------   ----------- -------------
    0D203-22882-079  54,674      18,225      25,822     8,607       49,875      16,625      2,263       754 
    0D301-05422-079  97,103      32,367      136,959    45,652      104,020     34,673      15,612      5,204 

我必须使用与实际和数量相关联的年份和月份,根据发布日期调整PlanQtyActualQty

我创建了一个数据透视表脚本,但是收到错误:

DECLARE   @PivotColumns AS NVARCHAR(MAX)
SELECT   @PivotColumns = COALESCE(@PivotColumns + ',','') + QUOTENAME([YRMOPLAN]) + QUOTENAME([YRMOACT])
FROM (SELECT DISTINCT  CONVERT(char(6), cast([ReleasedDate] as date), 112) + 'Plan'  as [YRMOPLAN]
,CONVERT(char(6), cast([ReleasedDate] as date), 112) + 'Actual'  as [YRMOACT] FROM [dbo].[ProdOrders]) as PivotQuery ORDER BY [YRMOPLAN] 
SELECT   @PivotColumns 

DECLARE @query NVARCHAR(MAX)
SET @query = 'SELECT * FROM 
             (
             SELECT ItemCode, CONVERT(char(6), cast([ReleasedDate] as date), 112) + ''Plan''  as [YRMOPLAN]
            ,CONVERT(char(6), cast([ReleasedDate] as date), 112) + ''Actual''  as [YRMOACT], PlanQty,ActualQty  
             FROM ProdOrders
             ) x
             PIVOT 
             (
                 SUM(PlanQty)
                 FOR [YRMOPLAN] IN (' + @PivotColumns + ')
            ) p

            PIVOT 
             (
                 SUM(ActualQty)
                 FOR [YRMOACT] IN (' + @PivotColumns + ')
            ) p2;' 
EXEC SP_EXECUTESQL @query

这是我得到的错误:

  

Msg 102,Level 15,State 1,Line 10
  ' 201708Actual'

附近的语法不正确

1 个答案:

答案 0 :(得分:1)

在构建@PivotColums时,你错过了[计划]和[实际]的逗号

DECLARE   @PivotColumns AS NVARCHAR(MAX)
SELECT   @PivotColumns = COALESCE(@PivotColumns + ',','') + QUOTENAME([YRMOPLAN])+',' + QUOTENAME([YRMOACT])
FROM (SELECT DISTINCT  CONVERT(char(6), cast([ReleasedDate] as date), 112) + 'Plan'  as [YRMOPLAN]
,CONVERT(char(6), cast([ReleasedDate] as date), 112) + 'Actual'  as [YRMOACT] FROM [dbo].[ProdOrders]) as PivotQuery ORDER BY [YRMOPLAN] 
SELECT   @PivotColumns 

然而,您可以稍微简化一下

示例

Declare @SQL varchar(max) = '
Select *
 From (
        Select A.ItemCode
              ,B.*
         From  [dbo].[ProdOrders] A
         Cross Apply ( values ( convert(varchar(6),ReleasedDate,112)+''-Plan'',PlanQty)
                             ,( convert(varchar(6),ReleasedDate,112)+''-Actual'',ActualQty)
                     ) B (Item,Value)
      ) S
 Pivot (sum([Value]) For [Item] in (' + Stuff((Select Distinct ','+QuoteName(convert(varchar(6),ReleasedDate,112)+'-Plan') 
                                                              +','+QuoteName(convert(varchar(6),ReleasedDate,112)+'-Actual') 
                                               From [dbo].[ProdOrders]
                                               Order By 1 
                                               For XML Path('')),1,1,'')  + ') ) p'
Exec(@SQL);
--Print @SQL

<强>返回

enter image description here