Error with SUM function using distinct #58101
Replies: 11 comments
-
|
This is expected behavior. You need to remove: $elementos->select('reclamacion.*')->distinct(); |
Beta Was this translation helpful? Give feedback.
-
|
Yeah, if one wants the sum of all one shouldn't use distinct—why would one anyway in such a case? |
Beta Was this translation helpful? Give feedback.
-
|
You can set distinct to false and after sum reset it to the initial value. |
Beta Was this translation helpful? Give feedback.
-
|
I need DISTINCT because the original query has many joins, and the query selects many fields. The The sum function should only perform the sum of the amounts from the result of the original query, not recalculate the records of that query. If you run a SQL SUM query, it doesn't group by amounts, but by all fields in the query, so in this case it would leave all records. |
Beta Was this translation helpful? Give feedback.
-
In that case, there would be more results than I need, because of the joins. |
Beta Was this translation helpful? Give feedback.
-
|
This probably isn't a Laravel but an SQL issue. Could you run |
Beta Was this translation helpful? Give feedback.
-
This is the result of ddRawSql() select |
Beta Was this translation helpful? Give feedback.
-
|
@DavidVaras ddRawSql explains the behavior. This is intentional in Laravel. What you want: Solution can be what @macropay-solutions mentioned or more readable version which shows what you want first then what you want next. |
Beta Was this translation helpful? Give feedback.
-
If you use In my humble opinion, you should never use For the calculation, I'm simply using a foreach loop in PHP. This ensures that the totaled values match the values returned by the query and those displayed on the screen. |
Beta Was this translation helpful? Give feedback.
-
|
@DavidVaras, $total = Model::distinct()
->get(['column'])
->sum('column');Using PHP on the large data, will cause less efficient to loads all rows into memory. |
Beta Was this translation helpful? Give feedback.
-
|
Okay, thank you all very much. I thought that was how it was supposed to work. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Laravel Version
12.8.1
PHP Version
8.3.28
Database Driver & Version
mysql Ver 8.0.32 for Linux on x86_64 (MySQL Community Server - GPL)
Description
If your query uses
DISTINCTand you want to find the sum of a field, such as an amount, when you execute the function$elementos->sum('importe'), it will perform aDISTINCToperation on the amounts and sum them, instead of summing all the amounts across the records.Real-world example
This query uses
DISTINCT, and the result is shown above. If you run$elementos->sum(DB::raw('IFNULL(importe,0)'));A filter is applied to the different amounts in the result; it doesn't use all the rows displayed. This means that when you have identical amounts, only one of them is summed.
The result of that instruction would be:
From my point of view it is a mistake because when you ask for the sum of the records you want the total sum and not just the different ones.
Thanks in advance.
Steps To Reproduce
Create an Eloquent query with multiple fields, using the DISTINCT clause in the SELECT clause, and one of the fields being an amount field. There must be multiple amounts with the same value within the selected records.
$elementos = Reclamacion::whereNull('reclamacion.deleted_at');
$elementos->select('reclamacion.*')->distinct();
$elementos->sum(DB::raw('IFNULL(importe,0)'));
Beta Was this translation helpful? Give feedback.
All reactions