Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Database Server Software > MySQL > POP CENSUS QUERY PROBLEM ver 4.1 MySQL

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-20-07, 03:59
MadCoding MadCoding is offline
Registered User
 
Join Date: Sep 2004
Posts: 10
Question POP CENSUS QUERY PROBLEM ver 4.1 MySQL

Hi all,

I am trying to clarify the proper syntax for the following statement:

select
ifnull(count(if(cat_id=1,1,null)),0) as count1,
ifnull(count(if(cat_id=2,1,null)),0) as count2,
ifnull(count(if(cat_id=3,1,null)),0) as count3,
ifnull(count(if(cat_id=4,1,null)),0) as count4,
ifnull(count(if(cat_id=5,1,null)),0) as count5,
ifnull(count(if(cat_id=6,1,null)),0) as count6,
ifnull(count(if(cat_id=7,1,null)),0) as count7,
ifnull(count(if(cat_id=8,1,null)),0) as count8,
ifnull(count(if(cat_id=9,1,null)),0) as count9,
ifnull(count(if(cat_id=10,1,null)),0) as count10,
ifnull(count(if(cat_id=11,1,null)),0) as count11,
ifnull(count(if(cat_id=12,1,null)),0) as count12,
ifnull(count(if(cat_id=13,1,null)),0) as count13,
ifnull(count(if(cat_id=14,1,null)),0) as count14,
ifnull(count(if(cat_id=15,1,null)),0) as count15,
ifnull(count(if(cat_id=16,1,null)),0) as count16,
ifnull(count(if(cat_id=17,1,null)),0) as count17,
ifnull(count(if(cat_id=18,1,null)),0) as count18,
from descriptions

Ok this is the query for a piece of Cold Fusion 7.0 code, which is supposed to querying a multi indexed recursive table called "descriptions".

It is looking a column name called "cat_id" where values can only be 1 to 18 (numerical). cat_id is a actually the representation of a category scheme where there are 18 different categories being polled for thier population. However, because the data being inputted into this "descriptions" table, there may be CATEGORY EXCLUSIONS, or in other words, there could be a couple of categories excluded or missed. So when Cold Fusion looks at this issue, it has issues when dealing with NULL result values for a the above specific query.

This issue here is, instead of using ineffecient cold fusion switch statements (slow way of doing things) I am trying to put the work load unto the database as an alternative.

However, this is a little over my head currently, and I was given this mySQL statement as a possible solution to my situation, but I am finding it a bit daunting to figure out why the above statement keeps throwing a mySQL syntax error. I have never really used an advanced IFNULL statement before.

I just cannot see the issue. Can any MySQL guru's shed any light on the following issue?

Cheers and thanx in advance! I WANNA BE A GURU!

a WANNABEAGURU!
Reply With Quote
  #2 (permalink)  
Old 09-20-07, 05:30
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 734
Can you post the DDL for the table + a sample of the data contained within.
Doing a count on NULL will ALWAYS return 0 so it makes the IFNULL part of the statement moot.

I await further information so that a solution may be provided (& discussed) for you.

NOTE : DDL stands for data definition language. Thus if you do a "SHOW CREATE TABLE <tablename>" directly into MySQL substituting the <tablename> part for your actual table it should show you the created structure behind the table (easy for a cut and paste job here).

Last edited by aschk : 09-20-07 at 05:47.
Reply With Quote
  #3 (permalink)  
Old 09-20-07, 05:32
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 734
P.s. the syntax error you are getting if for the following line :
ifnull(count(if(cat_id=18,1,null)),0) as count18,

You need to remove the , (comma) at the end before the FROM part of the statement.
Reply With Quote
  #4 (permalink)  
Old 09-20-07, 05:35
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 734
Ooh, and you also need a GROUP BY clause in your statement in order to do use COUNT
Reply With Quote
  #5 (permalink)  
Old 09-20-07, 08:04
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,556
you do not need a GROUP BY if the only things in the SELECT are aggregate functions

however, the workload is optimized when you write the query like this instead --
Code:
select cat_id , count(*) as cat_count from description group by cat_id
i can assure you that in coldfusion, manipulating these results is trivial, and there won't be any pesky NULLs, either

__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On