3

我在查询中有以下输出。

SKILL                                              LEVEL          SCORERANGE
-----------------------------------------------------------------------------
Stunts                                             LOW            0.0 - 4.0
Stunts                                             MED            3.0 - 7.0
Stunts                                             HI             6.0 - 10.0
Pyramids                                           LOW            0.0 - 4.0
Pyramids                                           MED            3.0 - 7.0
Pyramids                                           HI             6.0 - 10.0
Tosses                                             LOW            0.0 - 4.0
Tosses                                             MED            3.0 - 7.0
Tosses                                             HI             6.0 - 10.0
Standing Tumbling                                  LOW            0.0 - 4.0
Standing Tumbling                                  MED            3.0 - 7.0
Standing Tumbling                                  HI             6.0 - 10.0
Running Tumbling                                   LOW            0.0 - 4.0
Running Tumbling                                   MED            3.0 - 7.0
Running Tumbling                                   HI             6.0 - 10.0
Jumps                                              LOW            0.0 - 4.0
Jumps                                              MED            3.0 - 7.0

我想在不汇总任何内容的情况下对这些数据进行 PIVOT。所以我想要一个结果,每个技能只显示一行并以 LEVEL 为中心,像这样......

SKILL                                              LOWRANGE       MEDRANGE       HIRANGE 
Stunts                                             0.0 - 4.0      3.0 - 7.0      6.0 - 10.0
Pyramids                                           0.0 - 4.0      3.0 - 7.0      6.0 - 10.0
Tosses                                             0.0 - 4.0      3.0 - 7.0      6.0 - 10.0
Standing Tumbling                                  0.0 - 4.0      3.0 - 7.0      6.0 - 10.0
Running Tumbling                                   0.0 - 4.0      3.0 - 7.0      6.0 - 10.0
...

我确实对基本的数据透视语法有很好的理解,但我在这方面很挣扎,因为结果集中没有什么可以聚合的。

为了给你一些东西来尝试为我解决...

SELECT SKILL, LOWRANGE, MEDRANGE, HIRANGE
FROM (SELECT SKILL, [LEVEL], SCORERANGE FROM ScoreRanges) ps
PIVOT 
    (
        MAX(SCORERANGE)  --THIS IS PROBABLY WRONG
        FOR SCORERANGE IN
        (
           --SOMETHING GOES HERE
        )
    ) as pvt

谢谢你的帮助。

赛斯

4

2 回答 2

4

如您所知,每个SKILL, LEVEL组合只能使用1 行,Max或者Min

SELECT SKILL, [LOW] LOWRANGE, [MED] MEDRANGE, [HI] HIRANGE
FROM (SELECT SKILL, [LEVEL], SCORERANGE FROM ScoreRanges) ps
PIVOT (
        MAX(SCORERANGE) 
        FOR LEVEL IN ([LOW], [MED], [HI])
    ) as pvt
于 2010-09-30T00:08:10.340 回答
1

你几乎做对了

SELECT SKILL, LOW as LOWRANGE, MED as MEDRANGE, HI as HIRANGE
FROM (SELECT SKILL, [LEVEL], SCORERANGE FROM ScoreRanges) ps
PIVOT 
    (
        MAX(SCORERANGE)  
        FOR [LEVEL] IN
        (
           [LO], [HIGH], [MED]
        )
    ) as pvt

这是您可以在 Adventureworks 数据库中运行的示例,因此您也可以尝试使用它来查看其PIVOT工作原理

SELECT City, F AS Females, M AS Males
 FROM(
SELECT Gender,City
FROM HumanResources.Employee AS e
    INNER join HumanResources.EmployeeAddress AS ea
    ON e.EmployeeID = ea.EmployeeID
    INNER join Person.Address AS p
    ON ea.AddressID = p.AddressID
    ) AS pivTemp
PIVOT
(   COUNT(Gender)
    FOR Gender IN (F,M)
) AS pivTable

我这里还有另一个 PIVOT 示例:在 SQL Server 中使用配置值填充变量的三种不同方式

于 2010-09-30T00:10:36.777 回答