将多行显示为列的最佳方法

时间:2013-11-10 20:47:15

标签: sql sql-server

所以我写了下面的查询,但是我遇到了结果集的问题。我目前知道如何解决这个问题的唯一方法是创建MULTIPLE视图,并做几个左连接,但我怀疑这是效率最低的方法。我需要抓住一些行并将它们转换为列,我只是不知道该怎么做。

use TSTCO
GO

select job.MANUFACTUREORDER_I as MONumber, job.ENDDATE as DueDate, job.ITEMNMBR, item.ITEMDESC, MANUFACTUREORDPRI_I as MOPriority, bom.CPN_I,
item2.ITEMDESC
from WO010032 job
left join iv00101 item on job.ITEMNMBR=item.ITEMNMBR
left join BM010115 bom on bom.PPN_I = job.ITEMNMBR and bom.BOMNAME_I = job.BOMNAME_I
left join iv00101 item2 on bom.CPN_I = item2.ITEMNMBR
where MANUFACTUREORDER_I like '2007-13580'
and (item2.itemdesc like '10:%' or item2.itemdesc like '09:%'  or item2.itemdesc like '101:%'  or item2.itemdesc like '12:%'  or item2.itemdesc like '13:%'  or item2.itemdesc like '131:%'  or item2.itemdesc like '14:%'  or item2.itemdesc like '15:%'  or item2.itemdesc like '16:%'  or item2.itemdesc like '17:%'  or item2.itemdesc like '18:%'  or item2.itemdesc like '21:%'  or item2.itemdesc like '22:%'  or item2.itemdesc like '23:%'  or item2.itemdesc like '24:%')
Order by item2.Itemdesc

我当前的结果集如下所示:

MONumber                        DueDate                 ITEMNMBR                        ITEMDESC                                                                                              MOPriority CPN_I                           ITEMDESC
------------------------------- ----------------------- ------------------------------- ----------------------------------------------------------------------------------------------------- ---------- ------------------------------- -----------------------------------------------------------------------------------------------------
2007-13580                      2013-11-08 00:00:00.000 111.3101                        High Cut Full Back, Gripper Leg                                                                       2          JP1903-000                      10: PFP Lycra/H 5219                                                                                 
2007-13580                      2013-11-08 00:00:00.000 111.3101                        High Cut Full Back, Gripper Leg                                                                       2          JP2203-000                      10: Std 88/12 Lycra:White                                                                            
2007-13580                      2013-11-08 00:00:00.000 111.3101                        High Cut Full Back, Gripper Leg                                                                       2          Z-GRAPHIC DESIGN 15 SHOTGUN     12: Z-Graphic Design 15 Shotgun                                                                      
2007-13580                      2013-11-08 00:00:00.000 111.3101                        High Cut Full Back, Gripper Leg                                                                       2          Z-PANEL A BLACK                 13: Panel A Black                                                                                    
2007-13580                      2013-11-08 00:00:00.000 111.3101                        High Cut Full Back, Gripper Leg                                                                       2          Z-PANEL B BROWN                 13: Panel B Brown                                                                                    
2007-13580                      2013-11-08 00:00:00.000 111.3101                        High Cut Full Back, Gripper Leg                                                                       2          Z-PANEL C CARDINAL              13: Panel C Cardinal                                                                                 
2007-13580                      2013-11-08 00:00:00.000 111.3101                        High Cut Full Back, Gripper Leg                                                                       2          Z-NECK TRIM BLACK               14: NeckTrim Black                                                                                   
2007-13580                      2013-11-08 00:00:00.000 111.3101                        High Cut Full Back, Gripper Leg                                                                       2          Z-THREAD FLATLOCK:BROWN         16: Flatlock Thread Color:Brown                                                                      

我希望它看起来像:

MONumber                        DueDate                 ITEMNMBR                        ITEMDESC                          MOPriority FABRIC                             Graphic                           Panel A               Panel B              Panel C                Neck Trim            Flatlock Thread
------------------------------- ----------------------- ------------------------------- --------------------------------- ---------- ---------------------------------  --------------------------------- --------------------  -------------------- --------------------   -------------------  --------------------------------
2007-13580                      2013-11-08 00:00:00.000 111.3101                        High Cut Full Back, Gripper Leg   2          10: PFP Lycra/H 5219               12: Z-Graphic Design 15 Shotgun   13: Panel A Black     13: Panel B Brown    13: Panel C Cardinal   14: NeckTrim Black   16: Flatlock Thread Color:Brown

我不希望任何人为我做任何事情,但一些指导会非常感激。我认为Pivot可能是我需要开始的地方,但我不太确定。当我看一下PIVOT语法时,我不确定从哪里开始。从第一个代码块可以看出,列总是以某些数字开头,就像结构总是以“10:”开头。数字后面的文字是变化的部分,我需要返回。一些列最终会变成“NULL”也不值得,因为并非一切都会有颈部修剪等。

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

如何使用一个或两个CTE frist准备数据,然后使用动态数据透视查询。

请参阅下面的链接,该链接可能对您的方案有用,或者至少可以为您提供线索。

Script to create dynamic pivot query