You are on page 1of 2

SELECT

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.

This gives insight as to which product is contributing more to revenue


SELECT
o.OrderID,
GROUP_CONCAT(p.ProductName SEPARATOR ', ') AS ProductsOrdered,
pt.PartnerName,
t.TransactionType,
o.OrderDate,
o.TotalAmount AS GrandTotal
FROM Orders o
JOIN OrderItems oi ON o.OrderID = oi.OrderID
JOIN Products p ON oi.ProductID = p.ProductID
JOIN Partners pt ON o.PartnerID = pt.PartnerID
LEFT JOIN Transactions t ON o.OrderID = t.RelatedEntityID AND
t.TransactionType = 'Sales'
WHERE o.OrderID = ?
GROUP BY o.OrderID

This query extract data from Orders, Orderitems, Products, Partners,


Transactions to create invoice. Invoice contains following fields:
Products ordered, Partner name, Transaction type, Order date, Grand total.

You might also like