I have following query working :
SELECT
COUNT(id), AgeRange
FROM
(
select
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 AS AgeRange
from queue
where DATE between '01-Apr-2011' and '05-May-2011'
) T
GROUP BY
AgeRange;
Now my requirement is that, I want these result to be printed always in a sequence, first for less than 0, than for 31-60 and so on.
Even if get count 0 for any interval say 31-60. It should return 0 for that interval. Can anyone help.
Further clarification: what I want is if for any case I am getting zero count it should also print that.
In my table of database for this query it is not displaying the ‘less than 0’ case:
COUNT(ID) AGERANGE AGERANGESEQUENCE
-------------- -------------- ----------------
11139 0-30 2
2292 31-60 3
329 61-90 4
1078 91+ 5
746 NO INFORMATION 6
Its not showing me the first row of count 0.
I want that too, so that I can get a well defined table structure.
If you want to order by that column – just add an
after the
GROUP BY AgeRangeclause:If that sequence doesn’t work out for you, then you’d have to add a second
CASEstatement to define aAgeRangeSequenceto your inner select and then order by that column:PS: in your
CASEstatement, you should check forWHEN age IS NULLand not useWHEN age = NULLsince you cannot compare toNULLusing the normal comparison operators!