Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CaseStatement has no aggregate functions #713

Closed
gitmalong opened this issue Oct 10, 2023 · 2 comments
Closed

CaseStatement has no aggregate functions #713

gitmalong opened this issue Oct 10, 2023 · 2 comments

Comments

@gitmalong
Copy link

gitmalong commented Oct 10, 2023

How can I aggregate the SUM over the result of an CASE expression? I found there are no aggregate functions such as count or sum when building a CaseStatement.

https://docs.rs/sea-query/latest/sea_query/query/struct.CaseStatement.html

https://github.com/SeaQL/sea-query/pull/304/files#diff-8f5819aa8f1164e24c550b1dbe468b3fd76da938b9c19e8c17144d64ad463733R993

In plain postgres SQL I would do:

SUM(
     CASE WHEN years_passed IS NOT NULL
     THEN gain_loss ELSE CAST(0 AS DECIMAL) END
) 
AS my_val
@gitmalong gitmalong changed the title CaseStatement has not aggregate functions CaseStatement has no aggregate functions Oct 10, 2023
@gitmalong
Copy link
Author

I found I can make it work by wrapping Func::sum() around

@tyt2y3
Copy link
Member

tyt2y3 commented Oct 12, 2023

@gitmalong thanks for posting this. Would you mind posting the full snippet, that others might find useful?

Untested off my head:

Func::sum(Expr::case(..).finally(..))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants