4

I am having problem to fetch query in which i have a check user score in range to display the grade if user score between 75 and 100 then its A. If user score between 60- 75 then its B and .. so on .

I am getting this values

CASE users.points_earned
WHEN  75-100 THEN
    'A+'
WHEN  60-75 THEN
    'A'
WHEN  40-60 THEN
    'B+'
WHEN  1--40 THEN
    'B'
ELSE
    'Absent'
    end as rank 

buts not working how to check range in case statement of postgresql

1 Answer 1

5

You can use BETWEEN for check ranges.

WITH users(points_earned) as(
    select 75
    union all
    select 90
    union all
    select 200
)

SELECT CASE 
WHEN users.points_earned BETWEEN 40 AND 75 THEN 'A+'
WHEN users.points_earned BETWEEN 76 AND 100 THEN 'A'
ELSE 'Absent'
END as rank 
FROM users
Sign up to request clarification or add additional context in comments.

1 Comment

@FerozSiddiqui BTW because the case statement returns the first satisfied condition it could be simplified like case when users.points_earned < 40 then 'B' when users.points_earned < 60 then 'B+' ... end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.