如何在不聚合列的情况下转动sql数据

时间:2010-09-30 00:02:16

标签: sql-server sql-server-2005 tsql pivot pivot-without-aggregate

我在查询中有以下输出。

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

感谢您的帮助。

赛斯

2 个答案:

答案 0 :(得分:4)

如您所知,每个SKILL, LEVEL组合只有1行,您可以使用MaxMin

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

答案 1 :(得分: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示例:Three different ways of populating variables with configuration values in SQL Server