I need help to build a ORACLE SQL query that can give me Sales by Item Name for each month. Here are the columns that are needed in query – Date (month), Item Name, Quantity Sold, Average Unit Price, Net Sales , Credit units
Quantity Sold
of an Item is the net quantity sold after credits for a particular item in that month; that is =SUM(Invoice Items. Quantity sold) – SUM(Credit Note Items. credit Quantity )
Net Sales
of an item in a particular month = SUM(Invoice Items.Total Amount) - SUM(Credit Note Items. Credit Amount)
Credit units : it refers to total units of item(credit Quantity) that have been credited in that month
Below are tables:
Invoice Items (Invoice Date, Invoice ID, Product ID, Item Name, Quantity sold, Total Amount)
Inventory Adjustment (Date, Entity, EntityID, ItemID, Item Name, Quantity, Unit Price, Stock On Hand, Net Amount) This table holds unit price and quantity of an item when invoice or credit note for that item was generated. Entity can be 'Invoice' or 'CreditNote'. EntityID is Invoice ID or CreditNotes ID
Credit Notes (CreditNote Date, CreditNotes ID, Customer ID)
Credit Note Items (CreditNotes ID, Item ID, Item Name, credit Quantity, Credit Amount) Note: this is line item table for Credit Notes table. CreditNote Date is available in master table
So far, I have this query but I'm unable to figure out next step of joining with credit note and credit note items and getting Net Sales and Quantity Sold.
SELECT
inv_items."Invoice Date" inv_date,
inv_items."Product ID" inv_items_Prod_ID,
inv_items.Item Name,
AVG(IDR."Unit Price"),
SUM(inv_items."Quantity sold") total_Qty_Sold,
SUM(inv_items."Total Amount") inv_ItemsSale
FROM "Invoice Items" inv_items
JOIN "Inventory Adjustment" IDR ON IDR."EntityID" = inv_items."Invoice ID"
AND IDR."ItemID" = inv_items."Product ID"
AND IDR."Date" = inv_items."Invoice Date"
GROUP BY 1,
2,
3
Any help is much appreciated.
group by 1, 2, 3
may work in other database products (I think I read it does), but it doesn't work in Oracle.order by 1, 2, 3
works, but for grouping you must refer to the columns by name, not by numbers. – mathguy