Added Sum_IF() aggregate function #24174
Open
+262
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Add new aggregate function sum_if(x, y) and sum_if(x, y, n) that returns the sum of values matching a condition. This provides a more concise way to write conditional sums without using CASE expressions.
Motivation and Context
Currently, to sum values conditionally, users need to write verbose CASE expressions like sum(CASE WHEN x THEN y END). The new sum_if function provides a cleaner, more readable alternative similar to the existing count_if function. This also aligns with common SQL developer requests for simplified conditional aggregations.
Issue/Feature Request #19060
Impact
Adds new SQL aggregate function sum_if
No breaking changes to existing functionality
Improves SQL readability for conditional sum operations
Test Plan
Added unit tests for basic functionality
Added tests for null handling
Added tests for default value parameter
Added tests for various data types
Added tests for edge cases (empty sets, all nulls)
Added integration tests with real data scenarios
Contributor checklist