0

I'm trying to get all my accumulated sales for each store, even if the value is null (no order for the conditions), but the LEFT JOIN only gives me rows that have a correspondence, which does not suit me :

SELECT s.identifierExt as StoreID, 
YEAR(o.creation) AS Year,
MONTHNAME(o.creation) AS Month,
MONTH(o.creation) AS IDMonth,
ROUND(SUM(o.price), 2) AS Sales
FROM store s
LEFT JOIN order o ON o.store = s.id 
AND (o.creation < '2018-09-13 00:00:00') 
AND (o.place NOT IN ('PENDING','CANCELLED')) 
AND (o.creation > '2018-01-12 00:00:00')
GROUP BY Year, Month, StoreID
ORDER BY IDMonth, StoreID ASC

Thanks in advance.

FanoFN
  • 6,815
  • 2
  • 13
  • 33

1 Answers1

0

Possible it's because of the difference between INNER and OUTER joins. The inner join requires that there is a corresponding row (match on the join condition), the outer join does not have such a requirement: it'll return columns with NULL.

It seems you want an OUTER join:

SELECT s.identifierExt as StoreID, 
YEAR(o.creation) AS Year,
MONTHNAME(o.creation) AS Month,
MONTH(o.creation) AS IDMonth,
ROUND(SUM(o.price), 2) AS Sales
FROM store s
LEFT OUTER JOIN order o ON o.store = s.id 
AND (o.creation < '2018-09-13 00:00:00') 
AND (o.place NOT IN ('PENDING','CANCELLED')) 
AND (o.creation > '2018-01-12 00:00:00')
GROUP BY Year, Month, StoreID
ORDER BY IDMonth, StoreID ASC

This way you'll also get stores without orders.

Also see this answer on the difference between inner and outer joins

Rubenz
  • 46
  • 3