如何将这个查询sql服务器连接在一起?

时间:2016-05-18 12:14:10

标签: sql sql-server join pivot coalesce

SQL FIDDLE DEMO HERE

我有Workers表的这个表结构:

    CREATE TABLE Workers
(
     [Name] varchar(250),
     [IdWorker] varchar(250),    
     [work] varchar(250)
);

INSERT INTO Workers ([Name],  [IdWorker],  [work])
values 
('Sam', '001', 'Director'),
('Julianne', '002', 'Recepcionist'),
('Jose', '003', 'Recepcionist');

我想要的是为每个工作找到工人的名字用逗号分隔,如下:

Director   Recepcionist
-------    ------------
Sam         Julianne, Jose

我尝试使用此查询:

 DECLARE @rec VARCHAR(MAX) 
 SELECT @rec = COALESCE(@rec + ', ', '') + Name from 
Workers where job = 'Recepcionist'  SELECT @dir AS Recepcionist

我得到了这个结果:

Recepcionist
------------
Julianne, Jose

这仅适用于一项工作,但我需要添加更多,所以我尝试使用此查询:

SELECT  [Director] , [Recepcionist] 
FROM 
(SELECT [job],  [Name],RANK() OVER (PARTITION BY [job] ORDER BY [job],[Name]) as rnk
 FROM Workers ) p
 PIVOT(
Min([Name])
FOR [job] IN
( [Director] , [Recepcionist] )
) AS pvt

我得到了这个结果:

Director  Recepcionist
--------  ------------
Sam       Julianne
          Jose

我需要用逗号分隔同一行中的结果,如何组合这两个查询? 我接受建议,谢谢。

3 个答案:

答案 0 :(得分:2)

我在您的示例查询中假设您的工作是引用工作列。以下查询应该按照你的sql小提琴来完成工作。

    SELECT  STUFF(
                    (
                        SELECT  ', ' + cast([Name] as varchar(max))
                        FROM    Workers
                        WHERE   [work] = 'Recepcionist'
                        FOR XML PATH('')
                    ), 1, 2, ''
            ) AS Recepcionist
            ,STUFF(
                (
                    SELECT  ', ' + cast([Name] as varchar(max))
                    FROM    Workers
                    WHERE   [work] = 'Director'
                    FOR XML PATH('')
                ), 1, 2, '') AS Director;

答案 1 :(得分:1)

使用PIVOT的方式就是这样。

SELECT  * 
FROM    (SELECT [work],
                STUFF((SELECT   ', ' + [Name]
                        FROM    Workers s
                        WHERE   s.WORK = w.WORK
                        FOR XML PATH('')),
                        1, 2, '') AS [workers]              
         FROM   Workers w) t
PIVOT (
    MAX([workers])
    FOR [work] IN ([Director], [Recepcionist])
) p

PIVOT的另一种选择是MAX(CASE)

SELECT  MAX(CASE WHEN [work] = 'Director' THEN [workers] END) AS [Director], 
        MAX(CASE WHEN [work] = 'Recepcionist' THEN [workers] END) AS [Recepcionist]
FROM    (SELECT [work],
                STUFF((SELECT   ', ' + [Name]
                        FROM    Workers s
                        WHERE   s.WORK = w.WORK
                        FOR XML PATH('')),
                        1, 2, '') AS [workers]              
         FROM   Workers w) t

这两个允许您通过公司或部门等其他字段分隔数据

答案 2 :(得分:0)

你可以这样做

SELECT
    t1.job
        ,STUFF(
                   (SELECT
                        ', ' + t2.Name
                        FROM Workers t2
                        WHERE t1.job =t2.job 
                        ORDER BY t2.Name
                        FOR XML PATH(''), TYPE
                   ).value('.','varchar(max)')
                   ,1,2, ''
              ) AS ChildValues
    FROM Workers t1
    GROUP BY t1.job