why introducing date parameter messes up my query? 3 tables JOIN in mySQL

Writing a report that will return 3 columns

  1. list of all simple products
  2. current stock levels against each product
  3. sold qty in specified period against each product. If no sales, then show zero.
sku       | qty | sold qty    
product 1 | 5   | 15    
product 2 | 7   | 0    
product 3 | 0   | 15    
product 4 | 0   | 0

Code works as intended when not specifying a date period. However, that gives me entire sales history. I want to see sales history for, say, last month, quarter, year.

When specifying a period using above line it skips SKUs that had no sales in specified period.

sku       | qty | sold qty    
product 1 | 5   | 15    
product 3 | 0   | 15

I would like to see all SKUs, including those with zero sales.

SELECT 
    p.sku,
    FORMAT(s.qty, 0) AS qty,
    IFNULL(FORMAT(SUM(o.qty_invoiced), 0), 0) AS 'sold qty'
FROM
    mage_catalog_product_entity AS p
        LEFT JOIN
    mage_cataloginventory_stock_item AS s ON p.entity_id = s.product_id
        LEFT JOIN
    mage_sales_flat_order_item AS o ON p.entity_id = o.product_id
WHERE
    p.type_id = 'simple' 
       AND o.created_at BETWEEN '2018-11-01 00:00:01' AND '2019-01-31 23:59:59'
GROUP BY p.sku
ORDER BY SUM(o.qty_invoiced) DESC;

I would like to see all SKUs in column 1, including those with zero sales. It does that when looking at entire sales history and not specifying a date period. Not sure how adding a date period in the mix changes the result.

#sql #mysql

1 Likes1.50 GEEK