Professional Documents
Culture Documents
Reports
Reports
p.ProductName,
p.Category,
IFNULL(SUM(CASE WHEN t.TransactionType = 'Sale' THEN oi.Quantity ELSE 0
END), 0) AS TotalSalesQuantity,
IFNULL(SUM(CASE WHEN t.TransactionType = 'Sale' THEN oi.Subtotal ELSE 0
END), 0) AS TotalSalesAmount,
IFNULL(SUM(CASE WHEN t.TransactionType = 'Purchase' THEN oi.Quantity ELSE
0 END), 0) AS TotalPurchaseQuantity,
IFNULL(SUM(CASE WHEN t.TransactionType = 'Purchase' THEN oi.Subtotal ELSE
0 END), 0) AS TotalPurchaseAmount
FROM Products p
LEFT JOIN OrderItems oi ON p.ProductID = oi.ProductID
LEFT JOIN Orders o ON oi.OrderID = o.OrderID
LEFT JOIN Transactions t ON o.OrderID = t.RelatedEntityID
GROUP BY p.ProductName, p.Category
This query extracts data from OrderItems, Orders, Transactions and Products to display total sales of
products, total sales quantity, total purchase quantity, total purchase of products and category.