Solved

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

Posted on 2007-03-22
3
253 Views
Last Modified: 2010-03-20
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
0
Comment
Question by:eguilherme
  • 2
3 Comments
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 500 total points
ID: 18771959
you should specify NULL where you currently have 0 ...

count counts rows where the indicated column is not null...
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 18771966
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 ...
0
 
LVL 10

Author Comment

by:eguilherme
ID: 18772017
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
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now