php - MySQL - Group by for UNION query -


i've been trying find solution problem couple of hours , can't come right query. have 2 tables, stocktake_scans , stocktake_items. need select data both tables , group them together. query have @ moment this:

select  a.department_description, a.sub_department_description, a.total_cost, a.total_retail, sum(a.qty) qty, a.total_vat, a.vat_rate  (      select (case when trim(ifnull(sp.department_description, '')) = '' 'n/a' else sp.department_description end) department_description,      (case when trim(ifnull(sp.sub_department_description, '')) = '' 'n/a' else sp.sub_department_description end) sub_department_description,      sum(sp.unit_cost_price * ss.quantity) total_cost,      sum(sp.unit_retail_price * ss.quantity) total_retail,      sum(ss.quantity) qty,      (sum(sp.unit_cost_price*ss.quantity)) * (sv.vat_rate/100) total_vat,      sv.vat_rate vat_rate               stocktake_scans ss          inner join stocktake_products sp on ss.stocktake_product_id = sp.stocktake_product_id          left join stocktake_vat_codes sv on sv.vat_code = sp.vat_code , sv.stocktake_id = '5778'          ss.stocktake_id = '5778'          group sp.department_description, sp.sub_department_description      union          select (case when trim(ifnull(si.department_description, '')) = '' 'n/a' else si.department_description end) department_description,          'n/a' sub_department_description,          sum(si.unit_cost_price * si.quantity) total_cost,          sum(si.unit_retail_price * si.quantity) total_retail,          sum(si.quantity) qty,         sum(si.unit_cost_price * quantity)*(sv.vat_rate/100) total_vat,          sv.vat_rate vat_rate          stocktake_items si          left join stocktake_vat_codes sv on sv.vat_code = si.vat_code , sv.stocktake_id = '5778'          si.stocktake_id = '5778' group si.department_description )      group          a.department_description, a.sub_department_description 

this doesn't job. data stocktake_scans, followed data stocktake_items.

i below first result image 1

and @ end of excel file this

image 2

what right way of doing this, both alcohol figures displayed in 1 place?

add order a.department_description, a.sub_department_description before last group by


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -