如何根据属性分组订购点列?
例如:
degreeid studentid point
1 1 100
2 1 150
3 1 205
4 1 600
1 2 520
2 2 566
3 2 450
4 2 566
5 2 450
......
1 n m
我想以这种方式显示查询结果:
degreeid studentid point
1 2 520
1 1 100
1 n 90 --( min value)
2 2 566
2 1 150
2 n 70 --( min value)
我使用光标,为每个学位制作
ORDER point DESC
和结果
INSERT INTO #temporary TABLE
此临时表用于显示数据。
这是一个好方法吗,因为我有很多数据,大约有500和3000条记录?
答案 0 :(得分:1)
按以下格式使用订购
SELECT * FROM TABLE ORDER BY degreeid ASC,point DESC;
示例
mysql> SELECT user,duration FROM tt order by USER ASC,duration DESC;
+--------+----------+
| user | duration |
+--------+----------+
| user1 | 00:06:00 |
| user1 | 00:02:00 |
| user2 | 00:06:00 |
| user2 | 00:01:00 |
| user3 | 00:04:00 |
| user55 | 00:01:00 |
| user55 | 00:01:00 |
+--------+----------+
7 rows in set (0.00 sec)
答案 1 :(得分:1)
如果没有您的查询很难知道,但我想您想订购2个标准:
select degreeid ,studentid ,point
from table
order by degreeid , point desc
如果您正在分组,order by
是相同的:
select degreeid ,studentid ,sum(point) as sum_point
from table
group by degreeid ,studentid
order by degreeid , sum_point desc