如何在sql server中使用pivot(没有聚合)?

时间:2015-10-08 13:35:48

标签: sql sql-server database

请帮我解决这个问题: 您将获得一个包含两列的表: 列是以下之一:

Doctor
Professor
Singer
Actor

编写查询以输出相应occ下面的名称。采用以下格式:

+--------+-----------+--------+------+

| Doctor | Professor | Singer | Actor|

+--------+-----------+--------+------+

名称必须按字母顺序排列。

示例输入

Name        Occupation
Meera       Singer
Ashely      Professor
Ketty       Professor
Christeen   Professor
Jane        Actor
Jenny       Doctor
Priya       Singer    

示例输出

Jenny    Ashley     Meera  Jane

Samantha Christeen  Priya  Julia

NULL     Ketty      NULL   Maria

请注意

当没有更多名称对应职业时,打印“NULL”。

我尝试使用:

SELECT *
FROM
(
SELECT [Name], [Occupation] 
FROM occupations 
) AS source
PIVOT
(
    max([Name])
    FOR [occupation] IN ([Doctor], [Professor], [Singer], [Actor]) 
) as pvt;

给出以下输出:

Priya Priyanka Kristeen Samantha 

如何解决?

5 个答案:

答案 0 :(得分:12)

您只需要根据字体的职业和顺序为每个名称添加一个行号。然后在您的透视查询中包含该行号。

CREATE TABLE Occupations (
     NAME VARCHAR(MAX),
     Occupation VARCHAR(MAX)
    )
INSERT  INTO Occupations
VALUES
        ('Samantha','Doctor'),
        ('Julia','Actor'),
        ('Maria','Actor'),
        ('Meera','Singer'),
        ('Ashley','Professor'),
        ('Ketty','Professor'),
        ('Christeen','Professor'),
        ('Jane','Actor'),
        ('Jenny','Doctor'),
        ('Priya','Singer');

SELECT
    [Doctor],
    [Professor],
    [Singer],
    [Actor]
FROM
    (SELECT 
         ROW_NUMBER() OVER (PARTITION BY Occupation ORDER BY Name) rn,
         [Name],
         [Occupation] 
     FROM 
         Occupations
    ) AS source 
PIVOT
    ( MAX([Name]) FOR [occupation] IN ([Doctor],[Professor],[Singer],[Actor]) ) as pvt
ORDER BY rn


DROP TABLE Occupations

答案 1 :(得分:1)

我在 Oracle 中尝试过此操作,似乎更容易理解:

SELECT min(Doctor), min(Professor), min(Singer), min(Actor)
FROM
( Select
ROW_NUMBER() OVER (PARTITION BY Occupation order by Name) rn, 
CASE 
WHEN Occupation = 'Doctor' then Name
end as Doctor,
CASE 
WHEN Occupation = 'Professor' then Name
end as Professor,
CASE 
WHEN Occupation = 'Singer' then Name
end as Singer,
CASE 
WHEN Occupation = 'Actor' then Name
end as Actor
from OCCUPATIONS
order by Name) a
group by rn
order by rn;

答案 2 :(得分:1)

这是答案的MYSQL版本。有点困难,因为HACKERRANK.com中没有FULL OUTER JOIN。

  x  y  z  a
  1  2  2  2
  2  1 NA  2
  1  3  3  3
 NA  1  2  2
 NA NA NA NA

答案 3 :(得分:0)

SET @r1=0, @r2=0, @r3 =0, @r4=0;
SELECT MIN(Doctor), MIN(Professor), MIN(Singer), MIN(Actor) FROM
(SELECT CASE Occupation WHEN 'Doctor' THEN @r1:=@r1+1
                       WHEN 'Professor' THEN @r2:=@r2+1
                       WHEN 'Singer' THEN @r3:=@r3+1
                       WHEN 'Actor' THEN @r4:=@r4+1 END
       AS RowLine,
       CASE WHEN Occupation = 'Doctor' THEN Name END AS Doctor,
       CASE WHEN Occupation = 'Professor' THEN Name END AS Professor,
       CASE WHEN Occupation = 'Singer' THEN Name END AS Singer,
       CASE WHEN Occupation = 'Actor' THEN Name END AS Actor
       FROM OCCUPATIONS ORDER BY Name) AS t
GROUP BY RowLine;

答案 4 :(得分:0)

SELECT [Doctor], [Professor], [Singer], [Actor] FROM   
(
    SELECT ROW_NUMBER() OVER (PARTITION BY Occupation ORDER BY Name) ROW_NO, 
    ISNULL(NULL,Name) as Name, Occupation
    FROM Occupations
) AS t 
PIVOT(
    MAX(Name)
    FOR Occupation IN (
        [Doctor], 
        [Professor], 
        [Singer], 
        [Actor]
    )
) AS pivot_table
ORDER BY ROW_NO;