- Notifications
You must be signed in to change notification settings - Fork11.7k
Error with SUM function using distinct#58101
-
Laravel Version12.8.1 PHP Version8.3.28 Database Driver & Versionmysql Ver 8.0.32 for Linux on x86_64 (MySQL Community Server - GPL) DescriptionIf your query uses Real-world example
This query uses 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 ReproduceCreate 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. |
BetaWas this translation helpful?Give feedback.
All reactions
Replies: 11 comments
-
This is expected behavior. You need to remove: $elementos->select('reclamacion.*')->distinct(); |
BetaWas this translation helpful?Give feedback.
All reactions
-
Yeah, if one wants the sum ofall one shouldn't use distinct—why would one anyway in such a case? |
BetaWas this translation helpful?Give feedback.
All reactions
-
You can set distinct to false and after sum reset it to the initial value. |
BetaWas this translation helpful?Give feedback.
All reactions
-
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. |
BetaWas this translation helpful?Give feedback.
All reactions
-
In that case, there would be more results than I need, because of the joins. |
BetaWas this translation helpful?Give feedback.
All reactions
-
This probably isn't a Laravel but an SQL issue. Could you run |
BetaWas this translation helpful?Give feedback.
All reactions
-
This is the result of ddRawSql() select |
BetaWas this translation helpful?Give feedback.
All reactions
-
@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. |
BetaWas this translation helpful?Give feedback.
All reactions
-
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. |
BetaWas this translation helpful?Give feedback.
All reactions
-
@DavidVaras, $total = Model::distinct() ->get(['column']) ->sum('column'); Using PHP on the large data, will cause less efficient to loads all rows into memory. |
BetaWas this translation helpful?Give feedback.
All reactions
👍 1
-
Okay, thank you all very much. I thought that was how it was supposed to work. |
BetaWas this translation helpful?Give feedback.
All reactions
This discussion was converted from issue #58085 on December 12, 2025 11:25.