Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Need help in Select case with tricky logic


Need help in Select case with tricky logic

Author
Message
born2achieve
born2achieve
SSC Veteran
SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)

Group: General Forum Members
Points: 230 Visits: 474
hi,

Having trouble some using select case for the below logic.


with sample as (

select 6 as Items,0 as score union all
select 2 as Items,30 as score union all
select 10 as Items,50 as score union all
select 12 as Items,75 as score )

select * from sample;



i want to segregate and sum the value as three category


score >= 0 and <40
score >= 40 and <70
score >=70



[on the sample data i provided, two items meets my first condition so sum the value and displayed as 6]

Expected result:

 
With Result as (
select 8 as poor, 10 as average,12 as Good)

select * from result



Any sample query please. bit confused about using select case for this category
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8556 Visits: 18143
That's not difficult, what have you tried?


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
born2achieve
born2achieve
SSC Veteran
SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)

Group: General Forum Members
Points: 230 Visits: 474
Hi Luis,

How are you? Since long time keeping your foot print on my post. Appreciate your time on this. Some time mind doesn't work to write easy logic.

this is how i implemented.

select   sum(case when score >= 0 and score < 40 then Items end) as poor,
sum(case when score >= 40 and score < 70 then Items end) as average,
sum(case when score >= 70 then Items end) as Good
from sample



Thanks for your precious time on this post.
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8556 Visits: 18143
That seems to give the correct result. What's the problem? You could add an ELSE 0 to avoid having NULLs in your data.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
born2achieve
born2achieve
SSC Veteran
SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)

Group: General Forum Members
Points: 230 Visits: 474
Yes it gives proper result. that's why i marked this a resolved already

Thank you for your time Luis.
Avi1
Avi1
SSC-Enthusiastic
SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)

Group: General Forum Members
Points: 138 Visits: 67
I know this is resolved, still....
just a suggetion use a look up table or create another cte for all grades.

Then join this look up table with your main cte (or table) where score in main table is between score range in the grade lookup. this will give you more felxibility.

with sample as (

select 6 as Items,0 as score union all
select 2 as Items,30 as score union all
select 10 as Items,50 as score union all
select 12 as Items,75 as score )
,
LkpGrade as (
select 'Grade1' as Grade,0 as MinScore, 39 as MaxScore union all
select 'Grade2' as Grade,40 as MinScore, 69 as MaxScore union all
select 'Grade3' as Grade,70 as MinScore, 100 as MaxScore
)
select g.Grade, sum(s.Items) from sample s
inner join LkpGrade g on s.score between g.MinScore and g.MaxScore
group by g.Grade;

hope this helps

Regards,
Avi
born2achieve
born2achieve
SSC Veteran
SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)SSC Veteran (230 reputation)

Group: General Forum Members
Points: 230 Visits: 474
thanks for your time on this pandey
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search