我要做的是基本上有一个基于两列的3列表。
id, value
----------
1, 7
1, 1
2, 6
3, 9
3, 2
4, 2
我想从[上方] ...... [/ p>] [下方]
id, sum_of_strength, max_of_strength
---------------------------------------
1, 8, 11
2, 6, 11
3, 11, 11
4, 2, 11
以下是我当前正在玩的内容,但max_value
与sum_value
相同...有没有人知道是否有办法执行此操作。我尝试了很多其他解决方案但没有任何效果。
SELECT
sum_table.id,
sum_table.sum_value,
(SELECT MAX(sum_table.sum_value) AS max_value) AS max_value
FROM (
SELECT
original_table.id,
CAST(SUM(original_table.value) AS UNSIGNED) AS sum_value
FROM original_table
GROUP BY original_table.id
) AS sum_table
GROUP BY sum_table.id;
答案 0 :(得分:2)
You can use a subquery to get the max value:
SELECT id,
CAST(SUM(value) AS UNSIGNED) AS sum_value,
(SELECT CAST(SUM(value) AS UNSIGNED) AS s
FROM original_table
GROUP BY id
ORDER BY s DESC
LIMIT 1) AS max_value
FROM original_table
GROUP BY id
Grouping has to be applied twice:
value
per id