Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2008 SQL Server中的简单查询复制_Sql Server 2008 - Fatal编程技术网

Sql server 2008 SQL Server中的简单查询复制

Sql server 2008 SQL Server中的简单查询复制,sql-server-2008,Sql Server 2008,我需要根据以下要求筛选查询 我的问题是: select * from tbltemp 电流输出: Caterogy SeqCategory DescofChange RequestId TaskCompVer ----------------------------------------------------------------------------- BIGBEAR BIGBEAR BIGBEAR B14020002

我需要根据以下要求筛选查询

我的问题是:

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查询,以便它生成实际输出

我尝试使用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”附近的语法不正确

您可以使用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
此处演示:

编辑 除此之外,还可以交叉应用:


演示:

首先,为什么会出现错误

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

FROM 
 tbltemp temp1
         ^here
2.表tbltemp中没有列名为WishItemPE的列。3号线

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

STUFF((SELECT ', ' + temp2.WishItemPE
最终查询


请参阅sry..我正在使用旧的sqlserver数据库…那是sry吗?东西不起作用..谢谢你的问题用SQL SERVER 2008标记,东西在这个版本中..我正在使用SQL 2008,但我正在连接到一个较旧的版本y?东西坏了。。
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