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

Add aggregate function SUM_IF() #19060

Open
Stevoisiak opened this issue Feb 15, 2023 · 7 comments
Open

Add aggregate function SUM_IF() #19060

Stevoisiak opened this issue Feb 15, 2023 · 7 comments

Comments

@Stevoisiak
Copy link

Stevoisiak commented Feb 15, 2023

Feature Request

Implement a SUM_IF(x, y) aggregate function that would return the sum of TRUE input values. This would be equivalent to SUM(CASE WHEN x THEN y END)

Desired Functionality

Add a function similar to COUNT_IF(x) that would allow developers to simplify their code and improve readability of conditional sums.

Example Usage

Take the following example of conditional sums:

SELECT
    key_column,
    SUM(CASE WHEN category = "sports" THEN budget END) AS sports_budget,
    SUM(CASE WHEN category = "food" THEN budget END) AS food_budget,
    SUM(CASE WHEN category = "advertising" THEN budget END) AS advertising_budget
FROM table_name
GROUP BY 1

Adding a SUM_IF() function would allow it to be rewritten as:

SELECT
    key_column,
    SUM_IF(category = "sports", budget) AS sports_budget,
    SUM_IF(category = "food", budget) AS food_budget,
    SUM_IF(category = "advertising", budget) AS advertising_budget
FROM table_name
GROUP BY 1

Additional Information

The syntax could optionally allow for a third parameter n to allow specifying a fallback SUM value that would avoid NULL values. So for example, SUM_IF(condition, value, 0) would be equivalent to SUM(CASE WHEN condition THEN value ELSE 0 END).

@tdcmeehan
Copy link
Contributor

CC @kaikalur

@ges1227
Copy link
Contributor

ges1227 commented Oct 11, 2023

I'd like to take on this feature request 😀

@tsafacjo
Copy link

tsafacjo commented Nov 1, 2023

I'd like to take on this feature request too.

@tdcmeehan
Copy link
Contributor

Feel free to submit a PR!

@DHRUV6029
Copy link

hi @tdcmeehan is this still open can i work on this

@tdcmeehan
Copy link
Contributor

@DHRUV6029 yes it's still open.

@DHRUV6029
Copy link

hello @tdcmeehan @Stevoisiak @ges1227 I have submitted the pr #19060 for this feature can you please review it please.

thanks

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

Successfully merging a pull request may close this issue.

5 participants