Reply
Using count multiple times in one query
Old 07-22-2008, 08:39 AM Using count multiple times in one query
drew22299's Avatar
Skilled Talker

Posts: 89
Location: Wiltshire, UK
Hi,

I want to count the number of records that, for example contain 'client' and 'NULL' using one query instead of lots of separate queries.

Here is my attempt at the query but it doesn't work, can anyone show me the correct mysql syntax?

PHP Code:
 
SELECT status
,
COUNT(status='client') AS "client",
COUNT(status='NULL') AS "not assigned"
FROM people
GROUP BY status 
__________________
www.hotlista.co.uk

Last edited by drew22299 : 07-22-2008 at 04:46 PM.
drew22299 is offline
Reply With Quote
View Public Profile Visit drew22299's homepage!
 
When You Register, These Ads Go Away!
Old 07-23-2008, 04:13 AM Re: Using count multiple times in one query
vividearth's Avatar
Experienced Talker

Posts: 32
Location: London, UK
Don't have MySql so these are not tested but you could do it using subquery if you need the column names as follows:

select 'clientcount'=(
select COUNT(*)
from people
where status='client'
),
'not assigned'=(
select COUNT(*)
from people
where statusisnull
)
from people

or like this if column name not important:

SELECT status,
COUNT(*) AS statuscount
FROM people
WHERE status='client' or status is null
GROUP BY status

Last edited by vividearth : 07-23-2008 at 04:14 AM.
vividearth is offline
Reply With Quote
View Public Profile Visit vividearth's homepage!
 
Old 07-23-2008, 05:13 AM Re: Using count multiple times in one query
drew22299's Avatar
Skilled Talker

Posts: 89
Location: Wiltshire, UK
Thanks for your reply vividearth I tried both of the queries but they didn't return the count for each data item stored in the status column.

The first query returned two columns each with the value of 1 for each record

PHP Code:
clientcount=select count(*) | not assigned=select(*)... 
The second query returns a column called status and statuscount but both contain no data. Is status a reserved word?

The status column only contains, NULL, client, closed and I want to count how many records have NULL, and how many in client etc

Would it be really slow to use three seperate count queries?
__________________
www.hotlista.co.uk
drew22299 is offline
Reply With Quote
View Public Profile Visit drew22299's homepage!
 
Old 07-23-2008, 05:43 AM Re: Using count multiple times in one query
chrishirst's Avatar
Super Moderator

Posts: 13,576
Location: Blackpool. UK
use a UNION query

Code:
select status,count(*) as rec from people where isnull(status) or status = "" group by status
union
select status,count(*) as rec from people where status = "closed" group by status
union
select status,count(*) as rec from people where status = "client" group by status
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 07-23-2008, 06:36 AM Re: Using count multiple times in one query
drew22299's Avatar
Skilled Talker

Posts: 89
Location: Wiltshire, UK
That query did exactly what I wanted, thanks chrishirst I was going to ask how to print the query results using PHP because I didn't know how to output data from a query that has different results for the same column name but I figured it out quickly:

PHP Code:
$query Count(*) ...
 
while(
$getPeopleIds->fetchInto($getPeopleId) {
 
echo 
$getPeopleId['status'];
echo 
$getPeopleId['rec'];
echo 
"<br>";
 

__________________
www.hotlista.co.uk
drew22299 is offline
Reply With Quote
View Public Profile Visit drew22299's homepage!
 
Old 07-23-2008, 02:46 PM Re: Using count multiple times in one query
Learning Newbie's Avatar
Moderator

Posts: 5,199
Name: John Alexander
Actually I'm surprised the answer hasn't come up yet.

Code:
Select Status, Sum(ClientCount) As Clients, Sum(NullCount) As Nulls From (
   Select
      Case When Status = 'Client' Then 1 Else 0 End As ClientCount,
      Case When Status Is Null Then 1 Else 0 End As NullCount,
      Status
   From People
   ) As InnerQuery
Group By Status
The advantage of this is efficiency. Once you start running subqueries and unions, you're parsing the table multiple times, in slices. Without good indexing, it forces multiple table scans. With proper indexes, it only adds management overhead. The way I posted, above, is purely set based, and will force the table to be read once, with all parsing happening inline.
__________________
4 ways to improve the lives of the "bottom billion"

"HEY YOU KIDS GET OFF MY LAWN!" -John McCain
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Old 07-23-2008, 02:55 PM Re: Using count multiple times in one query
Learning Newbie's Avatar
Moderator

Posts: 5,199
Name: John Alexander
I just realized - you're asking for a count by value for one column, not counts within other slices. The query I posted would be great if you wanted to know how many null vs "client" values were in the status column rated by age, zip code, or whatever.

Actually the easy answer seems to be

Select Status, Count(*) From People Group By Status

This will return a table like

Status - Count
Null - 23
Client - 15
Some Other Value - 92
Yet Another Value - 1
__________________
4 ways to improve the lives of the "bottom billion"

"HEY YOU KIDS GET OFF MY LAWN!" -John McCain
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Old 07-24-2008, 12:37 PM Re: Using count multiple times in one query
drew22299's Avatar
Skilled Talker

Posts: 89
Location: Wiltshire, UK
You can always rely on Learning Newbie to show you the most efficient queries
__________________
www.hotlista.co.uk
drew22299 is offline
Reply With Quote
View Public Profile Visit drew22299's homepage!
 
Old 07-24-2008, 07:54 PM Re: Using count multiple times in one query
Learning Newbie's Avatar
Moderator

Posts: 5,199
Name: John Alexander
Quote:
Originally Posted by drew22299 View Post
You can always rely on Learning Newbie to show you the most efficient queries
It's what I do - for a living. I realize half the time when someone asks a question like this, they don't need the absolute best performance. But I never know when they do or when they don't, and if a web site with server side code and also queries a database per request, once you start having more than a handful of visitors, performance becomes scalability. So I figure it's important.
__________________
4 ways to improve the lives of the "bottom billion"

"HEY YOU KIDS GET OFF MY LAWN!" -John McCain
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Using count multiple times in one query
 

Thread Tools

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.16885 seconds with 12 queries