如何从派生的 table 中 SELECT 具有 MAX(value) 的行,其中值都是计算的总和?
How can I SELECT a row with MAX(value) from a derived table where the values are all calculated sums?
我尝试了以下代码,但我只得到了包含所有名称和总和值的完整 table,而不是包含最大值的一行:
SELECT stageName, max(total_salary)
FROM (
SELECT c.*, sum(p.dailySalary) as total_salary
from contender as c
left join participant as p
on (p.contender = c.idContender)
group by c.idContender ) b
group by stageName;
output:
Yellow Jesters 205
TikTok 3073
Teabags 947
Bobbleheads 11840
Reddit 1486
但我只需要:
摇头娃娃 11840
PS: 请提出一个不使用 desc 和 limit
的解决方案
如果您只想在结果集中排在首位,您可以排序和限制:
select c.*, sum(p.dailySalary) as total_salary
from contender as c
left join participant as p on p.contender = c.idContender
group by c.idContender
order by total_salary desc
limit 1
如果有顶平的可能,而你又想允许,可以使用window函数:
select *
from (
select
c.*,
sum(p.dailySalary) as total_salary,
rank() over(order by sum(p.dailySalary) desc) rn
from contender as c
left join participant as p on p.contender = c.idContender
group by c.idContender
) t
where rn = 1
这是一个适用于任何版本 MySQL 5.x 的解决方案,不使用 ORDER BY、LIMIT、window 函数、视图或 CTE。
SELECT a.stagename, a.total_salary
FROM (
SELECT c.*, sum(p.dailySalary) as total_salary
from contender as c
left join participant as p
on (p.contender = c.idContender)
group by c.idContender ) AS a
LEFT OUTER JOIN (
SELECT c.*, sum(p.dailySalary) as total_salary
from contender as c
left join participant as p
on (p.contender = c.idContender)
group by c.idContender ) AS b
ON a.total_salary < b.total_salary
WHERE b.total_salary IS NULL;
测试于 MySQL 5.7.27.
输出:
+-------------+--------------+
| stagename | total_salary |
+-------------+--------------+
| Bobbleheads | 11840 |
+-------------+--------------+
我尝试了以下代码,但我只得到了包含所有名称和总和值的完整 table,而不是包含最大值的一行:
SELECT stageName, max(total_salary)
FROM (
SELECT c.*, sum(p.dailySalary) as total_salary
from contender as c
left join participant as p
on (p.contender = c.idContender)
group by c.idContender ) b
group by stageName;
output:
Yellow Jesters 205
TikTok 3073
Teabags 947
Bobbleheads 11840
Reddit 1486
但我只需要: 摇头娃娃 11840
PS: 请提出一个不使用 desc 和 limit
的解决方案如果您只想在结果集中排在首位,您可以排序和限制:
select c.*, sum(p.dailySalary) as total_salary
from contender as c
left join participant as p on p.contender = c.idContender
group by c.idContender
order by total_salary desc
limit 1
如果有顶平的可能,而你又想允许,可以使用window函数:
select *
from (
select
c.*,
sum(p.dailySalary) as total_salary,
rank() over(order by sum(p.dailySalary) desc) rn
from contender as c
left join participant as p on p.contender = c.idContender
group by c.idContender
) t
where rn = 1
这是一个适用于任何版本 MySQL 5.x 的解决方案,不使用 ORDER BY、LIMIT、window 函数、视图或 CTE。
SELECT a.stagename, a.total_salary
FROM (
SELECT c.*, sum(p.dailySalary) as total_salary
from contender as c
left join participant as p
on (p.contender = c.idContender)
group by c.idContender ) AS a
LEFT OUTER JOIN (
SELECT c.*, sum(p.dailySalary) as total_salary
from contender as c
left join participant as p
on (p.contender = c.idContender)
group by c.idContender ) AS b
ON a.total_salary < b.total_salary
WHERE b.total_salary IS NULL;
测试于 MySQL 5.7.27.
输出:
+-------------+--------------+
| stagename | total_salary |
+-------------+--------------+
| Bobbleheads | 11840 |
+-------------+--------------+