dbTalk Databases Forums  

multiple count on same table

comp.databases.mysql comp.databases.mysql


Discuss multiple count on same table in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
pac Man
 
Posts: n/a

Default multiple count on same table - 11-16-2010 , 05:06 AM






Hi guys

Lets say I have a table:
Stats (id, person_id, creation_date)

I want to select stats for person_id = 1. I need to retrieve a
count(*) for person_id = 1, but for different times.

1 - How many times the person_id is in the table during the last
month?
2 - How many times the person_id is in the table during the last 3
months?
3 - How many times the person_id is in the table during the last 12
month?

I can of course make 3 different queries:

select count(*) as total_1_month from stats where person_id = 1 and
creation_date > DATE_SUB(curdate(), INTERVAL 1 MONTH);
select count(*) as total_3_month from stats where person_id = 1 and
creation_date > DATE_SUB(curdate(), INTERVAL 3 MONTH);
select count(*) as total_12_month from stats where person_id = 1 and
creation_date > DATE_SUB(curdate(), INTERVAL 12 MONTH);

Is it possibile to select these 3 kinds of stats in one query? As the
person_id is always the same. Thanks.

Reply With Quote
  #2  
Old   
Lennart Jonsson
 
Posts: n/a

Default Re: multiple count on same table - 11-16-2010 , 05:44 AM






On 2010-11-16 12:06, pac Man wrote:
Quote:
Hi guys

Lets say I have a table:
Stats (id, person_id, creation_date)

I want to select stats for person_id = 1. I need to retrieve a
count(*) for person_id = 1, but for different times.

1 - How many times the person_id is in the table during the last
month?
2 - How many times the person_id is in the table during the last 3
months?
3 - How many times the person_id is in the table during the last 12
month?

I can of course make 3 different queries:

select count(*) as total_1_month from stats where person_id = 1 and
creation_date > DATE_SUB(curdate(), INTERVAL 1 MONTH);
select count(*) as total_3_month from stats where person_id = 1 and
creation_date > DATE_SUB(curdate(), INTERVAL 3 MONTH);
select count(*) as total_12_month from stats where person_id = 1 and
creation_date > DATE_SUB(curdate(), INTERVAL 12 MONTH);

Is it possibile to select these 3 kinds of stats in one query? As the
person_id is always the same. Thanks.
Use a case stmt as in:

select ...,
count( case when
creation_date > DATE_SUB(curdate(), INTERVAL 12 MONTH)
then 1 end ) as total_12_month
from stats where person_id = 1

you might want to spell out the null explicetly, but it is not necessary:

case when ... then 1 else null end


/Lennart

Reply With Quote
  #3  
Old   
pac Man
 
Posts: n/a

Default Re: multiple count on same table - 11-16-2010 , 06:08 AM



On 16 Nov, 12:44, Lennart Jonsson <erik.lennart.jons... (AT) gmail (DOT) com>
wrote:
Quote:
On 2010-11-16 12:06, pac Man wrote:



Hi guys

Lets say I have a table:
Stats (id, person_id, creation_date)

I want to select stats for person_id = 1. I need to retrieve a
count(*) for person_id = 1, but for different times.

1 - How many times the person_id is in the table during the last
month?
2 - How many times the person_id is in the table during the last 3
months?
3 - How many times the person_id is in the table during the last 12
month?

I can of course make 3 different queries:

select count(*) as total_1_month from stats where person_id = 1 and
creation_date *> DATE_SUB(curdate(), INTERVAL 1 MONTH);
select count(*) as total_3_month from stats where person_id = 1 and
creation_date *> DATE_SUB(curdate(), INTERVAL 3 MONTH);
select count(*) as total_12_month from stats where person_id = 1 and
creation_date *> DATE_SUB(curdate(), INTERVAL 12 MONTH);

Is it possibile to select these 3 kinds of stats in one query? As the
person_id is always the same. Thanks.

Use a case stmt as in:

select ...,
* * count( case when
* * * * * * * * creation_date *> DATE_SUB(curdate(), INTERVAL 12 MONTH)
* * * * * *then 1 end ) as total_12_month
from stats where person_id = 1

you might want to spell out the null explicetly, but it is not necessary:

case when ... then 1 else null end

/Lennart
Works like a charm, thank you!

Reply With Quote
Reply




Thread Tools
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 On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.