简单的SQL Server中的查询重复

时间:2014-02-19 08:35:13

标签: sql-server-2008

我需要根据以下要求过滤查询..

我的查询:

select * from tbltemp

当前输出:

Caterogy   SeqCategory   DescofChange    RequestId     TaskCompVer
-----------------------------------------------------------------------------    
BIGBEAR    BIGBEAR       BIGBEAR         B14020002     Provide ASPM Wish List
ARCUS3PL   KOJN-RE       ARCUS3PL        B14020002     Provide ASPM Wish List
AURORA     Aurora        Aurora          B14020003     Provide ASPM Wish List

期望的输出:

Caterogy           SeqCategory       DescofChange      RequestId   TaskCompVer
---------------------------------------------------------------------------------------    
BIGBEAR,ARCUS3PL   BIGBEAR,KOJN-RE   BIGBEAR,ARCUS3PL  B14020002   Provide ASPM Wish List
AURORA             Aurora            Aurora            B14020003   Provide ASPM Wish List

我如何以生成实际输出的方式过滤上面的选择查询..

我尝试使用STUFF,但它抛出了语法错误:

SELECT
    RequestId, 
    STUFF((SELECT ', ' + temp2.WishItemPE
           FROM tbltemp temp2  
           WHERE temp2.TaskCompVer = temp1.TaskCompVer 
             AND temp2.RequestId = temp1.RequestId        
           FOR XML PATH('')), 1, 1, '') AS WishItemPE  
FROM
    tbltemp

错误:

  

' XML'

附近的语法不正确

2 个答案:

答案 0 :(得分:0)

您可以使用stuff功能:

select distinct stuff(
  ( select  cast(',' as varchar(max)) + t1.Category
    from temp t1
    WHERE t1.RequestID = t.RequestID
    order by t1.Category
    for xml path('')
    ), 1, 1, '') as Category,

 stuff(
  ( select  cast(',' as varchar(max)) + t1.SeqCategory
    from temp t1
    WHERE t1.RequestID = t.RequestID
    order by t1.SeqCategory
    for xml path('')
    ), 1, 1, '') as SeqCategory,
 stuff(
  ( select  cast(',' as varchar(max)) + t1.DescofChange
    from temp t1
    WHERE t1.RequestID = t.RequestID
    order by t1.DescofChange
    for xml path('')
    ), 1, 1, '') as DescofChange, 
  RequestID, 
 stuff(
  ( select  Distinct cast(',' as varchar(max)) + t1.TaskProvider
    from temp t1
    WHERE t1.RequestID = t.RequestID

    for xml path('')
    ), 1, 1, '') as TaskProvider
from temp t

演示:http://sqlfiddle.com/#!3/f1789/9


修改 替代方案可以是CROSS APPLY

SELECT  Categories, SeqCategories, DescofChanges, RequestID, TaskProvider
FROM temp as A
Cross Apply
(
 SELECT Category + ',' 
 FROM temp AS B 
 WHERE A.RequestID = B.RequestID FOR XML PATH('')
)D (Categories)
Cross Apply
(
 SELECT SeqCategory + ',' 
 FROM temp AS B 
 WHERE A.RequestID = B.RequestID FOR XML PATH('')
)E (SeqCategories)
Cross Apply
(
 SELECT DescofChange + ',' 
 FROM temp AS B 
 WHERE A.RequestID = B.RequestID FOR XML PATH('')
)F (DescofChanges)

GROUP BY RequestID,  Categories, SeqCategories, DescofChanges, TaskProvider

演示:http://sqlfiddle.com/#!3/f1789/56

答案 1 :(得分:0)

首先,为什么是错误,

1.您没有为基表指定表别名。你的最后一行查询。

FROM 
 tbltemp temp1
         ^here

2.表tbltemp中没有列名为WishItemPE的列。行号3。

最后,你必须使用下面的Query来获得所需的输出。

STUFF((SELECT ', ' + temp2.WishItemPE

最终查询

SELECT distinct
    STUFF((SELECT ', ' + temp2.[Caterogy]
           FROM tbltemp temp2  
           WHERE temp2.TaskCompVer = temp1.TaskCompVer 
             AND temp2.RequestId = temp1.RequestId     
           FOR XML PATH('')), 1, 1, '') AS [Caterogy],
    RequestId, 
    STUFF((SELECT ', ' + temp2.[SeqCategory]
           FROM tbltemp temp2  
           WHERE temp2.TaskCompVer = temp1.TaskCompVer 
             AND temp2.RequestId = temp1.RequestId     
           FOR XML PATH('')), 1, 1, '') AS [SeqCategory],
    STUFF((SELECT ', ' + temp2.[DescofChange]
           FROM tbltemp temp2  
           WHERE temp2.TaskCompVer = temp1.TaskCompVer 
             AND temp2.RequestId = temp1.RequestId     
           FOR XML PATH('')), 1, 1, '') AS [DescofChange],
    [TaskCompVer]
FROM
    tbltemp as temp1

<强> SQL Fiddle