tags:

views:

47

answers:

2

This SQL query gives me today's number of users active in the last 30 days:

SELECT COUNT(*) 
FROM table.users
WHERE creation_tsz >= (now() - interval '30 days')

How can I modify it to get not a single value, but a table of active users for a range of dates?

My desired output would look like this:

Date       Users active in the last 30 days  
1/1/2010   10000  
1/2/2010   11234  
1/3/2010   12343  
1/4/2010   15944  
...        ... 

Thanks,

Rob

+2  A: 

Replace COUNT(*) with *.

Andomar
Can't argue with that.
David Neale
The above exhausted my allowed memory size.
Roberto
@Roberto: You can reduce the size of the output in two ways. Either limit the number of columns by replacing `count(*)` with `name, lastname, email, ...`. Or reduce the time window, i.e. `interval '5 days'`
Andomar
@Andomar: the interval must be 30 days, what I want to do is have a table output with two colums: a date (from a range I specify) and the number of users active in the 30 days leading to that date --maybe I wasn't clear.
Roberto
@Roberto: For the date range, try `where creation_tsz between ('2010-01-01' - interval '30 days') and '2010-01-01'`. Maybe you can post an example of the output you'd like to see.
Andomar
+1  A: 

I don't know what database you are using so it is hard to be specific. If there are no times in your dates, you can do this:

SELECT creation_tsz as Date, COUNT(*) as Count
FROM table.users
WHERE creation_tsz >= (now() - interval '30 days')
GROUP BY creation_tsz

Otherwise, this will get you pretty close to what you want (year excluded because you are only doing a 30 day range):

SELECT month(creation_tsz) as Month, day(creation_tsz) as Day, COUNT(*) as Count
FROM table.users
WHERE creation_tsz >= (now() - interval '30 days')
GROUP BY month(creation_tsz), day(creation_tsz)
RedFilter
@orbman:how do I specify the start and end date of the date range for which I want the results?Thanks
Roberto
Try changing the where clause to: `where creation_tsz between '2010-01-01' and '2010-01-30'`
Andomar