如何将SQL Server行转置为多列?

时间:2017-05-26 17:18:12

标签: sql-server tsql

如何将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,如下所示,但我喜欢将行转置为多列

 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

2 个答案:

答案 0 :(得分:0)

枢轴应该适合你。
查看此链接:https://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx
如果您发布了足够的虚拟数据,则可以帮助您查询。您可以使用http://rextester.com/之类的内容来共享虚拟数据 祝你好运!

答案 1 :(得分:0)

这会在问题中进行其他修改之前使用列和示例数据。

使用cross apply()取消忽略您的列,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 |
+---------------+--------+----------+--------+-----+------+--------+-------+------+---------+