简体   繁体   中英

MySQL query to get the total from three tables

Using MySQL, how do I get the total items and total revenue for each manager's team? Suppose I have these 3 different tables (parent-child-grandchild):

在此处输入图片说明

Employee1 is under Supervisor1, and they are both under Manager1, and so on, but real data are in random arrangement. Color-coded the numbers to visualize which gets added.

I want my query to output the total items and total revenue of each manager's team like:

在此处输入图片说明

To easily create the table:

DROP TABLE IF EXISTS manager;
CREATE TABLE manager (id int, name varchar(55), no_of_items int, revenue int);
INSERT INTO manager (id, name, no_of_items, revenue)
VALUES
    (1  , 'Manager1' ,    10    ,   100), 
    (2  , 'Manager2' ,    20    ,   200),
    (3  , 'Manager3' ,    30    ,   300);

DROP TABLE IF EXISTS supervisor;
CREATE TABLE supervisor (id int, name varchar(55), manager_id int, no_of_items int, revenue int);
INSERT INTO supervisor (id, name, manager_id, no_of_items, revenue)
VALUES
    (4  , 'Sup1' ,    1,   100    ,   1000), 
    (5  , 'Sup2' ,    2,   200    ,   2000),
    (6  , 'Sup3' ,    3,   300    ,   3000);

DROP TABLE IF EXISTS employee;
CREATE TABLE employee (id int, name varchar(55), supervisor_id int, no_of_items int, revenue int);
INSERT INTO employee (id, name, supervisor_id, no_of_items, revenue)
VALUES
    (7  , 'Emp1' ,    4,   400    ,   4000), 
    (8  , 'Emp2' ,    5,   500    ,   5000),
    (9  , 'Emp3' ,    4,   600    ,   6000);

SQL Fiddle

Utilizing a combination of Nested Subqueries and UNION ALL , you can use the following:

SELECT inner_nest.manager_id, 
   inner_nest.name, 
   SUM(inner_nest.total_items) AS total_items,  
   SUM(inner_nest.total_revenue) AS total_revenue 

FROM (
  SELECT id as manager_id, 
         name, 
         SUM(no_of_items) AS total_items, 
         SUM(revenue) AS total_revenue 
  FROM manager
  GROUP BY id  

  UNION ALL 

  SELECT m.id as manager_id, 
         m.name, 
         SUM(s.no_of_items) AS total_items, 
         SUM(s.revenue) AS total_revenue 
  FROM manager m
  INNER JOIN supervisor s ON s.manager_id = m.id 
  GROUP BY m.id  

  UNION ALL 

  SELECT m.id as manager_id, 
         m.name, 
         SUM(e.no_of_items) AS total_items, 
         SUM(e.revenue) AS total_revenue 
  FROM manager m
  INNER JOIN supervisor s ON s.manager_id = m.id 
  INNER JOIN employee e ON e.supervisor_id = s.id 
  GROUP BY m.id  
) AS inner_nest 

GROUP BY inner_nest.manager_id

Try this: http://sqlfiddle.com/#!9/7a0aef/20

select id,name,COALESCE(sum(distinct m),0)+COALESCE(sum(distinct s),0)+COALESCE(sum(e),0)
as total_item,COALESCE(sum(distinct mv),0)+COALESCE(sum(distinct sv),0)+COALESCE(sum(ev),0)
as total_revenue
from
(
select m.id,m.name,m.no_of_items as m,s.no_of_items as s,e.no_of_items as e,
  m.revenue as mv,s.revenue as sv,e.revenue as ev
from manager m left join supervisor s
on m.id=s.manager_id
left join employee e on s.id=e.supervisor_id)a
group by id,name

A single query with the appropriate joins does the trick... In pseudo code (sorry, I'm typing on my phone)

Select a.manager_id, (a.total_items+b.total_items+c.total_items) as totalitems, (a.total_revenue+b.total_revenue+c.total_revenue) as totalrevenue
From parent_table a
Join child_table b on a.manager_id=b.manager.id
Join grandchild_table c on b.sup_id=c.sup_id
Group by a.manager_id

Some adjustments may be needed, but this should definitely point you in your way

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM