2
0
Fork 0
mirror of https://github.com/MartinThoma/LaTeX-examples.git synced 2025-04-25 14:28:05 +02:00
LaTeX-examples/documents/musterloesung-db-klausur-b/d3d.sql
Stefan Koch 756fee46ef 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
2013-07-30 11:14:45 +02:00

6 lines
201 B
SQL

CREATE VIEW Beratungsanzahl AS (
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
)