views:

205

answers:

3

I'm trying to figure out some sliding window stats on my users. I have a table with a user, and columns such as created_at and verified_at. For each month, I'd like to find out how many users registered (a simple group by date_trunc of the created_at), and then of those people, how many verified within my sliding window (call it 60 days).

I'd like to do a SQL query that gives me something like:

Month    | Registered | Verified in 60 days
Jan 2009 | 1543       | 107
Feb 2009 | 2000       | 250

I'm using postgresql. I starting looking at sum(case...), but I don't know if I can get my case to be dependent on the date_trunc somehow.

This doesn't work, of course, but here's the idea:

SELECT DATE_TRUNC('month', created_at) as month, 
COUNT(*) as registered,
SUM(CASE WHEN verified_at < month+60 THEN 1 ELSE 0 END) as verified
FROM users
GROUP BY DATE_TRUNC('month', created_at)
A: 

perhaps you could union together the different months.

select sum(whatever), 'january' from user where month = 'january'
union all
select sum(whatever), 'february' from user where month = 'february'
...
SquidScareMe
The key is I don't want to list out the months in the SQL query, as I don't know how many months I've got. This is a query that will run every day, for months (or years!) as new data comes in.
teich
+2  A: 
SELECT  COUNT(created_at) AS registered,
        SUM(CASE WHEN verified_at <= created_at + '60 day'::INTERVAL THEN 1 ELSE 0 END) AS verified
FROM    generate_series(1, 20) s
LEFT JOIN
        users
ON      created_at >= '2009-01-01'::datetime + (s || ' month')::interval
        AND created_at < '2009-01-01'::datetime + (s + 1 || ' month')::interval
GROUP BY
        s
Quassnoi
That's awesome. I wound up leaving out the series, and just using your interval in the code above, which works great.
teich
`@teich`: the series make sure that no gaps are left if there are no users for any month.
Quassnoi
A: 
SELECT
    MONTH,
    COUNT(*) AS Registered,
    SUM (CASE WHEN datediff(day,reg_date,ver_date) < 60 THEN 1 ELSE 0) as 'Verified in 60 //days datediff is an MSSQL function amend for postgresql'
FROM
    TABLE
GROUP BY
    MONTH
Paul Creasey