简体   繁体   中英

SQL aggregation using join

I have 2 tables with Naam as primary key, the one table contains information on Naam (lumchartcentrumuser) and the other table contains information on presentations held by naam (lumchartecentrumonderwijs).

I want to use a bit more complex aggregation which counts the number of presentations grouped by Naam, using a where however i keep on getting errrors. Does anybody see what i am doing wrong :

SELECT lumchartcentrumuser.Naam,
       COUNT(lumchartecentrumonderwijs.ID) AS Getal
FROM lumchartecentrumonderwijs
WHERE lumchartcentrumuser.Type <> 3
 AND lumchartecentrumonderwijs.Categorie <> "
LEFT JOIN lumchartcentrumuser ON
lumchartecentrumonderwijs.Naam=lumchartcentrumuser.Naam 
GROUP BY Naam

The syntax is wrong. It should be like SELECT..FROM...JOIN...WHERE...GROUP BY

SELECT lumchartcentrumuser.Naam, COUNT(lumchartecentrumonderwijs.ID) AS Getal
FROM lumchartecentrumonderwijs 
LEFT JOIN lumchartcentrumuser 
ON lumchartecentrumonderwijs.Naam=lumchartcentrumuser.Naam 
WHERE lumchartcentrumuser.Type <> 3 AND 
      lumchartecentrumonderwijs.Categorie <> '' 
GROUP BY Naam

You need to move your WHERE statements below the JOIN statements. Something like this

SELECT lumchartcentrumuser.Naam, COUNT(lumchartecentrumonderwijs.ID) AS Getal
FROM lumchartecentrumonderwijs 
LEFT JOIN lumchartcentrumuser 
ON lumchartecentrumonderwijs.Naam = lumchartcentrumuser.Naam
WHERE lumchartcentrumuser.Type <> 3 AND lumchartecentrumonderwijs.Categorie <> ""
GROUP BY Naam

Also make sure you close your quotation marks (fixed above).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM