Streaks with post count

With some help from Claude

Owned by simonw, visibility: Public

SQL query
WITH raw_posts AS (
    SELECT created::date AS post_date
    FROM blog_entry
    UNION ALL
    SELECT created::date
    FROM blog_blogmark
    UNION ALL
    SELECT created::date
    FROM blog_quotation
),
dates_and_counts AS (
    SELECT 
        post_date,
        COUNT(*) as posts_this_date
    FROM raw_posts
    GROUP BY post_date
),
streak_groups AS (
    SELECT 
        post_date,
        posts_this_date,
        COUNT(*) OVER (PARTITION BY dateadd) as streak_length,
        FIRST_VALUE(post_date) OVER (PARTITION BY dateadd ORDER BY post_date) as start_date,
        LAST_VALUE(post_date) OVER (
            PARTITION BY dateadd 
            ORDER BY post_date
            ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
        ) as end_date,
        SUM(posts_this_date) OVER (PARTITION BY dateadd) as total_posts,
        ROW_NUMBER() OVER (PARTITION BY dateadd) as rn
    FROM (
        SELECT 
            post_date,
            posts_this_date,
            post_date - (ROW_NUMBER() OVER (ORDER BY post_date))::integer * interval '1 day' as dateadd
        FROM dates_and_counts
    ) d
)
SELECT 
    streak_length as duration_in_days,
    start_date,
    end_date,
    total_posts as num_posts
FROM streak_groups
WHERE rn = 1
ORDER BY start_date DESC

Results were truncated

duration_in_days start_date end_date num_posts
5 2025-01-02 2025-01-06 20
367 2023-12-31 2024-12-31 1151
1 2023-12-23 2023-12-23 1
4 2023-12-18 2023-12-21 6
3 2023-12-14 2023-12-16 5
9 2023-12-04 2023-12-12 23
3 2023-11-29 2023-12-01 9
3 2023-11-25 2023-11-27 4
4 2023-11-20 2023-11-23 17
6 2023-11-13 2023-11-18 13
2 2023-11-10 2023-11-11 3
2 2023-11-07 2023-11-08 4
2 2023-11-04 2023-11-05 5
3 2023-10-30 2023-11-01 8
6 2023-10-22 2023-10-27 13
1 2023-10-19 2023-10-19 2
1 2023-10-17 2023-10-17 3
1 2023-10-14 2023-10-14 2
4 2023-10-07 2023-10-10 7
12 2023-09-23 2023-10-04 31
4 2023-09-16 2023-09-19 6
3 2023-09-12 2023-09-14 6
3 2023-09-08 2023-09-10 6
3 2023-09-04 2023-09-06 8
1 2023-08-30 2023-08-30 4
19 2023-08-09 2023-08-27 35
4 2023-08-03 2023-08-06 5
1 2023-08-01 2023-08-01 1
6 2023-07-24 2023-07-29 9
3 2023-07-18 2023-07-20 7
1 2023-07-16 2023-07-16 2
2 2023-07-12 2023-07-13 4
3 2023-07-08 2023-07-10 6
1 2023-07-04 2023-07-04 1
4 2023-06-29 2023-07-02 4
1 2023-06-27 2023-06-27 2
2 2023-06-22 2023-06-23 2
3 2023-06-17 2023-06-19 4
4 2023-06-12 2023-06-15 6
1 2023-06-08 2023-06-08 5
7 2023-05-30 2023-06-05 12
1 2023-05-27 2023-05-27 3
2 2023-05-24 2023-05-25 7
5 2023-05-18 2023-05-22 14
2 2023-05-14 2023-05-15 6
5 2023-05-08 2023-05-12 21
5 2023-05-01 2023-05-05 15
7 2023-04-23 2023-04-29 14
3 2023-04-19 2023-04-21 7
8 2023-04-10 2023-04-17 29
2 2023-04-07 2023-04-08 10
11 2023-03-26 2023-04-05 41
4 2023-03-21 2023-03-24 21
12 2023-03-07 2023-03-18 31
1 2023-03-05 2023-03-05 1
1 2023-03-03 2023-03-03 1
1 2023-03-01 2023-03-01 2
4 2023-02-24 2023-02-27 9
8 2023-02-15 2023-02-22 19
1 2023-02-13 2023-02-13 2
11 2023-02-01 2023-02-11 20
12 2023-01-19 2023-01-30 27
5 2023-01-13 2023-01-17 8
2 2023-01-09 2023-01-10 4
3 2022-12-31 2023-01-02 5
1 2022-12-28 2022-12-28 1
1 2022-12-24 2022-12-24 1
1 2022-12-22 2022-12-22 3
1 2022-12-20 2022-12-20 3
1 2022-12-15 2022-12-15 1
3 2022-12-09 2022-12-11 3
4 2022-12-04 2022-12-07 9
2 2022-12-01 2022-12-02 3
2 2022-11-28 2022-11-29 3
1 2022-11-26 2022-11-26 2
2 2022-11-23 2022-11-24 2
4 2022-11-18 2022-11-21 5
1 2022-11-16 2022-11-16 3
1 2022-11-13 2022-11-13 1
1 2022-11-11 2022-11-11 1
6 2022-11-04 2022-11-09 11
2 2022-10-31 2022-11-01 2
3 2022-10-27 2022-10-29 5
3 2022-10-23 2022-10-25 3
1 2022-10-21 2022-10-21 1
8 2022-10-12 2022-10-19 11
1 2022-10-10 2022-10-10 1
2 2022-10-07 2022-10-08 4
7 2022-09-29 2022-10-05 17
1 2022-09-24 2022-09-24 1
7 2022-09-15 2022-09-21 19
1 2022-09-12 2022-09-12 3
4 2022-09-04 2022-09-07 14
3 2022-08-31 2022-09-02 7
1 2022-08-29 2022-08-29 1
2 2022-08-24 2022-08-25 3
4 2022-08-19 2022-08-22 7
2 2022-08-16 2022-08-17 4
1 2022-08-13 2022-08-13 1
3 2022-08-09 2022-08-11 8
1 2022-08-06 2022-08-06 1
7 2022-07-29 2022-08-04 10
4 2022-07-24 2022-07-27 8
1 2022-07-22 2022-07-22 1
2 2022-07-19 2022-07-20 5
3 2022-07-13 2022-07-15 3
3 2022-07-09 2022-07-11 3
2 2022-07-06 2022-07-07 5
1 2022-06-30 2022-06-30 1
1 2022-06-28 2022-06-28 2
1 2022-06-23 2022-06-23 2
3 2022-06-18 2022-06-20 5
2 2022-06-12 2022-06-13 3
2 2022-06-07 2022-06-08 3
1 2022-06-05 2022-06-05 1
2 2022-05-30 2022-05-31 4
2 2022-05-26 2022-05-27 4
3 2022-05-21 2022-05-23 4
4 2022-05-15 2022-05-18 7
1 2022-05-13 2022-05-13 1
1 2022-05-06 2022-05-06 1
3 2022-05-02 2022-05-04 5
5 2022-04-26 2022-04-30 9
1 2022-04-24 2022-04-24 2
1 2022-04-21 2022-04-21 1
2 2022-04-18 2022-04-19 4
1 2022-04-13 2022-04-13 2
2 2022-04-07 2022-04-08 2
2 2022-03-28 2022-03-29 2
2 2022-03-23 2022-03-24 4
1 2022-03-19 2022-03-19 1
1 2022-03-17 2022-03-17 1
2 2022-03-14 2022-03-15 5
1 2022-03-12 2022-03-12 2
3 2022-03-08 2022-03-10 4
2 2022-03-04 2022-03-05 2
2 2022-02-26 2022-02-27 3
1 2022-02-23 2022-02-23 1
2 2022-02-20 2022-02-21 2
2 2022-02-17 2022-02-18 2
2 2022-02-14 2022-02-15 2
1 2022-02-12 2022-02-12 2
2 2022-02-09 2022-02-10 3
1 2022-02-07 2022-02-07 1
1 2022-02-05 2022-02-05 1
1 2022-02-02 2022-02-02 2
2 2022-01-30 2022-01-31 4
1 2022-01-27 2022-01-27 3
1 2022-01-25 2022-01-25 2
2 2022-01-20 2022-01-21 2
8 2022-01-11 2022-01-18 9
2 2022-01-08 2022-01-09 2
1 2022-01-06 2022-01-06 1
1 2022-01-04 2022-01-04 1
1 2021-12-24 2021-12-24 3
1 2021-12-20 2021-12-20 1
3 2021-12-16 2021-12-18 4
1 2021-12-12 2021-12-12 1
6 2021-12-05 2021-12-10 11
2 2021-12-01 2021-12-02 2
1 2021-11-25 2021-11-25 1
1 2021-11-22 2021-11-22 4
1 2021-11-18 2021-11-18 2
1 2021-11-15 2021-11-15 1
1 2021-11-13 2021-11-13 1
1 2021-11-10 2021-11-10 1
5 2021-11-03 2021-11-07 7
2 2021-10-28 2021-10-29 3
1 2021-10-25 2021-10-25 1
1 2021-10-23 2021-10-23 1
1 2021-10-21 2021-10-21 1
3 2021-10-17 2021-10-19 5
1 2021-10-12 2021-10-12 1
1 2021-10-09 2021-10-09 1
1 2021-10-06 2021-10-06 2
2 2021-09-28 2021-09-29 2
1 2021-09-26 2021-09-26 1
2 2021-09-23 2021-09-24 3
2 2021-09-19 2021-09-20 2
1 2021-09-13 2021-09-13 1
1 2021-09-10 2021-09-10 1
3 2021-09-06 2021-09-08 4
1 2021-09-03 2021-09-03 1
1 2021-08-30 2021-08-30 2
1 2021-08-28 2021-08-28 1
5 2021-08-21 2021-08-25 12
1 2021-08-19 2021-08-19 1
2 2021-08-16 2021-08-17 2
2 2021-08-13 2021-08-14 3
4 2021-08-08 2021-08-11 7
1 2021-08-06 2021-08-06 4
2 2021-08-02 2021-08-03 2
2 2021-07-28 2021-07-29 3
4 2021-07-21 2021-07-24 4
4 2021-07-16 2021-07-19 11
2 2021-07-13 2021-07-14 4
1 2021-07-10 2021-07-10 1
3 2021-07-06 2021-07-08 4
2 2021-07-01 2021-07-02 5
2 2021-06-27 2021-06-28 3
Copy and export data

Duration: 21.87ms