simonw / simonw/til

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

Open
#40 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
HTML
Stars
1.5k
Forks
120
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Open the linked TIL entry about cumulative totals and compare its original query with the proposed SQL window-function query in this issue. Check the linked example query against the stated owner filter and ordering; done means the TIL presents the improved query accurately and its example still works.

Written by the indexing model from the issue text.

Assessment

Tech stack
sql
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.