2017-10-11 146 views
0

如何使支點和程序,如果我有表與存儲過程

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

+0

桑普拉數據是偉大的,但確實是這麼多的需要它? – jarlh

+0

你可以在文本中給出幾行樣本數據,以便我們可以複製? – wast

+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 
+0

如果我想在最終結果數據中添加列總數,如何在代碼中添加列 –

+0

不客氣。我添加了查詢的TotalQty列結束。 –

+0

先生@sarslan 謝謝先生,sarslan 如何一步一步瞭解樞軸,你能向我解釋 –