Sales Report
Hi I’m trying to write a query that will produce a sales volume report for the last month for an e-commerce store. Its based around an Access Database. Table Structure follows.
Products
productid PK
name
Orders
orderID PK
date
Orderitems
id PK
orderid FK
productid FK
quantity
I have this
SELECT products.productid, Sum(orderitems.quantity) AS totalsold, products.[name] FROM orderitems, products, orders
WHERE products.productid= orderitems.productid AND orders.orderid=orderitems.orderid AND orders.[date]> DateAdd ("m", -1, date())
GROUP BY products.[name], products.productid
Which gives me the number of each item sold in the past month, but only for items where there have actually been sales. How would I adjust this to give me sales figures of 0 for products which haven’t sold?
I’m guessing its something like this:
SELECT products.productid, products.[name], IIF(Sum(orderitems.quantity) IS NULL, 0, Sum(orderitems.quantity)) AS totalsold
FROM products LEFT OUTER JOIN orderitems ON orderitems.productid = products.productid INNER JOIN orders ON orders.orderid=orderitems.orderid
WHERE orders.[date]> DateAdd ("m", -1, date())
GROUP BY products.[name], products.productid
But I can’t seem to get my nested joins right and the Access brackets around them.
Can anyone help?
Thanks