Faisal Hilmi Faisal Hilmi - 2 years ago 56
MySQL Question

How to use GroupBy correctly from multiple joined table

hi im new in database query, i need some help to solve my proble, here is the table imagetable

and this is my query:

SELECT w.name as wholesaler_name, w.outlet_id,w.distributor_name, w.distributor_id, wt.name as tipe_plus, v.target, v.week as periode, sum(v.total_transaction), v.rebate, v.total_voucher
FROM voucher v
JOIN wholesalers w ON w.id=v.wholesaler_id
JOIN wholesaler_types wt ON w.wholesaler_type_id=wt.id
GROUP BY w.name, w.outlet_id, w.distributor_name, w.distributor_id, wt.name, v.target, v.week, v.rebate, v.total_voucher;

in period column, there is 2 same period (11.2017) for one wholesaler name, i want if that happen the total_transaction column will sum.
i use group by but the result is still same.
hope you can help.


Answer Source

chek total_vouchers column.

In your query everething will be ok if total_vouchers are same for 11.2017, because your query finaly step will want grouping by total_vouchers , in your result link i can not see total_vouchers, if they are not same for 11.2017 then exclude that column from group by and select or put that column in Sum(), Count().... (aggregate function) in select Section

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download