|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
Summing a Sum field
Hi,
I'm trying to Sum a field which is a Sum itself. MySQL doesn't seems to like it. Any ideas? The SQL is (a bit complex): SELECT Sum(ti.triQuantity) * i.ingQuantity, d.drnId, d.drnName FROM tblIngredients as i INNER JOIN tbldrinks AS d ON i.ingIngredientNum = d.drnId INNER JOIN tblTransactionItems as ti ON d.drnId = ti.triDrinkId INNER JOIN tblTransaction AS t ON ti.triTAId = t.trcId WHERE i.ingDrinkNum IN (SELECT ingDrinkNum FROM tblIngredients WHERE ingIngredientNum = 45) Help would be appreciated. Thanx |
|
#2
|
||||
|
||||
|
add
GROUP BY i.ingQuantity, d.drnId, d.drnName |
|
#3
|
||||
|
||||
|
I'm afraid I've posted the wrong SQL. Sorry.
Here is the correct one: SELECT Sum(ti.triQuantity) * i.ingQuantity as TotalQTY, d1.drnId, d1.drnName FROM tblDrinks as d1 INNER JOIN tblIngredients as i ON d1.drnId = i.ingIngredientNum INNER JOIN tbldrinks AS d ON i.ingDrinkNum = d.drnId INNER JOIN tblTransactionItems as ti ON d.drnId = ti.triDrinkId WHERE i.ingDrinkNum IN (48, 47) GROUP BY d1.drnId, d.drnId The problem is that the query returns sums of the same item, where as I want to get the total sum of that item (of all the occurrences) Thanx |
|
#4
|
||||
|
||||
|
keep looking at your groups
your GROUP BY list of columns must be all the non-aggregate columns in the SELECT list thus, it must be -- GROUP BY i.ingQuantity, d1.drnId, d1.drnName now, if that's the wrong grouping, then you have to address that |
![]() |
| Viewing: Dev Shed Forums > Databases > Database Management > Summing a Sum field |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|