i have following query working..
select count(id) ,
case
when age < 0 then 'less than 0'
when age >= 0 and age <=30 then '0-30'
when age >= 31 and age <=60 then '31-60'
when age >= 61 and age <=90 then '61-90'
when age >= 91 then '91+'
when age = null then 'NO INFORMATION'
else 'no catagory'
end
from queue where DATE between '01-Apr-2011' and '05-May-2011'
group by case
when age < 0 then 'less than 0'
when age >= 0 and age <=30 then '0-30'
when age >= 31 and age <=60 then '31-60'
when age >= 61 and age <=90 then '61-90'
when age >= 91 then '91+'
when age = null then 'NO INFORMATION'
else 'no catagory'
end
/
now my requirement is that, i dont want to use CASE clause again for grouping it with count..
by using CASE clause only once i want the same result..
also i want a sequence always to be followed by result of this query..
for ex it always print count first for ‘less than 0’ then for ‘0-30’ and so on.. even if there is 0 count found for interval say 31-60 it should print 0 for that it interval…
can anyone plzz help..
thanx in advance
You have to group by on all non-aggregated, non-constant columns and expressions
So you either repeat the entire CASE or you use a derived table like this if you don’t
want to repeat it.
This is standard SQL, should work in all mainstream RDBMS
Edit: Note I aliased the CASE as AgeRange and grouped on that.
GROUP BY (ordinal)isn’t standard SQL. It doesn’t work in SQL Server