如何对一列的值求和,然后使用 SQL 按另一列排序?
How to SUM values of a column and then order by another column using SQL?
当我 SELECT * FROM table;
我有以下结果:
+-------+--------+-------------+-------+
| state | number | candidate | votes |
+-------+--------+-------------+-------+
| AR | 12 | CANDIDATE A | 9 |
| AR | 12 | CANDIDATE A | 23 |
| LA | 12 | CANDIDATE A | 19 |
| OK | 12 | CANDIDATE A | 2 |
| TX | 12 | CANDIDATE A | 7 |
| TX | 12 | CANDIDATE A | 7 |
| AR | 25 | CANDIDATE B | 2 |
| LA | 25 | CANDIDATE B | 5 |
| LA | 25 | CANDIDATE B | 1 |
| OK | 25 | CANDIDATE B | 17 |
| OK | 25 | CANDIDATE B | 21 |
| TX | 25 | CANDIDATE B | 7 |
| LA | 17 | CANDIDATE C | 14 |
| LA | 17 | CANDIDATE C | 42 |
| OK | 17 | CANDIDATE C | 13 |
| OK | 17 | CANDIDATE C | 5 |
| TX | 17 | CANDIDATE C | 1 |
| TX | 17 | CANDIDATE C | 4 |
+-------+--------+-------------+-------+
所以我尝试将总票数与 SUM(votes) as tt_votes
相加,然后将结果按候选人分组以获得 desc 顺序。
SELECT state, number, name, SUM(votes) as tt_votes
FROM table
GROUP BY candidate
ORDER BY tt_votes DESC;
+-------+--------+-------------+----------+
| state | number | candidate | tt_votes |
+-------+--------+-------------+----------+
| LA | 17 | CANDIDATE C | 79 |
| AR | 12 | CANDIDATE A | 67 |
| TX | 25 | CANDIDATE B | 53 |
+-------+--------+-------------+----------+
state
列显示候选人的第一个(或最后一个?)记录,但我真正想要的不仅是按候选人分组,而且按州分组首先有一个结果是这样的:
+-------+--------+-------------+----------+
| state | number | candidate | tt_votes |
+-------+--------+-------------+----------+
| AR | 12 | CANDIDATE A | 32 |
| AR | 25 | CANDIDATE B | 2 |
| AR | 17 | CANDIDATE C | 0 |
| LA | 17 | CANDIDATE C | 56 |
| LA | 12 | CANDIDATE A | 19 |
| LA | 25 | CANDIDATE B | 6 |
| OK | 25 | CANDIDATE B | 38 |
| OK | 17 | CANDIDATE C | 18 |
| OK | 12 | CANDIDATE A | 2 |
| TX | 12 | CANDIDATE A | 14 |
| TX | 25 | CANDIDATE B | 7 |
| TX | 17 | CANDIDATE C | 5 |
+-------+--------+-------------+----------+
它按字母顺序列出各州,然后每个州的每个候选人的 tt_votes 按 tt_votes 排序。
尝试过 UNION 或插入 WHERE state = AR
等,但没有成功。
有什么想法可以做到吗?
编辑
如果我使用 (SELECT state, ANY_VALUE(number), candidate, SUM(votes) as tt_votes WHERE state = 'AR' GROUP BY state, number , candidate) UNION ((SELECT state, ANY_VALUE(number), candidate, SUM(votes) as tt_votes WHERE state = 'LA' GROUP BY state, number, candidate) UNION (SELECT state, ANY_VALUE(number), candidate, SUM(votes) as tt_votes WHERE state = 'OK' GROUP BY state, number, candidate); 等我有我们需要的东西。但是有更好的方法吗?
SELECT state, ANY_VALUE(number), candidate, SUM(votes) AS tt_votes
FROM table
GROUP BY state, candidate
ORDER BY state, candidate;
当我 SELECT * FROM table;
我有以下结果:
+-------+--------+-------------+-------+
| state | number | candidate | votes |
+-------+--------+-------------+-------+
| AR | 12 | CANDIDATE A | 9 |
| AR | 12 | CANDIDATE A | 23 |
| LA | 12 | CANDIDATE A | 19 |
| OK | 12 | CANDIDATE A | 2 |
| TX | 12 | CANDIDATE A | 7 |
| TX | 12 | CANDIDATE A | 7 |
| AR | 25 | CANDIDATE B | 2 |
| LA | 25 | CANDIDATE B | 5 |
| LA | 25 | CANDIDATE B | 1 |
| OK | 25 | CANDIDATE B | 17 |
| OK | 25 | CANDIDATE B | 21 |
| TX | 25 | CANDIDATE B | 7 |
| LA | 17 | CANDIDATE C | 14 |
| LA | 17 | CANDIDATE C | 42 |
| OK | 17 | CANDIDATE C | 13 |
| OK | 17 | CANDIDATE C | 5 |
| TX | 17 | CANDIDATE C | 1 |
| TX | 17 | CANDIDATE C | 4 |
+-------+--------+-------------+-------+
所以我尝试将总票数与 SUM(votes) as tt_votes
相加,然后将结果按候选人分组以获得 desc 顺序。
SELECT state, number, name, SUM(votes) as tt_votes
FROM table
GROUP BY candidate
ORDER BY tt_votes DESC;
+-------+--------+-------------+----------+
| state | number | candidate | tt_votes |
+-------+--------+-------------+----------+
| LA | 17 | CANDIDATE C | 79 |
| AR | 12 | CANDIDATE A | 67 |
| TX | 25 | CANDIDATE B | 53 |
+-------+--------+-------------+----------+
state
列显示候选人的第一个(或最后一个?)记录,但我真正想要的不仅是按候选人分组,而且按州分组首先有一个结果是这样的:
+-------+--------+-------------+----------+
| state | number | candidate | tt_votes |
+-------+--------+-------------+----------+
| AR | 12 | CANDIDATE A | 32 |
| AR | 25 | CANDIDATE B | 2 |
| AR | 17 | CANDIDATE C | 0 |
| LA | 17 | CANDIDATE C | 56 |
| LA | 12 | CANDIDATE A | 19 |
| LA | 25 | CANDIDATE B | 6 |
| OK | 25 | CANDIDATE B | 38 |
| OK | 17 | CANDIDATE C | 18 |
| OK | 12 | CANDIDATE A | 2 |
| TX | 12 | CANDIDATE A | 14 |
| TX | 25 | CANDIDATE B | 7 |
| TX | 17 | CANDIDATE C | 5 |
+-------+--------+-------------+----------+
它按字母顺序列出各州,然后每个州的每个候选人的 tt_votes 按 tt_votes 排序。
尝试过 UNION 或插入 WHERE state = AR
等,但没有成功。
有什么想法可以做到吗?
编辑
如果我使用 (SELECT state, ANY_VALUE(number), candidate, SUM(votes) as tt_votes WHERE state = 'AR' GROUP BY state, number , candidate) UNION ((SELECT state, ANY_VALUE(number), candidate, SUM(votes) as tt_votes WHERE state = 'LA' GROUP BY state, number, candidate) UNION (SELECT state, ANY_VALUE(number), candidate, SUM(votes) as tt_votes WHERE state = 'OK' GROUP BY state, number, candidate); 等我有我们需要的东西。但是有更好的方法吗?
SELECT state, ANY_VALUE(number), candidate, SUM(votes) AS tt_votes
FROM table
GROUP BY state, candidate
ORDER BY state, candidate;