Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do you pivot sql data without aggregating a column

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

like image 342
Seth Spearman Avatar asked Mar 02 '26 16:03

Seth Spearman


2 Answers

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
like image 151
Martin Smith Avatar answered Mar 05 '26 08:03

Martin Smith


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

like image 21
SQLMenace Avatar answered Mar 05 '26 09:03

SQLMenace



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!