您可以使用Sql PIVOT查询在两列上进行透视和聚合

时间:2011-10-12 00:49:59

标签: sql sql-server tsql pivot

可以在数据透视查询中透视两列。

假设我有以下数据......

CREATE TABLE JudgeScores 
    (PerformanceID int, 
    JudgeID int, 
    Criteria varchar(10), 
    StrengthScore decimal(9,4), 
    StyleScore decimal(9,4))

--first team performance
--judge1
INSERT INTO JudgeScores (1, 1, "Stunts",        4.2, 1.1)
INSERT INTO JudgeScores (1, 1, "Jumps",         3.9, 0.8)
INSERT INTO JudgeScores (1, 1, "Tumbling",      4.5, 1.0)
INSERT INTO JudgeScores (1, 1, "Choreography",  4.2, 1.5)
--judge2
INSERT INTO JudgeScores (1, 2, "Stunts",        4.1, 1.1)
INSERT INTO JudgeScores (1, 2, "Jumps",         4.0, 0.9)
INSERT INTO JudgeScores (1, 2, "Tumbling",      4.4, 1.1)
INSERT INTO JudgeScores (1, 2, "Choreography",  4.2, 1.6)

--judge3
INSERT INTO JudgeScores (1, 3, "Stunts",        3.8, 1.2)
INSERT INTO JudgeScores (1, 3, "Jumps",         4.2, 0.7)
INSERT INTO JudgeScores (1, 3, "Tumbling",      4.3, 1.2)
INSERT INTO JudgeScores (1, 3, "Choreography",  4.1, 1.3)


--second team performance
--judge1
INSERT INTO JudgeScores (2, 1, "Stunts",        4.3, 1.3)
INSERT INTO JudgeScores (2, 1, "Jumps",         4.0, 0.9)
INSERT INTO JudgeScores (2, 1, "Tumbling",      4.6, 1.1)
INSERT INTO JudgeScores (2, 1, "Choreography",  4.0, 1.0)
--judge2
INSERT INTO JudgeScores (2, 2, "Stunts",        4.1, 1.1)
INSERT INTO JudgeScores (2, 2, "Jumps",         4.0, 0.9)
INSERT INTO JudgeScores (2, 2, "Tumbling",      4.5, 1.2)
INSERT INTO JudgeScores (2, 2, "Choreography",  4.2, 1.6)

--judge3
INSERT INTO JudgeScores (2, 3, "Stunts",        4.1, 1.1)
INSERT INTO JudgeScores (2, 3, "Jumps",         4.5, 0.9)
INSERT INTO JudgeScores (2, 3, "Tumbling",      4.4, 1.2)
INSERT INTO JudgeScores (2, 3, "Choreography",  4.2, 1.6)

我想选择数据,以便按以下方式调整此数据

PerformanceID, JudgeID, StuntsStrength, StuntsStyle, JumpsStrength, JumpsStyle, TumbleStrength, TumbleStyle, ChorStrength, ChorStyle
1     1     4.2,    1.1,    3.9,    0.8,   4.5,   1.0,    4.2,     1.5
1     2     4.1,    1.1,    4.0,    0.9,   4.4,   1.1,    4.2,     1.6
...
2     1     4.3,    1.3,    4.0,    0.9,   4.6,   1.1,    4.0,    1.0
2     2     4.1,    1.1,    4.0,    0.9,   4.5,   1.2,    4.2,    1.6
2     3     ...

可以使用数据透视查询完成此操作。如果不是最好的方法是什么?

2 个答案:

答案 0 :(得分:4)

IMO这比枢轴等效物更具可读性。

保持简单和可维护性:

 select PerformanceId,
        JudgeId,
        [StuntsStrength] = avg(case when Criteria = 'Stunts' then StrengthScore else null end),
        [StuntsStyle] = avg(case when Criteria = 'Stunts' then StyleScore else null end),
        [JumpsStrength] = avg(case when Criteria = 'Jumps' then StrengthScore else null end),
        [JumpsStyle] = avg(case when Criteria = 'Jumps' then StyleScore else null end),
        [TumbleStrength] = avg(case when Criteria = 'Tumbling' then StrengthScore else null end),
        [TumbleStyle] = avg(case when Criteria = 'Tumbling' then StyleScore else null end),
        [ChorStrength] = avg(case when Criteria = 'Choreography' then StrengthScore else null end),
        [ChorStyle] = avg(case when Criteria = 'Choreography' then StyleScore else null end)
from    dbo.JudgeScores
group
by      PerformanceId, JudgeId;

答案 1 :(得分:1)

您不需要像动态sql或pivot运算符那样花哨来执行此查询。

SELECT j.PerformanceID, j.JudgeID, 
    StuntsStrengthScore = (SELECT StrengthScore FROM JudgeScores WHERE PerformanceID = j.PerformanceID and JudgeID = j.JudgeID and Criteria = 'Stunts'),
    StuntsStyleScore = (SELECT StyleScore FROM JudgeScores WHERE PerformanceID = j.PerformanceID and JudgeID = j.JudgeID and Criteria = 'Stunts'),
    JumpsStrengthScore = (SELECT StrengthScore FROM JudgeScores WHERE PerformanceID = j.PerformanceID and JudgeID = j.JudgeID and Criteria = 'Jumps'),
    JumpsStyleScore = (SELECT StyleScore FROM JudgeScores WHERE PerformanceID = j.PerformanceID and JudgeID = j.JudgeID and Criteria = 'Jumps'),
    TumblingStrengthScore = (SELECT StrengthScore FROM JudgeScores WHERE PerformanceID = j.PerformanceID and JudgeID = j.JudgeID and Criteria = 'Tumbling'),
    TumblingStyleScore = (SELECT StyleScore FROM JudgeScores WHERE PerformanceID = j.PerformanceID and JudgeID = j.JudgeID and Criteria = 'Tumbling'),
    ChoreographyStrengthScore = (SELECT StrengthScore FROM JudgeScores WHERE PerformanceID = j.PerformanceID and JudgeID = j.JudgeID and Criteria = 'Choreography'),
    ChoreographyStyleScore = (SELECT StyleScore FROM JudgeScores WHERE PerformanceID = j.PerformanceID and JudgeID = j.JudgeID and Criteria = 'Choreography')
FROM JudgeScores j
ORDER BY PerformanceID, JudgeID