CrossTab - COUNT(case when Field between 1 and 2 then Field else 0)

Hello all,
first i have a query like this:

COUNT(case when FRAL_UTIL between 1 and 11 then FRAL_UTIL else 0 end) as '1 a 12',
COUNT(case when FRAL_UTIL between 12 and 22 then FRAL_UTIL else 0 end) as '12 a 23',
COUNT(case when FRAL_UTIL between 23 and 33 then FRAL_UTIL else 0 end) as '23 a 34',
COUNT(case when FRAL_UTIL between 34 and 44 then FRAL_UTIL else 0 end) as '34 a 45',
COUNT(case when FRAL_UTIL between 45 and 55 then FRAL_UTIL else 0 end) as '45 a 56',
count(FRAL_UTIL) as 'Acumulador 1 a 56',
cast(round(cast(count(FRAL_UTIL) as float) / (select count(FRAL_UTIL) from FRALDARIO)*100, 1) as decimal(18,2)) as '%'

this was supposed to be a simple crosstab report, but as most of you realized, all those count between returns the same value, but then i ask... is it possible to make it count all columns THAT are between those values ? since right now if the field value is within the range, it will just count all values, and not the ones within the specified range..

so.. is it possible to do such thing ? (otherwise how could i do such thing in one query ? )
ps. sorry for my poor english since its not my main language
LVL 10
Who is Participating?
LowfatspreadConnect With a Mentor Commented:
you should specify NULL where you currently have 0 ...

count counts rows where the indicated column is not null...
coalesce(COUNT(case when FRAL_UTIL between 1 and 11 then FRAL_UTIL else null end),0) as '1 a 12',

if you want zero's ...
eguilhermeAuthor Commented:
so for now i putted null, and it showed as it was supposed to, but then that count actually works ?

thx man.. saved me some time
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.