Solved

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

Posted on 2007-03-22
3
262 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

821 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