Skip to content
New issue

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

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

Already on GitHub? # to your account

How to avoid COALESCE in SUM operations #27825

Closed
dmitriysidorov opened this issue Apr 14, 2022 · 1 comment
Closed

How to avoid COALESCE in SUM operations #27825

dmitriysidorov opened this issue Apr 14, 2022 · 1 comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@dmitriysidorov
Copy link

dmitriysidorov commented Apr 14, 2022

I have a question about SUM implementation

When I use the SUM function and in DB no records or all records have a NULL value in the column, I always get 0. I am confused by the result. After checking query translation, I see that COALESCE function is used, which is really strange for me because NULL and 0 are not the same value.

In my scenario, if I get a NULL, as it should be in my opinion, I understand that no data there (we have nothing to sum), but I get a 0 which for me means that data exists and the result of the sum is 0, but in fact, it is not.

What do you think? Is it possible to use SUM without COALESCE?

Include provider and version information

EF Core version:
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (e.g. .NET 6.0)
Operating system:
IDE: (e.g. Visual Studio 2022 17.1)

@smitpatel
Copy link
Contributor

Sum function on LINQ returns 0 when the sequence is empty. Sum in database generally ignores null values. Both behavior and use of COALESCE by design. I believe your interpretation of null/0 as a result of sum is inconsistent with LINQ.

@smitpatel smitpatel added the closed-no-further-action The issue is closed and no further action is planned. label Apr 14, 2022
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

3 participants