sequelize / sequelize/sequelize

Implementing PostgreSQL "SUM(field) FILTER (WHERE condition)"

Open
#5,732 28 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

dialect: postgres existing workaround status: awaiting response
Dominant language
TypeScript
Stars
30.4k
Forks
4.3k
Avg merge
1d 6h
Merged PRs (30d)
68

Description

I wonder if there is a proper way of writing the SUM FILTER expression besides wrapping the whole SQL as a literal(). The fn() does not seem to support post-clauses, neither literal() allows concatenation of multiple parameters:

db.literal(
    db.fn('sum', db.col(field1)),
    'FILTER (WHERE',
    db.where(db.col(field2), '>', value),
    ')'
);

Thanks.

Contributor guide

Open the contributing guide

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

The issue names fn(), literal(), db.col(), and db.where(), but no files or tests. Start by tracing how these expression helpers generate PostgreSQL SQL and review the existing discussion for the intended API. Done means SUM(field) FILTER (WHERE condition) can be expressed without wrapping the complete SQL in literal().

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, typescript
Domain
databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.