2014-02-19 52 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