带存储过程的动态数据透视

时间:2017-10-11 11:26:53

标签: sql pivot

如果我有桌子

,如何制作枢轴和程序
CREATE TABLE IncentiveHdr
(
    Style nvarchar(50) NOT NULL,
    Line nvarchar(50) NOT NULL,
    Period varchar(6) NOT NULL,
    Prod_Date datetime NOT NULL,
    Prod_Qty int NULL ,
    Prod_PlusMinus int NULL 
)

enter image description here

如果需要如下所示

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以使用此查询。它确定所有必要的列并在PIVOT函数中使用它。

DECLARE @ColumnNames NVARCHAR(MAX) =''
SELECT  @ColumnNames = @ColumnNames + ', ' + QUOTENAME ( ColName ) + ', ' + QUOTENAME ( ColName +' pulus minus' )  
FROM (SELECT DISTINCT CONVERT(VARCHAR,Prod_Date,6) ColName FROM IncentiveHdr ) AS T
SET @ColumnNames = STUFF(@ColumnNames,1,1,'')


DECLARE @SqlText NVARCHAR(MAX)
SET @SqlText  = 
    'SELECT * FROM 
         (SELECT Style, CONVERT(VARCHAR,Prod_Date,6) Prod_Date, Prod_Qty FROM IncentiveHdr
            UNION ALL
          SELECT Style, CONVERT(VARCHAR,Prod_Date,6) +'' pulus minus'' Prod_Date, Prod_PlusMinus Prod_Qty FROM IncentiveHdr
            UNION ALL
          SELECT Style, ''TotalQty'' Prod_Date, SUM (Prod_Qty + Prod_PlusMinus) Prod_Qty FROM IncentiveHdr GROUP BY Style
           ) SRC
       PIVOT (MAX(Prod_Qty) FOR Prod_Date IN (' + @ColumnNames + ', [TotalQty]) ) PVT'

EXEC sp_executesql @SqlText

结果:

Style                                              01 Sep 17   01 Sep 17 pulus minus 02 Sep 17   02 Sep 17 pulus minus 03 Sep 17   03 Sep 17 pulus minus 04 Sep 17   04 Sep 17 pulus minus 05 Sep 17   05 Sep 17 pulus minus 06 Sep 17   06 Sep 17 pulus minus 07 Sep 17   07 Sep 17 pulus minus 08 Sep 17   08 Sep 17 pulus minus 09 Sep 17   09 Sep 17 pulus minus 10 Sep 17   10 Sep 17 pulus minus 11 Sep 17   11 Sep 17 pulus minus 12 Sep 17   12 Sep 17 pulus minus 13 Sep 17   13 Sep 17 pulus minus 14 Sep 17   14 Sep 17 pulus minus 15 Sep 17   15 Sep 17 pulus minus 16 Sep 17   16 Sep 17 pulus minus 17 Sep 17   17 Sep 17 pulus minus 18 Sep 17   18 Sep 17 pulus minus 19 Sep 17   19 Sep 17 pulus minus 20 Sep 17   20 Sep 17 pulus minus 21 Sep 17   21 Sep 17 pulus minus 22 Sep 17   22 Sep 17 pulus minus 23 Sep 17   23 Sep 17 pulus minus 24 Sep 17   24 Sep 17 pulus minus 25 Sep 17   25 Sep 17 pulus minus 26 Sep 17   26 Sep 17 pulus minus 27 Sep 17   27 Sep 17 pulus minus 28 Sep 17   28 Sep 17 pulus minus 29 Sep 17   29 Sep 17 pulus minus 30 Sep 17   30 Sep 17 pulus minus TotalQty
-------------------------------------------------- ----------- --------------------- ----------- --------------------- ----------- --------------------- ----------- --------------------- ----------- --------------------- ----------- --------------------- ----------- --------------------- ----------- --------------------- ----------- --------------------- ----------- --------------------- ----------- --------------------- ----------- --------------------- ----------- --------------------- ----------- --------------------- ----------- --------------------- ----------- --------------------- ----------- --------------------- ----------- --------------------- ----------- --------------------- ----------- --------------------- ----------- --------------------- ----------- --------------------- ----------- --------------------- ----------- --------------------- ----------- --------------------- ----------- --------------------- ----------- --------------------- ----------- --------------------- ----------- --------------------- ----------- --------------------- -----------
FD3326/7/8                                         0           0                     0           0                     0           0                     0           0                     0           0                     0           0                     0           0                     0           0                     0           0                     0           0                     0           0                     0           0                     0           0                     0           0                     0           0                     0           0                     0           0                     60          0                     120         0                     160         0                     0           0                     190         0                     160         0                     0           0                     210         0                     230         0                     240         0                     250         0                     240         0                     200         0                     2060
NF0A2TAK                                           0           0                     128         0                     0           0                     197         0                     263         0                     124         0                     0           0                     0           0                     0           0                     0           0                     0           0                     0           0                     0           0                     0           0                     0           0                     0           0                     0           0                     0           0                     0           0                     0           0                     0           0                     0           0                     0           0                     0           0                     0           0                     0           0                     0           0                     0           0                     0           0                     0           0                     712