繁体   English   中英

选择行以在多个表中获得最大总和值MYSQL

[英]Select Row for Max Sum Value In Multiple Tables 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

您的列别名令人难以理解。 用英语来说, 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

下面的旧答案:

内部查询中每个pid都有2个topagent ,因为它是2 group by的并集。 外部group by pid没有topagent函数,因此topagent返回的topagent是内部查询中出现的第一个topagent

如果有帮助,请尝试一下-

SELECT a.prjname, IFNULL(usr.name,'-') AS Top_Agent, SUM(a.sale) AS Total_Sales_for_Project
FROM 
(
SELECT prj.id AS prjid,prj.name AS prjname,usr.id,usr.name AS usrname,IFNULL(SUM(unit.price),0) AS sale
FROM projects AS prj 
LEFT JOIN `types` AS typ ON typ.project_id=prj.id 
LEFT JOIN units AS unt ON unt.type_id=typ.id AND unt.status='sold'
LEFT JOIN bookings bkg ON bkg.unit=unt.id 
LEFT JOIN users usr ON usr.id=bkg.agent_it 
GROUP BY prj.id,usr.id 
ORDER BY prj.id,usr.id,sale DESC 
) a 
GROUP BY a.prjid

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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