在多个表MYSQL中选择最大总和值的行

时间:2015-08-26 06:22:08

标签: mysql

我需要查询所有项目销售额最高的用户,其中用户在用户表中,销售单位表,项目表中的项目。

Projects     Top Agent    Total Sales for Project
Project A    User A       100000
Project B    User B        20000
Project C    User A         1000
Project D    -                 0

“项目”列列出所有项目,无论其是否有销售。

“最高业务代表”列列出项目中销售额最高的用户。

项目总销售额是项目的总销售额。

我得到的代理列不正确,因为其他人的销售额最高,查询似乎返回结果的第一行

SELECT projects, pid, CASE WHEN agent is null THEN '-' ELSE agent END as agent, 
CASE WHEN FORMAT(topagent,0) > 0 THEN FORMAT(topagent,0) ELSE 0 END as salesvolume 
FROM ( 
SELECT projects.name as projects, projects.id as pid, 
concat(users.f_name, ' ', users.l_name) as agent, 
SUM(units.price) AS topagent 
FROM users inner join bookings on bookings.agent_id = users.id 
inner join units on bookings.unit = units.id 
inner join types on types.id = units.types_id 
inner join projects on projects.id = types.project_id 
WHERE units.status = 'Sold' 
GROUP BY pid 
union 
select projects.name as projects, projects.id as pid, 
concat(users.f_name, ' ', users.l_name) as agent, 
SUM(units.price) AS topagent 
from projects left outer join types on projects.id = types.project_id 
left outer join units on types.id = units.types_id and units.status = 'Sold' 
left outer join bookings on units.id = bookings.unit and units.status = 'Sold' 
left outer join users on bookings.agent_id = users.id and units.status = 'Sold' 
group by pid 
) a 
GROUP BY pid 
order by topagent desc

1 个答案:

答案 0 :(得分:0)

您的列别名难以阅读。在英语中,topagent的意思似乎是“人类的销售总和”。但在SQL中,GROUP BY pid表示SUM(units.price)实际上意味着“项目中的销售总额”。

然后UNIONprojects列表添加到users列表中。此时代理商名称基本上是随机的。

如果我将需求解读为“按每个项目的顶级销售代理的销售价值排名的项目列表”,那么您将拥有以下SQL:

SELECT
  pid,
  projects.name as project_name,
  IFNULL(a.top_agent_name,'-') as top_agent_name,
  CASE WHEN FORMAT(top_agent_sales,0) > 0 THEN FORMAT(top_agent_sales,0) ELSE 0 END as top_agent_salesvolume
FROM
  projects
JOIN
  SELECT
    a.pid,
    a.agent_name as top_agent_name,
    a.agent_sales as top_agent_sales
  FROM
    (SELECT
      projects.id as pid,
      concat(users.f_name, ' ', users.l_name) as agent_name,
      SUM(units.price) AS agent_sales
    FROM users
      inner join bookings on bookings.agent_id = users.id
      inner join units on bookings.unit = units.id
      inner join types on types.id = units.types_id
      inner join projects on projects.id = types.project_id
    WHERE units.status = 'Sold'
    GROUP BY pid, users.id
    ) a # get all agents for all projects
  JOIN
    (SELECT
      MAX(agent_sales) as max_project_agent_sales
    FROM
      (SELECT
        projects.id as pid,
        SUM(units.price) AS agent_sales
      FROM users
        inner join bookings on bookings.agent_id = users.id
        inner join units on bookings.unit = units.id
        inner join types on types.id = units.types_id
        inner join projects on projects.id = types.project_id
      WHERE units.status = 'Sold'
      GROUP BY pid, users.id
      )
    GROUP BY pid) b ON a.pid = b.pid
    WHERE
      a.agent_sales = b.max_project_agent_sales

ORDER BY a.agent_sales desc

以下旧答案:

内部查询中每个topagent有2 pid个,因为它是2 group by s的并集。外部group by pid中没有缩减函数,因此select中返回的topagent是内部查询中出现的第一个。{/ p>