Solved

How do I count Nulls and not nulls at the same time?

Posted on 2008-11-03
9
292 Views
Last Modified: 2010-04-21
I have a view in SQL server 2005, that I use as my reports data.

My records typically looks like this:

ProvinceID      PID      Province       CID      CName      CityID      CityName      Votes
20815      20815      Gauteng      208117      Pretoria      208117      Pretoria      321
20815      20815      Gauteng      208117      Pretoria      208117      Pretoria      386
20815      20815      Gauteng      208117      Pretoria      208117      Pretoria      311
20815      20815      Gauteng      208117      Pretoria      208117      Pretoria      323
20815      20815      Gauteng      208117      Pretoria      208117      Pretoria      321
20815      20815      Gauteng      208118      Boksburg      208118      Boksburg      218
20815      20815      Gauteng      208118      Boksburg      208118      Boksburg      320
20815      20815      Gauteng      208118      Boksburg      208118      Boksburg      430
20815      20815      Gauteng      208118      Boksburg      208118      Boksburg      340
20815      20815      Gauteng      208118      Boksburg      208118      Boksburg      350
10101      10101      KZN      101001      Durban      101001      Durban      40
NULL      10101      KZN      NULL      NULL      101002      Maritzburg      NULL
NULL      10101      KZN      NULL      NULL      101003      East London      NULL
NULL      10101      KZN      NULL      NULL      101004      Umhlanga      NULL
NULL      10101      KZN      NULL      NULL      101005      Salt Rock      NULL
NULL      10102      CPT      NULL      NULL      101006      Bellville      NULL

My desired outcome is:

Province      Total Cities with no votes              Total Cites
Gauteng      0                                    10
KZN      4                                     5
CPT      1                                     1

I group by PID, then I need to count all the Cities per Province where CID is NULL and where CID is not NULL (all the cities per province).

I can't seem to get it right!

Please advise.

0
Comment
Question by:Lalla Belle
[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
9 Comments
 
LVL 77

Expert Comment

by:peter57r
ID: 22867406
You wil have to count the nulls in a formula field.  Yu need 3 formula fields altogether.

In the group header:

WhilePrintingRecords;
Numbervar NullKnt:=0;
""

In the Detail line:

WhilePrintingRecords;
if isnull({table.CID}) then NullKnt:=NullKnt+1;
""
In the group Footer you display the count using a 3rd formula field.:

WhilePrintingRecords;
Numbervar NullKnt;
NullKnt
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22867473
To do this from the SQL side, you would just do something like this:
SELECT Province
, SUM(CASE WHEN CID IS NULL THEN 1 ELSE 0 END) AS [Total Cities with no votes]
, SUM(CASE WHEN CID IS NOT NULL THEN 1 ELSE 0 END) AS [Total Cities]
FROM ProvinceTable

Open in new window

0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22867476
Sorry hit submit too quickly:
SELECT Province
, SUM(CASE WHEN CID IS NULL THEN 1 ELSE 0 END) AS [Total Cities with no votes]
, SUM(CASE WHEN CID IS NOT NULL THEN 1 ELSE 0 END) AS [Total Cities]
FROM ProvinceTable
GROUP BY Province

Open in new window

0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 26

Expert Comment

by:Kurt Reinhardt
ID: 22867498
Even easier, why not just use the Running Total field (assuming you're using CR7 or greater and it looks like you're using 2008)?  Each running total field has three parts:
  1. Field to Summarize - in this case it's the CID field and you'll count the field
  2. Evaluate - select "use a formula" and type in isnull({table.CID}) for the running total to count no votes and not isnull({table.CID}) for the running total to count cities with votes
  3. Reset - in your case, the running total needs to be reset on either the POD or Province fields - if you have an actual group for either of these fields, then use that, else use the field itself...
You will need to create two running totals, one for each type of count, but this is simpler than creating a manual running total that has 3 formulas for each count
0
 

Author Comment

by:Lalla Belle
ID: 22868124
mwvisa1:

When I run the query, the output is

CID      Total Cities with no votes      Total Cities
20809      6                                   0
20810      6                                   0
20811      0                                   50
20812      0                                   45
20813      0                                   45
20814      0                                   50
20815      0                                   55

It works fine when There are no nulls, but the 'Total cities' should equal the 'Total Cities with no votes' when they are null, now it says 0?

Thanks
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22869205
I got you.  I thought the you wanted the count of NULLs and count of NOT NULLs.

Am I understanding better now that you want count of NULLs and then count of ALL including nulls?

If so, think this should work:
SELECT Province
, SUM(CASE WHEN CID IS NULL THEN 1 ELSE 0 END) AS [Total Cities with no votes]
, SUM(*) AS [Total Cities]
FROM ProvinceTable
GROUP BY Province

Open in new window

0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 22869219
Arg, too quick to click SUBMIT button...too much coffee (usually I have the opposite problem).
SELECT Province
, SUM(CASE WHEN CID IS NULL THEN 1 ELSE 0 END) AS [Total Cities with no votes]
, COUNT(*) AS [Total Cities]
FROM ProvinceTable
GROUP BY Province

Open in new window

0
 

Author Closing Comment

by:Lalla Belle
ID: 31512695
Thanks, enjoy the coffee!
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22870275
You are most welcome!!
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Crystal Reports: 5 Tests for Top Performance It is complete, your masterpiece report.  Not only does it meet your customer’s expectations, it blows them out the water, all they want is beautifully summarised and displayed in a myriad of ways. …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

617 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