简体   繁体   English

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

[英]Select Row for Max Sum Value In Multiple Tables MYSQL

I need to query for the users with highest amount of sales by all projects, where the users are in users table, sales in units table, projects in projects table. 我需要查询所有项目中销售量最高的用户,其中用户在用户表中,销售在单位表中,项目在项目表中。

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

The Projects column is list all the projects regardless it has sales or not. “项目”列列出了所有项目,无论其是否销售。

The Top Agent column is list the user with the highest sales in the project. 顶级代理商列是列出项目中销售额最高的用户。

The Total Sales for Project is the total sales for a projects. 项目的总销售额是项目的总销售额。

The agent column i got is incorrect because there is someone else has the highest sales, the query seems to return the first row of the result 我得到的座席列不正确,因为有其他人的销售额最高,查询似乎返回了结果的第一行

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

Your column aliases are confusing to read. 您的列别名令人难以理解。 In English, it seems what you mean by topagent is "sum of sales by a human". 用英语来说, topagent意思似乎是“人类的销售额之和”。 But in SQL, your GROUP BY pid means that the SUM(units.price) really means "sum of sales in a project". 但是在SQL中,您的GROUP BY pid表示SUM(units.price)实际含义是“项目的销售总额”。

Then the UNION adds a list of projects to a list of users . 然后, UNIONprojects列表添加到users列表。 The agent names are basically random at this point. 此时,座席名称基本上是随机的。

If I decipher the requirements as "a list of projects ranked by the sales values of each project's top sales agent", then you'd have SQL as below: 如果我将需求解释为“按每个项目的主要销售代理商的销售值排名的项目列表”,那么您将拥有如下所示的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

Old answer below: 下面的旧答案:

There are 2 topagent s for each pid in the inner query since it's a union of 2 group by s. 内部查询中每个pid都有2个topagent ,因为它是2 group by的并集。 There isn't a reducing function in the outer group by pid so the topagent returned in the select is the first one that came up in the inner query. 外部group by pid没有topagent函数,因此topagent返回的topagent是内部查询中出现的第一个topagent

Try it if helps you- 如果有帮助,请尝试一下-

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