MySQL - 按总数降序对结果进行排序
我有一个表格如下
年份 -------- 组织 ------- 名称 ---------- 类别 -------- 点数
2005 -------- ABC ---------- N1 ---------- CAT1 -------- 10
2006 -------- DEF ---------- N2 ---------- CAT2 -------- 5
等
该表中的主键是(年份,组织,名称)
我需要如下输出
org ---------- 类别 ------ 点 (按总体点降序排序组织)
DEF ---------- CAT1 ------ 1000
DEF ---------- CAT2 ------ 5000
DEF ---------- CAT3 ------ 2000
ABC ---------- CAT1 ------ 6000
ABC ---------- CAT2 ------ 100
ABC ------- CAT3 ------ 50
DEF 分数为 8000,高于 ABC 的分数 6150。因此出现在在预期输出的顶部,
我编写了一条选择语句,如下所示
select org, cat, count(cat) from table whereyear=2006 group by org, cat order by org
我得到按 排序的结果>org 但我无法按每种类型 org 的总点数降序排列输出,
非常感谢任何帮助。谢谢-普拉文
I have a table as below
year -------- org ------- name ---------- category -------- points
2005 -------- ABC ------- N1 ---------- CAT1 -------- 10
2006 -------- DEF ------- N2 ---------- CAT2 -------- 5
etc
Primary key in this table is (year, org, name)
I need an output as below
org ------- category ------ points (sorted in descending order of overall points of org)
DEF ------- CAT1 ------ 1000
DEF ------- CAT2 ------ 5000
DEF ------- CAT3 ------ 2000
ABC ------- CAT1 ------ 6000
ABC ------- CAT2 ------ 100
ABC ------- CAT3 ------ 50
DEF score is 8000 which is higher than the score of ABC which is 6150. So appears at the top of the expected output
I wrote a select statement as below
select org, cat, count(cat) from table where year=2006 group by org, cat order by org
I get the result ordered by org but I am unable to get the output sorted in descending order of overall count of points of every type of org
Any help is much appreciated. Thanks - Praveen
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 JOIN 将
org
单独分组,并获取每个组的所有计数的SUM
:Use a JOIN to group the
org
alone, and get theSUM
of all the counts for each group:在查询的末尾,您使用 desc 关键字,这将帮助您
At the end of the query u use desc keyword, which will help u out