3

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
  • When you say "active" users, please define what you mean by that. Are you using creation_tsz or is there another column to use for "active"? Commented May 18, 2010 at 21:27
  • I am using creation_tsz, but I want to get this value not only for today (which I do with the above query) but for a range of dates I set. Commented May 18, 2010 at 21:29

3 Answers 3

2

Replace COUNT(*) with *.

Sign up to request clarification or add additional context in comments.

4 Comments

The above exhausted my allowed memory size.
@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'
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: 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.
1

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)

2 Comments

how do I specify the start and end date of the date range for which I want the results? Thanks
Try changing the where clause to: where creation_tsz between '2010-01-01' and '2010-01-30'
1

If each data row in your table represents a distinct user, you can use the query below.

SELECT COUNT(1), date
FROM table
WHERE date >= (now() - interval '30 days')
GROUP BY 2 ORDER BY 2 DESC;

If your table includes all sessions, but it has a distinct id per user, then you can use the query below.

SELECT COUNT(DISTINCT distinct_id), date
FROM table
WHERE date >= (now() - interval '30 days')
GROUP BY 2 ORDER BY 2 DESC;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.