Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-03-22
3
Medium Priority
?
281 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

704 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