I have this query which selects bakery products (their id, flavor, kind, price), and groups them by the purchasedate, and the productID, then orders them by productID.
I'm trying to calculate the number of each certain product sold each day. When I tried to count this number, the sale price of some items returns to the original price. Any ideas on how to fix this?
And once I have that problem solved, calculating the income, (I = NBR_Products * Price) is easy.
Here's the query, and below it is a link to the DB if you want to run the query and see what it generates.
SELECT DISTINCT purchaseDate, i.productId, p.flavor, p.kind, round(p.price * (100 - s.percentOff) / 100.00, 2) as price
FROM Receipt as r, LineItem as i, Product as p, ProductXSale as x, Sale as s
WHERE r.id = i.receiptId AND i.productID = p.id AND p.id = x.productID AND x.saleID = s.id AND r.purchaseDate BETWEEN s.startDate AND s.endDate
UNION
SELECT DISTINCT purchaseDate, i.productId as productID, p.flavor, p.kind, p.price as price
FROM Receipt as r, LineItem as i, Product as p
WHERE r.id = i.receiptId AND i.productID = p.id AND i.productID NOT IN (SELECT x.productID
FROM Sale as s, ProductXSale as x
WHERE s.id = x.saleID AND r.purchaseDate BETWEEN s.startDate AND s.endDate)
GROUP BY purchaseDate, I.productId
ORDER BY i.productId;
Here's the link to the database: http://ift.tt/1R56cOj
Aucun commentaire:
Enregistrer un commentaire