Home > Back-end >  Is there any way to group by credit score ranges in SQL?
Is there any way to group by credit score ranges in SQL?

Time:11-19

I have a table that has a column with a list of credit scores. I'd like to group them into categories of 'Very Poor', 'Poor', 'Fair' etc. But is there a way to count the number of occurrences in each of those categories? That's where I'm getting stuck. So far here's what I have, not sure where to add the COUNT or GROUP BY functions.

SELECT CreditScore, Geography, 
CASE WHEN CreditScore BETWEEN 300 AND 500 THEN 'Very Poor' 
WHEN CreditScore BETWEEN 501 AND 600 THEN 'Poor'
WHEN CreditScore BETWEEN 601 AND 700 THEN 'Fair'
WHEN CreditScore BETWEEN 601 AND 700 THEN 'Fair'
WHEN CreditScore BETWEEN 701 AND 750 THEN 'Good'
WHEN CreditScore BETWEEN 751 AND 850 THEN 'Great'
END AS SCORETEXT FROM customerchurn

CodePudding user response:

you could use the query resul as a table

select SCORETEXT, count(*) 
from (
    SELECT CreditScore, Geography, 
    CASE WHEN CreditScore BETWEEN 300 AND 500 THEN 'Very Poor' 
    WHEN CreditScore BETWEEN 501 AND 600 THEN 'Poor'
    WHEN CreditScore BETWEEN 601 AND 700 THEN 'Fair'
    WHEN CreditScore BETWEEN 601 AND 700 THEN 'Fair'
    WHEN CreditScore BETWEEN 701 AND 750 THEN 'Good'
    WHEN CreditScore BETWEEN 751 AND 850 THEN 'Great'
    END AS SCORETEXT FROM customerchurn
) a 
group by SCORETEXT
  •  Tags:  
  • sql
  • Related