92twinturboz
92twinturboz

Reputation: 23

Group by Range SQL Server

UPDATE!!!!

I have the following SQL query built with its respective output:

enter image description here

enter image description here

I would like to effectively add labels to each of the aggregations like below:

coupon percent   |   income level
-----------------------------------
 .023434355      |   0-20000
 .054888999      |   20000-35000
 .010000002      |   35000-100000

Any suggestions?

Upvotes: 2

Views: 103

Answers (1)

Chris Stillwell
Chris Stillwell

Reputation: 10547

You can add a CASE statement to your GROUP BY something like

GROUP BY CASE 
    WHEN storeAvgIncome <= 20000 THEN '0-20000' 
    WHEN storeAvgIncome > 20000 AND storeAvgIncome <=  35000 THEN '20000-35000' 
    WHEN storeAvgIncome > 35000 AND storeAvgIncome <= 100000 THEN '35000-100000'
    END

Then add the same to your SELECT

SELECT 
    CASE 
        WHEN storeAvgIncome <= 20000 THEN '0-20000' 
        WHEN storeAvgIncome > 20000 AND storeAvgIncome <=  35000 THEN '20000-35000' 
        WHEN storeAvgIncome > 35000 AND storeAvgIncome <= 100000 THEN '35000-100000'
        END AS [income level]

Upvotes: 3

Related Questions