Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在寻找一个总结一些特定值并对结果进行排序的 SQL 语句。更清楚地说:我有一个由标识符和值组成的表:
id val ab 10 ab 12 ab 3 cd 25 cd 10 ef 2 ef 7
这里 ab、cd 和 ef 的所有值都必须按结果求和并排序,以便得到以下结果:
cd 35 ab 25 ef 9
那么是否有一个 SQL 语句可以一次性执行该任务?
SELECT id, SUM(val) as total FROM your_table GROUP BY id ORDER BY total DESC;
使用群组功能sum:
sum
select id, sum(val) as val from my_table group by id order by 2 desc