Given a table
custid | date | action
1 | 2011-04-01 | activate
1 | 2011-04-10 | deactivate
1 | 2011-05-02 | activate
2 | 2011-04-01 | activate
3 | 2011-03-01 | activate
3 | 2011-04-01 | deactivate
The database is PostgreSQL.
I want an SQL query to show customers that were active at any stage during May.
So, in the above, that would be 1 and 2.
I just can’t get my head around the way to approach this. Any pointers?
update
Customer 2 was active during May, as he was activated Before May, and not Deactivated since he was Activated. As in, I’m alive this Month, but wasn’t born this month, and I’ve not died.
select distinct custid
from MyTable
where action = 'active' and date >= '20110501' and date < '20110601'
This approach won’t work, as it only shows activations during may, not ‘actives’.
Try this