2

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

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 语法时,我不确定从哪里开始。从第一段代码可以看出,列总是以特定的数字开头,就像 fabric 总是以“10:”开头。数字后面的文本是改变的部分,我需要返回。某些列最终将成为“NULL”也是毫无价值的,因为并非所有内容都会有颈部修剪等。

提前感谢您的帮助。

4

1 回答 1

0

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

请查看下面的链接,这可能对您的场景有用,或者至少可以为您提供线索。

创建动态数据透视查询的脚本

于 2013-11-29T08:39:21.940 回答