如何在不聚合列的情况下透视 SQL 数据

发布于 2024-09-25 15:30:04 字数 2534 浏览 3 评论 0原文

我在查询中有以下输出。

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

我想在不聚合任何内容的情况下对这些数据进行透视。所以我想要一个结果,该结果只显示每项技能的一行并旋转级别,就像这样...

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

感谢你的帮助。

赛斯

I have the following output in a query.

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

I want to PIVOT this data without aggregating anything. So I want a result that shows only one row for each skill and pivots the LEVEL, something like this...

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
...

I do have a pretty good understanding of basic pivot syntax but I am struggling with this one because there is nothing to aggregate in the result set.

To give you something to try to fix for me...

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

Thanks for your help.

Seth

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别想她 2024-10-02 15:30:04

如您所知,每个 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

As you know you will only have 1 row per SKILL, LEVEL combination you can use Max or 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
落花随流水 2024-10-02 15:30:04

您几乎猜对了

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 中使用配置值填充变量的三种不同方法

You almost got it right

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

Here is an example you can run in the Adventureworks database, so you can play around with that also to see how PIVOT works

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

I also have nother PIVOT example here: Three different ways of populating variables with configuration values in SQL Server

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文