I want to get my sales for each day which is located in my orders_summary table.
orders_summary table columns: id, date, amount, sku_id
products table columns: id, sku
Currently Im getting my daily sales like this:
SELECT MAX(CASE WHEN os.date = '01/01/2022' THEN COALESCE(amount,0)::INT ELSE 0 END) AS orders_1, MAX(CASE WHEN os.date = '01/02/2022' THEN COALESCE(amount,0)::INT ELSE 0 END) AS orders_2 FROM products AS p LEFT JOIN orders_summary AS os ON p.id = os.sku_id WHERE p.id = '1' GROUP BY p.id;
Is it important to add
AND date BETWEEN '01/01/2022' AND '01/02/2022' in my where clause?
Yes absolutely. Imagine having 10 years worth of data in the table where you’re only interested in the data for two days. You must use the
where clause which restricts the number of rows (down to 0.05% in this case) before doing the