I'm having issues getting a total count of items sold. The script below shows me the total sales but and not summing them up. I got it to sum them up but it was listing duplicate invoices so I enter “distinct” before invoice.no. I can't figure out where I'm messing this up. Mind you I have no previous SQL training, I've only gotten this far from reading other readers’ questions here. Please help. Thanks in advance.
SELECT DISTINCT
sh.invoice_no, sr.item_id, sr.item_desc, sr.last_sale, sr.SQ,
sr.DQ, sr.AQ, sr.VQ,sr.PQ, sr.iQOH, sr.avg_MAC, si.cost,
sr.PT, si.item_id AS Item, SUM(sh.qty_shipped) AS QTY_Shipped
FROM iml_view_item_stock_review_nh as sr
INNER JOIN p21_view_inventory_supplier as si ON sr.item_id = si.item_id
INNER JOIN p21_sales_history_view as sh ON sr.item_id = sh.item_id
WHERE (sr.item_id LIKE 'IEI%')
AND (sr.PT = 'R')
AND (sh.invoice_date > CONVERT(DATETIME, '2013-10-17 00:00:00', 102))
GROUP BY
sr.item_id, sr.item_desc, sr.last_sale,
sr.SQ, sr.DQ, sr.AQ, sr.VQ,
sr.PQ, sr.iQOH, sr.avg_MAC, si.cost,
sr.PT, si.item_id, sh.invoice_no
Below is a sample of the data. I omitted some of the other columns. Hope this helps.
|invoice_no | item_id | QTY_Shipped |
|-----------------------------------------|
| 1000616 | IEI0803000F | 4 |
| 1001289 | IEI0803000F | 2 |
| 1018242 | IEI0803000F | 2 |
| 1001224 | IEI212I | 2 |
| 1002265 | IEI212I | 2 |
| 1003547 | IEI212I | 2 |
The Expected output is something like:
| item_id | QTY_Shipped |
|--------------|--------------|
| IEI0803000F | 8 |
| IEI212I | 6 |
Sorry if I don't have much knowledge. My company is sending me to get trained and certified in January and soon I'll be helping others. Thanks in advance.