2014-02-19 135 views
0

我需要过滤根据以下要求查询..简单的查询复制在SQL Server

我的查询:

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 

我是如何来筛选这样的方式在上述选择查询它所产生的实际输出..

我尝试使用的东西,但它抛出语法错误:

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 

错误:

Incorrect syntax near 'XML'

+0

看到http://stackoverflow.com/questions/451415/simulating-group-concat-mysql-function-in-microsoft -sql-server-2005 –

回答

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 

demo here: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

+0

sry ..我使用旧的sqlserver数据库...这是Y? STUFF不起作用..谢谢 – Kapil

+0

你的问题被标记为'SQL SERVER 2008'和'STUFF'在这个版本中... – Milen

+0

我正在使用SQL 2008但是我连接到更老的那个是y?东西不工作.. – Kapil

0

首先,为什么出错,

1.You没有指定基表的表别名。查询中的最后一行。

FROM 
tbltemp temp1 
     ^here 

2.有表tbltemp与列名WishItemPE没有列。线路号码3.

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

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