sql - How to get the totals of each column MySQL -
i have sql query number of jobs, gross weight etc different companies.
the result in mysql workbench looks this:
i add row @ bottom calculates total of each column i.e total of jobs column 267. how add row totals?
sql query:
select c.name, count(distinct jn.id) jobs, sum(jn.actualweight) grosswt, sum(jpc.advisedqty) units_in, sum(jpd.advisedqty) units_out customer c left join job_new jn on jn.jobownerid = c.id left join job_pieces jp on jp.jobid = jn.id left join job_pieces jpc on jpc.jobid = jn.id , jn.collectmemberid = jn.jobownerid left join job_pieces jpd on jpd.jobid = jn.id , jn.delivermemberid = jn.jobownerid jn.isactive = 1 group jn.jobownerid
you looking group by
modifier with rollup
:
select c.name, count(distinct jn.id) jobs, sum(jn.actualweight) grosswt, sum(jpc.advisedqty) units_in, sum(jpd.advisedqty) units_out customer c left join job_new jn on jn.jobownerid = c.id left join job_pieces jp on jp.jobid = jn.id left join job_pieces jpc on jpc.jobid = jn.id , jn.collectmemberid = jn.jobownerid left join job_pieces jpd on jpd.jobid = jn.id , jn.delivermemberid = jn.jobownerid jn.isactive = 1 group c.name rollup;
note: changed group by
key 2 reasons. first, should match column used in select
. second, using value left join
ed table, value null
. undesirable.
Comments
Post a Comment