簡體   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