joshmfrankel / joshmfrankel.github.io

Blog and Personal site
http://joshfrankel.me
MIT License
2 stars 1 forks source link

How to group SQL by month #64

Closed joshmfrankel closed 1 year ago

joshmfrankel commented 1 year ago
SELECT DATE_TRUNC('month', started_at) as month, device_type, COUNT(id) as total 
FROM ahoy_visits 
WHERE started_at > CURRENT_DATE - INTERVAL '12 months' [[AND device_type <> {{remove_device_type}}]]
GROUP BY month, device_type
ORDER BY month DESC;