Solved

Creating crosstab query using COUNT gives incorrect row and column totals

Posted on 2004-09-15
1
428 Views
Last Modified: 2012-08-14
Can someone tell me why the row and column totals compute properly with this crosstab query (using SUM):
MySQL Version: 4.0.20-standard

#crosstab
SELECT MONTHNAME(dt),
      SUM(IF(category = 'A',id, 0)) AS 'cat a',
      SUM(IF(category = 'B',id, 0)) AS 'cat b',
      SUM(IF(category = 'C',id, 0)) AS 'cat c',
#row totals
      SUM(id) AS 'TOTAL'
FROM t1
GROUP BY MONTH(dt)
UNION
#column totals
SELECT 'YTD',
      SUM(IF(category = 'A',id, 0)) AS 'cat a',
      SUM(IF(category = 'B',id, 0)) AS 'cat b',
      SUM(IF(category = 'C',id, 0)) AS 'cat c',
      SUM(id) AS 'TOTAL'
FROM t1;

But, this same crosstab query using COUNT, gives incorrect Totals:

#crosstab
SELECT MONTHNAME(dt),
      COUNT(DISTINCT(IF(category = 'A',id, 0))) AS 'cat a',
      COUNT(DISTINCT(IF(category = 'B',id, 0))) AS 'cat b',
      COUNT(DISTINCT(IF(category = 'C',id, 0))) AS 'cat c',
#row totals
      COUNT(DISTINCT(id)) AS 'TOTAL'
FROM t1
GROUP BY MONTH(dt)
UNION
#column totals
SELECT 'YTD',
      COUNT(DISTINCT(IF(category = 'A',id, 0))) AS 'cat a',
      COUNT(DISTINCT(IF(category = 'B',id, 0))) AS 'cat b',
      COUNT(DISTINCT(IF(category = 'C',id, 0))) AS 'cat c',
      COUNT(DISTINCT(id)) AS 'TOTAL'
FROM t1;

-------------------------------------------------
Here's the table structure:
CREATE TABLE `t1` (                                                                                                                                      
         `id` int(11) NOT NULL default '0',                                                                                                                      
         `category` char(2) default NULL,                                                                                                                        
         `dt` date default NULL,                                                                                                                                
         PRIMARY KEY  (`id`)                                                                                                                                    
---------------------------------------------------
Here's the data:
1,A,2004-01-01,
2,B,2004-04-01,
3,C,2004-02-01,
4,A,2004-05-01,
5,B,2004-03-01,
6,C,2004-04-01,
7,A,2004-06-01,
8,B,2004-01-01,

--------------------------------------------------
I haven't been able to find anything on this topic via internet searches.
0
Comment
Question by:dplinnane
1 Comment
 
LVL 12

Accepted Solution

by:
AdrianSRU earned 500 total points
ID: 12070426
COUNT gives the number of non-null values.

COUNT(DISTINCT(IF(category = 'A',id, 0)))
will not be null unless id is null.

Try this:
COUNT(DISTINCT(IF(category = 'A',id, NULL)))


-Adrian
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

More Fun with XML and MySQL – Parsing Delimited String with a Single SQL Statement Are you ready for another of my SQL tidbits?  Hopefully so, as in this adventure, I will be covering a topic that comes up a lot which is parsing a comma (or other…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

760 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

17 Experts available now in Live!

Get 1:1 Help Now