2
0
Fork 0
mirror of https://github.com/MartinThoma/LaTeX-examples.git synced 2025-04-26 06:48:04 +02:00

left join sufficient

A LEFT (OUTER) JOIN is totally sufficient here, you do not want to have one Berater NULL with about 10 customers (because some customers do not have a Berater).

Moreover, when using the altered version as expected (i.e. the one with Kunden2Berater table) there is no column Kunden.berater_id anymore. You thus have to join Kunden2Berater.

I have NOT checked the COUNT throroughly, but according to my understand you will have to count on one of the Kunden2Berater columns, because COUNT() will ignore NULL columns and this is what we want. If LEFT JOIN returned a row where there are no columns on the right side, the count shall give NULL. Yet, I have to admit that I do not understand COUNT() too well. The DISTINCT is then unneeded, because we GROUP BY berater_id and the kunden_id for each berater itself will already be unique. So please do not take the COUNT statement for granted now, it's untested. http://www.techonthenet.com/sql/count.php
This commit is contained in:
Stefan Koch 2013-07-30 11:14:45 +02:00
parent 4b52753c61
commit 756fee46ef

View file

@ -1,6 +1,6 @@
CREATE VIEW Beratungsanzahl AS (
SELECT berater_id, count(DISTINCT Berater.berater_id) AS Anzahl
FROM Berater
FULL OUTER JOIN Kunden ON Berater.berater_id = Kunden.berater_id
SELECT berater_id, COUNT(kb.kunden_id) AS anzahl
FROM Berater b
LEFT OUTER JOIN Kunden2Berater kb ON b.berater_id = kb.berater_id
GROUP BY berater_id
)