a guest Oct 18th, 2019 113 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- SELECT products.id,
- SUM(nin.qty) AS purchased,
- SUM(nout.qty) AS sold
- FROM products
- LEFT OUTER JOIN product_categories ON (products.category_id = product_categories.id)
- LEFT OUTER JOIN nitems nin ON (nin.product_id = products.id AND nin.iinvoice_id IS NOT NULL)
- LEFT OUTER JOIN nitems nout ON (nout.product_id = products.id AND nout.oinvoice_id IS NOT NULL)
- WHERE products.company = 2
- AND products.product_type = 'nonitemised'
- GROUP BY products.id, products.name, product_categories.name, products.product_type
- ORDER BY products.name;
RAW Paste Data