Created
September 23, 2009 22:58
-
-
Save latompa/192366 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
You have an audit table | |
created | action | |
---------------------+-------- | |
2009-01-01 00:00:00 | create | |
2009-01-02 00:00:00 | delete | |
2009-01-15 00:00:00 | create | |
2009-01-16 00:00:00 | create | |
and you want a report with two counts, created and deleted for each week | |
week | created | deleted | |
---------------------+---------+--------- | |
2008-12-29 00:00:00 | 1 | 1 | |
2009-01-12 00:00:00 | 2 | 0 | |
SELECT week, SUM(created) as created, SUM(deleted) as deleted | |
FROM | |
( | |
(SELECT DATE_TRUNC('week', created_at) AS week, COUNT(*) as created, 0 as deleted | |
FROM audits WHERE action = 'create' GROUP BY week) | |
UNION | |
(SELECT DATE_TRUNC('week', created_at) AS week, 0 as created, COUNT(*) as deleted | |
FROM audits WHERE action = 'delete' GROUP BY week) | |
) s | |
GROUP BY week | |
ORDER BY week ASC | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment