Skip to content

"I imagine there's a more elegant way to do this using a window function" #40

@curiousleo

Description

@curiousleo

https://til.simonwillison.net/sql/cumulative-total-over-time says:

I imagine there's a more elegant way to do this using a window function but this works fine.

I was looking at queries of this sort recently. Here would be my suggestion:

select
  created_at,
  count(*) over (
    order by
      created_at
  ) as cumulative
from
  repos
where
  "owner" = :owner
order by
  created_at desc

It seems to run a little faster than the original query too.

Example query

I'd have written you an e-mail but couldn't find one on your website.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions