2017-05-26 48 views
1

如何將SQL Server數據行轉換爲多列?我有以下數據如何將SQL Server行轉換爲多列?

 [STOCK NO]  [PN1]   [PN2]    [PN3]   [PN4]       [PN4]   [PN5]   [PN6]  [PN7]  [PN8]     [PN9] [PN10] [PN11] [PN12] [Qty] 
    700349L   600446   201743   100208          300219   400533     100280  100218     100222   100220 100221  1 

類似節目我希望看到這樣的事情

[STOCK NO]  [bomRev]  [bomEntry] [partId] [qty] [cmnt] [srcLoc] [dType] [lead] [lineNbr] 
700349L    A    1   600446 1  `TEST` TEST  0   0  1 
700349L    A    2   201743 1  `TEST` TEST  0   0  2 
700349L    A    3   100208 1  `TEST` TEST  0   0  3 
700349L    A    4   300219 1  `TEST` TEST  0   0  4 

700349L    A    5   400533 1  `TEST` TEST  0   0  5 

700349L    A    6   100218 1  `TEST` TEST  0   0  6 
700349L    A    7   100222 1  `TEST` TEST  0   0  7 
700349L    A    8   100220 1  `TEST` TEST  0   0  8 
700349L    A    9   100221 1  `TEST` TEST  0   0  9 

然後我想使用select語句插入如下圖所示,但我想調換行多列

INSERT INTO [DT]([bomItem], [bomRev], [bomEntry], [partId], [qty], [cmnt],[srcLoc], [dType], [lead], [lineNbr]) 
    SELECT 
     [STOCK NO], 'A', [bomEntry], [partid], [qty], 'TEST', 'TEST', '0', '0', [lineNbr] 
    FROM 
     [ST] 

skip if blank

+0

的可能的複製[移調/樞軸行列和總和](https://stackoverflow.com/questions/18322488/transpose-pivot-rows-to-columns-and-sum)和[SQL總計 - 樞軸列](https://stackoverflow.com/q/25293671/62576) –

回答

0

這使用問題中的其他編輯之前的列和示例數據。

使用cross apply()到unpivot的欄位,並row_number()至數只具有一個值的那些行:

--INSERT INTO [DT]([bomItem], [bomRev], [bomEntry], [partId], [qty], [cmnt],[srcLoc], [dType], [lead], [lineNbr]) 
select 
    st.[CALC STOCK NO]  
    , bomRev = 'A' 
    , bomEntry = row_number() over (order by u.ordinal) 
    , u.Partid 
    , st.Qty 
    , cmnt = 'Test' 
    , srcLoc = 'Test' 
    , dType = '0' 
    , lead = '0' 
    , lineNbr = row_number() over (order by u.ordinal) 
from st 
    cross apply (values 
    (1,[BOM WHEEL PN]) 
    ,(2,[BOM TIRE PN]) 
    ,(3,[BOM VALVE PN]) 
    ,(4,[BOM - SECONDARY DISC PN]) 
    ,(5,[BOM - DISC]) 
    ,(6,[BOM - RIM]) 
    ,(7,[BUY WHEEL]) 
    ,(8,[COLOR PN]) 
    ,(9,[WHL BOM PART 1 PN]) 
    ,(10,[WHL BOM PART 2 PN]) 
    ,(11,[WHL BOM PART 3 PN]) 
    ,(12,[WHL BOM PART 4 PN]) 
    ,(13,[WHL BOM PART 5 PN]) 
) u (ordinal, partId) 
where nullif(u.partId,'') is not null 

rextester演示:http://rextester.com/XNGB57562

回報:

+---------------+--------+----------+--------+-----+------+--------+-------+------+---------+ 
| CALC STOCK NO | bomRev | bomEntry | Partid | Qty | cmnt | srcLoc | dType | lead | lineNbr | 
+---------------+--------+----------+--------+-----+------+--------+-------+------+---------+ 
| 700349L  | A  |  1 | 600446 | 1 | Test | Test |  0 | 0 |  1 | 
| 700349L  | A  |  2 | 201743 | 1 | Test | Test |  0 | 0 |  2 | 
| 700349L  | A  |  3 | 100208 | 1 | Test | Test |  0 | 0 |  3 | 
| 700349L  | A  |  4 | 300219 | 1 | Test | Test |  0 | 0 |  4 | 
| 700349L  | A  |  5 | 400533 | 1 | Test | Test |  0 | 0 |  5 | 
| 700349L  | A  |  6 | 100280 | 1 | Test | Test |  0 | 0 |  6 | 
| 700349L  | A  |  7 | 100218 | 1 | Test | Test |  0 | 0 |  7 | 
| 700349L  | A  |  8 | 100222 | 1 | Test | Test |  0 | 0 |  8 | 
| 700349L  | A  |  9 | 100220 | 1 | Test | Test |  0 | 0 |  9 | 
| 700349L  | A  |  10 | 100221 | 1 | Test | Test |  0 | 0 |  10 | 
+---------------+--------+----------+--------+-----+------+--------+-------+------+---------+ 
+0

非常感謝你!兩件事1.我可以使用插入的表來創建觸發器嗎? 2.我如何忽略/跳過如果空白部分我添加圖像來顯示 – user8066749

+0

@ user8066749更新我的答案,以刪除結果中的空字符串'nullif(u.partId,'')不是空'而不是'u。 partid不爲空「。我還在查詢之前包含了一條註釋掉的'insert'語句,您可以使用該語句插入到您所描述的表中。 – SqlZim

+0

非常感謝!我可以插入而不是'st'右鍵創建觸發器? – user8066749