I’m here again with another question/problem. I know that topic’s title may be like some others here, but I’ve noticed that they had another problems with that.
I have a problem with SQL LEFT JOIN. I’m using MySQL.
I want to have a table where TYPES would be for six months and calculations for one month (it might look strange, but I need it for some purposes).
I have two queries (I’ll show query and output that it would be more specific).
First query is for six months:
SELECT objectsoort AS types, COUNT(objectsoort) AS total, prijs AS price, SUM(prijs)/ SUM(oppervlakte) AS meters
FROM `huislijn`
WHERE <clause statements>
GROUP BY objectsoort
And I get result like this:
Second query is for one month:
SELECT objectsoort AS types1, COUNT(objectsoort) AS total1, AVG(prijs) AS price1, SUM(prijs)/ SUM(oppervlakte) AS meters1
FROM `huislijn`
WHERE <clause statements>
GROUP BY objectsoort
Then I get result like that:
I want to make them as one so I’m using LEFT JOIN and get query like that:
SELECT a.types1, b.total1, b.price1, b.meters1
FROM (SELECT objectsoort AS types1
FROM `huislijn`
WHERE <clause statements> ) a
LEFT JOIN
(SELECT objectsoort AS types12, COUNT( objectsoort ) AS total1, AVG( prijs ) AS price1, SUM( prijs ) / SUM( oppervlakte ) AS meters1
FROM `huislijn`
WHERE <clause statements> ) b
ON a.types1 = b.types12
GROUP BY types1
BUT the result isn’t what I’ve expected, because there should be some result near ‘appartement’ and ‘woning’ results isn’t the same as in query for one month:
I repeat that it would be more clearly: I want types of six months and calculations for one month. In this case it means that results should be near ‘appartement’ and ‘woning’.
So, I wanted to know, maybe I’m doing something wrong.
Thanks for your help and support.



Ok, I was asked to share solution, so here it is:
As cularis said, I was missed
GROUP BY objectsoortin some places. There’s the working query (I’ve changed it a bit, because working on it, but result is still the same):The result:
Thanks for cularis!