1

我正在使用 CTE 查询来记录一列(PO_list)下的 HeaderKey 用逗号分隔的采购订单编号列表,如下所示:

HeaderKey       PO_list
1000215         101142-00-36, 101820-00-37, 103643-00-37, 104120-00-37, 104530-00-37,
                105095-00-37, 105700-00-37, 5062 SD, 99273-00-37, FIAT SAMPLES, S.RAPSON
                1000219 102288, 104545
1000220         104321-00-3, 104321-00-4, 104321-00-5, 105715-00-4, 105715-00-5, 105715-003

当采购订单列表在 Freightcontents 表中为一个 headerkey 列出超过 15 个条目时,我遇到了吞吐量时间过长的问题。我希望有人能解决这个问题。

表定义如下

/****** Object: Table [dbo].[FreightContents] Script Date: 02/19/2013 16:21:38 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

Create TABLE [dbo].[FreightContents](

[HeaderKey] [char](35) NOT NULL,

[PurchaseOrderNumber] [char](30) NOT NULL,

) ON [PRIMARY] 

GO

SET ANSI_PADDING OFF
GO

INSERT INTO FreightContents VALUES('FJ#1000210','104001-00-3') 
INSERT INTO FreightContents VALUES('FJ#1000211','SAMPLE' ) 
INSERT INTO FreightContents VALUES('FJ#1000211','Hydraulic Cylinder' )
INSERT INTO FreightContents VALUES('FJ#1000211','99148/10182' )
INSERT INTO FreightContents VALUES('FJ#1000211','99148/101182' )
INSERT INTO FreightContents VALUES('FJ#1000211','99148' )
INSERT INTO FreightContents VALUES('FJ#1000211','99148' )
INSERT INTO FreightContents VALUES('FJ#1000211','101914' )
INSERT INTO FreightContents VALUES('FJ#1000211','101913' )
INSERT INTO FreightContents VALUES('FJ#1000211','101914' ) 
INSERT INTO FreightContents VALUES('FJ#1000211','101185' ) 
INSERT INTO FreightContents VALUES('FJ#1000211','101186' )
INSERT INTO FreightContents VALUES('FJ#1000211','101187' )
INSERT INTO FreightContents VALUES('FJ#1000211','101188' )
INSERT INTO FreightContents VALUES('FJ#1000211','101189' )
INSERT INTO FreightContents VALUES('FJ#1000211','101110' )
INSERT INTO FreightContents VALUES('FJ#1000211','101111' )
INSERT INTO FreightContents VALUES('FJ#1000211','101112' )
INSERT INTO FreightContents VALUES('FJ#1000211','101113' )
INSERT INTO FreightContents VALUES('FJ#1000211','101181' )
INSERT INTO FreightContents VALUES('FJ#1000211','101182')
INSERT INTO FreightContents VALUES('FJ#1000211','101914' ) 
INSERT INTO FreightContents VALUES('FJ#1000211','101914' ) 
INSERT INTO FreightContents VALUES('FJ#1000211','101914' ) 
INSERT INTO FreightContents VALUES('FJ#1000211','101185' ) 
INSERT INTO FreightContents VALUES('FJ#1000211','101182' ) 
INSERT INTO FreightContents VALUES('FJ#1000211','101182' ) 
INSERT INTO FreightContents VALUES('FJ#1000211','101182' ) 
INSERT INTO FreightContents VALUES('FJ#1000211','101186' ) 
INSERT INTO FreightContents VALUES('FJ#1000211','101182' ) 
INSERT INTO FreightContents VALUES('FJ#1000211','101187' ) 
INSERT INTO FreightContents VALUES('FJ#1000211','101186' ) 
INSERT INTO FreightContents VALUES('FJ#1000211','101185' ) 
INSERT INTO FreightContents VALUES('FJ#1000211','101184' ) 
INSERT INTO FreightContents VALUES('FJ#1000211','101183' )

CTE 查询如下:

/****** Object: View [dbo].[CTE_POList_UK] Script Date: 02/19/2013 16:26:23 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE VIEW [dbo].[CTE_POList_UK] as
WITH CTE ( HeaderKey, PO_list, product_name, length )
AS ( SELECT HeaderKey, CAST( '' AS VARCHAR(8000) ), CAST( '' AS VARCHAR(8000) ), 0
FROM Database1..FreightContents with(readuncommitted)
GROUP BY HeaderKey
UNION ALL
SELECT p.HeaderKey, CAST( PO_list +
CASE WHEN length = 0 THEN '' ELSE ', ' END + rtrim(PurchaseOrderNumber) AS VARCHAR(8000) ),
CAST( PurchaseOrderNumber AS VARCHAR(8000)), length + 1
FROM CTE c
INNER JOIN Database1..FreightContents p with(readuncommitted)
ON c.HeaderKey = p.HeaderKey
WHERE p.PurchaseOrderNumber > c.product_name )
SELECT HeaderKey, PO_list
FROM ( SELECT Distinct substring(HeaderKey,4,10), PO_list,
RANK() OVER ( PARTITION BY HeaderKey ORDER BY length DESC )
FROM CTE ) D ( HeaderKey, PO_list, rank )
WHERE rank = 1 ;

GO

创建后,它会通过右键单击对象资源管理器中的查询来显示结果。

4

2 回答 2

1

试试这个:

CREATE VIEW [dbo].[CTE_POList_UK2] 
AS
SELECT  DISTINCT 
        RIGHT(RTRIM(fc.HeaderKey), 7) HeaderKey
        ,
        STUFF(
        (   SELECT  ', ' + RTRIM(fc2.PurchaseOrderNumber)
            FROM    FreightContents fc2
            WHERE   fc2.HeaderKey = fc.HeaderKey
            FOR XML PATH(''), TYPE
        ).value('.', 'VARCHAR(MAX)')
        , 1, 2, '') PO_list
FROM    FreightContents fc

我尝试运行您建议的 CTE,但无法完成。这可以得到你需要的列表。

于 2013-02-19T19:05:55.923 回答
0

尝试本文中的一种方法,尤其是“黑盒 XML”方法:

WITH cte (HeaderKey, PO_list)
As
(
   SELECT
      H.HeaderKey,
      (SELECT ', ' + RTrim(PurchaseOrderNumber)
      FROM Database1.dbo.FreightContents As P
      WHERE P.HeaderKey = H.HeaderKey
      FOR XML PATH(''), TYPE).value('.', 'varchar(max)')
   FROM
      Database1.dbo.FreightContents As H
   GROUP BY
      H.HeaderKey
)
SELECT
   Substring(HeaderKey, 4, 10) As HeaderKey,
   Stuff(PO_list, 1, 2, '') As PO_list
FROM
   cte
;

SQL小提琴

于 2013-02-19T18:57:57.250 回答