| Author |
Topic |
|
corboamnesiac
Starting Member
5 Posts |
Posted - 2008-03-07 : 15:31:51
|
| suppose i havecolId | col2 | col3 | qtycol1 toyota camry 12 toyota camry 13 honda accord 14 honda accord 15 honda accord 1how do i return the sum of the qty for each car/modellike this:1 toyota camry 22 toyota camry 23 honda accord 34 honda accord 35 honda accord 3im having trouble applying SUM & GROUP by because im also selecting colID which is unique.what do i do!? |
|
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2008-03-07 : 17:19:42
|
Don't select colid Poor planning on your part does not constitute an emergency on my part. |
 |
|
|
corboamnesiac
Starting Member
5 Posts |
Posted - 2008-03-07 : 17:40:44
|
| i need to select it. |
 |
|
|
jdaman
Constraint Violating Yak Guru
354 Posts |
Posted - 2008-03-07 : 17:44:28
|
If that is truly what you need you can as follows:SELECT a1.colId, a1.col2, a1.col3, a2.totalqtyFROM YourTableHere a1 JOIN (SELECT col2, col3, SUM(qtycol) AS totalqty FROM YourTableHere GROUP BY col2, col3) a2 ON a1.col2 = a2.col2 AND a1.col3 = a2.col3 |
 |
|
|
corboamnesiac
Starting Member
5 Posts |
Posted - 2008-03-07 : 18:06:59
|
| i get an error trying to execute such query.does it matter if the total rows returned by that subquerydoes not match the outer query. it returns fewer rows since its GROUPING them right? |
 |
|
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2008-03-07 : 18:19:47
|
What error did you get? What was the error? what was the query you actually ran that produced the error?We can't help you if you don't provide adequate information.It seems like you want each row to have the total of all rows in a particular group?given your exact sample data, and exact requested results...the query posted works. Poor planning on your part does not constitute an emergency on my part. |
 |
|
|
corboamnesiac
Starting Member
5 Posts |
Posted - 2008-03-07 : 18:57:51
|
| the error message isnt very helpful. it just says it couldnt parse the query. Im using SQL Server Mobile.im guessing it doesnt support joining by a subquery??? |
 |
|
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2008-03-07 : 19:00:37
|
Possibly not. Post what you have, if you can..maybe we can tweak it to not require a subquery. Poor planning on your part does not constitute an emergency on my part. |
 |
|
|
corboamnesiac
Starting Member
5 Posts |
Posted - 2008-03-07 : 19:10:22
|
| SELECT a1.item_id, a1.car_id, a2.make, a2.model, a3.totalqtyFROM car a1, (SELECT a.car_id, b.make, b.model, SUM(a.qty) AS totalqty FROM car_info b, car a WHERE a.car_id = b.car_id GROUP BY a.car_id, b.make, b.model) a3, car_info a2WHERE a1.car_id = a3.car_id AND a3.make = a2.makeAND a3.model = a2.modelAND a1.car_id = a2.car_id |
 |
|
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2008-03-07 : 20:03:11
|
you have 3 tables there? not 2.Doesn't look like what was provided.here is the edit: SELECT a1.item_id,a1.car_id,a2.make,a2.model,a3.totalqtyFROM car a1 (SELECT make, model, SUM(a.qty) AS totalqty FROM car_info GROUP BY make, model) a2ona1.car_id = a2.car_idAND a1.make = a2.makeAND a1.model = a2.model Poor planning on your part does not constitute an emergency on my part. |
 |
|
|
|