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

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

Writing a report that will return 3 columns

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.

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

What are the differences between Standard SQL and Transact-SQL?

In this article, we'll explain syntax differences between standard SQL and the Transact-SQL language dedicated to interacting with the SQL

SQL Full Course - SQL Tutorial For Beginners - MySQL Essentials - SQL Training

This SQL Full Course video will cover all the topics of SQL starting from scratch. This video is great for beginners who want to learn SQL and for advanced people to brush up their skills.