Link to home
Start Free TrialLog in
Avatar of Edgard Yamashita
Edgard YamashitaFlag for Brazil

asked on

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

Hello all,
first i have a query like this:

SELECT FRAL_TAM,
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 '%'
FROM FRALDARIO GROUP BY FRAL_TAM

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
ASKER CERTIFIED SOLUTION
Avatar of Lowfatspread
Lowfatspread
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
do
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 ...
Avatar of Edgard Yamashita

ASKER

omg,
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