mysql - How do you combine two sum queries into one? -
select dep.depname, sum(worker.salary) total team, worker worker.depid = dep.depid group dep.depid select dep.depname, sum(manager.salary) total manager, dep manager.depid = dep.depid group team.depid
i tried along line of: select dep.depname, sum(manager.salary)+sum(worker.salary) total gave me strange output, adds n elements n number of time.
the more efficient way sum results department without doing join. join in department name.
this method allows separate department salaries 2 groups:
select depid, sum(wsalary) wsalary, sum(msalary) msalary, sum(wsalary) + sum(msalary) total ((select depid, sum(w.salary) wsalary, null msalary worker w group depid ) union (select depid, null wsalary, sum(m.salary) msalary manager m group depid ) ) t join dep d on t.depid = d.depid group t.depid
i assuming team
in first from
clause refers dep
, used in rest of query.
Comments
Post a Comment