tsql连接多个表并旋转特定列

时间:2014-08-12 16:58:35

标签: sql-server tsql pivot case pivot-table

我的数据如下: -

Color表: -

 Id | Red | Green | Blue | PersonId | MonthId
  1   22      53      4     1         1
  2   52     213    100     3         2
  3    2      32     44     2         3
  4   72     143      4     1         2  ......

Month表: -

   Id | Name
   1    January  
   2    February 
   3    March    
   4    April    
   5    May    ....

Person表: -

 Id | Name
  1   Greg  
  2   John  
  3   Gorge ...

我需要制作: -

 Name | January | February | March | April | May
 Greg   22
 Greg   53
 Greg   4
 Greg              72 ....
 John ....

我尝试了以下案例陈述,但我无法获得多种颜色

select p.Name,
       (case when m.Name = 'January' then Red) January
       from Person p, Month m, Color c where c.PersonId = p.Id and c.MonthId = m.Id

我需要在所有月份获得Red, Green and Blue

枢轴似乎有点太难了。如果有人有任何建议,我很乐意尝试一下。

数据表示可能不是100%正确,我想出了它来说明我的观点。

1 个答案:

答案 0 :(得分:1)

你不能使用PIVOT因为你需要聚合颜色值(红色,蓝色,绿色列值)这里使用PIVOT的一个例子是:

WITH Color_Values (ColorValue, PersonId, MonthId)
AS
(
    SELECT
        Red,
        PersonId,
        MonthId
    FROM Color
    UNION ALL
    SELECT
        Green,
        PersonId,
        MonthId
    FROM Color
    UNION ALL
    SELECT
        Blue,
        PersonId,
        MonthId
    FROM Color
),
Data (ColorValue, [Month], Person)
AS
(
    SELECT
        C.ColorValue,
        M.Id AS [Month],
        P.Name AS Person
    FROM [Month] AS M
    RIGHT OUTER JOIN Color_Values AS C
        ON M.Id = C.MonthId
    LEFT OUTER JOIN Person AS P
        ON P.Id = C.PersonID
)
SELECT
    Person AS Name,
    [1] AS Jan,
    [2] AS Feb,
    [3] AS Mar,
    [4] AS Apr,
    [5] AS May
FROM 
(
    SELECT ColorValue, [Month], Person
    FROM Data
) p
PIVOT
(
    COUNT (ColorValue)
    FOR [Month] IN
    ( [1], [2], [3], [4], [5] )
) AS pvt
ORDER BY
    pvt.Person;

但是既然你想要显示每个值(1月份Greg的3条记录而不是1条记录)你需要更像这样的东西:

SELECT
    P.Name,
    CASE C.MonthId WHEN 1 THEN C.Red END AS January,
    CASE C.MonthId WHEN 2 THEN C.Red END AS February,
    CASE C.MonthId WHEN 3 THEN C.Red END AS March,
    CASE C.MonthId WHEN 4 THEN C.Red END AS April,
    CASE C.MonthId WHEN 5 THEN C.Red END AS May
FROM Color AS C
INNER JOIN Person AS P
    ON P.Id = C.PersonId

UNION ALL

SELECT
    P.Name,
    CASE C.MonthId WHEN 1 THEN C.Blue END AS January,
    CASE C.MonthId WHEN 2 THEN C.Blue END AS February,
    CASE C.MonthId WHEN 3 THEN C.Blue END AS March,
    CASE C.MonthId WHEN 4 THEN C.Blue END AS April,
    CASE C.MonthId WHEN 5 THEN C.Blue END AS May
FROM Color AS C
INNER JOIN Person AS P
    ON P.Id = C.PersonId

UNION ALL

SELECT
    P.Name,
    CASE C.MonthId WHEN 1 THEN C.Green END AS January,
    CASE C.MonthId WHEN 2 THEN C.Green END AS February,
    CASE C.MonthId WHEN 3 THEN C.Green END AS March,
    CASE C.MonthId WHEN 4 THEN C.Green END AS April,
    CASE C.MonthId WHEN 5 THEN C.Green END AS May
FROM Color AS C
INNER JOIN Person AS P
    ON P.Id = C.PersonId

ORDER BY
    Name,
    January DESC,
    February DESC,
    March DESC,
    April DESC,
    May DESC