| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
|
| I have the following problem: (this is a simplified setup) I have a datatable (id, date, value) 1, 1.1.2005, 300 1, 2.1.2005, 400 2, 1.1.2005, 300 2, 2.1.2005, 400 3, 1.1.2005, 300 3, 2.1.2005, 400 and a keytable (parentid, id) 1, 1 1, 1 1, 2 2, 1 2, 2 2, 2 2, 3 Of course, "id" in my data table references "id" in my key table. Now i want to query the sum of "value" group by "date" for a certain "parentid" Unfortunately, as You can see, the "id" is not unique for a "parentid". SELECT SUM( value ), date FROM datatable INNER JOIN keytable ON datatable.id = keytable.id WHERE keytable.parentid = 2 GROUP BY date ORDER BY date will return the sum of these records: 2, 1.1.2005, 300 2, 2.1.2005, 400 2, 1.1.2005, 300 -- doublette! 2, 2.1.2005, 400 -- doublette! 3, 1.1.2005, 300 3, 2.1.2005, 400 because id=2 occurs twice for the keytable. Has anybody an idea how i can avoid the doublettes without using an IN( ... ) subselect which will probably take about forever? Thanks in advance, Joachim |
![]() |
| Thread Tools | |
| Display Modes | |