
Zitat von
klausm
hallo,
ich habe 2 tabellen:
frage
-----------
id, text
-----------
1, text 1
2, text 2
3, text 3
antwort
-----------
frage_id, punkte
-----------
1, 5
1, 3
1, 1
3, 8
wie bekomme ich ein ergebnis mit allen texten und der summe der punkte bzw. null wenn es keine gibt? also hier:
frage, sum(punkte)
------------
text 1, 9
text 2, 0
text 3, 8
bei einem left join bekomme ich immer pro text die summe aller punkte.
vielen dank!
Wenn man es richtig macht, dann nicht:
Code:
test=*# select * from frage;
id | t
----+-------
1 | text1
2 | text2
3 | text3
(3 rows)
test=*# select * from antwort ;
f_id | p
------+---
1 | 5
1 | 3
1 | 1
3 | 8
test=# select f.t, coalesce(sum(a.p),0) as punkte from frage f left join antwort a on (f.id=a.f_id) group by f.t order by t;
t | punkte
-------+--------
text1 | 9
text2 | 0
text3 | 8
(3 rows)
(4 rows) Andreas
Lesezeichen