Chat2Query
Hacker News

Powered by

TiDB Cloud

Q: How many new stories and comments per month?
WITH monthly_items AS ( SELECT DATE_FORMAT(FROM_UNIXTIME(time), '%Y-%m-01') AS month, type FROM items WHERE deleted = 0 AND dead = 0 AND time >= UNIX_TIMESTAMP('2023-01-01') ), monthly_counts AS ( SELECT month, type, COUNT(*) AS count FROM monthly_items GROUP BY month, type ) SELECT month, SUM( CASE WHEN type = 'story' THEN count ELSE 0 END ) AS new_stories, SUM( CASE WHEN type = 'comment' THEN count ELSE 0 END ) AS new_comments FROM monthly_counts GROUP BY month ORDER BY month ASC LIMIT 20;
+------------+-------------+--------------+ | month | new_stories | new_comments | |------------|-------------|--------------| | 2023-01-01 | 27479 | 350274 | |------------|-------------|--------------| | 2023-02-01 | 24240 | 323545 | |------------|-------------|--------------| | 2023-03-01 | 28274 | 363823 | |------------|-------------|--------------| | 2023-04-01 | 5127 | 68368 | +------------+-------------+--------------+