@Cesar
2015-12-18T10:17:05.000000Z
字数 643
阅读 1681
sql
学习
在sql数据库中将某张表A按字段a2进行分组,并取a1最大的五位数据
a1 | a2 | a3 | a4 |
---|---|---|---|
1 | 1 | 1 | 1 |
2 | 1 | 12 | 1 |
3 | 1 | 13 | 1 |
4 | 2 | 14 | 12 |
5 | 2 | 15 | 2 |
6 | 2 | 16 | 33 |
7 | 3 | 17 | 44 |
a1 | a2 | a3 | a4 |
---|---|---|---|
1 | 1 | 1 | 1 |
2 | 1 | 12 | 1 |
4 | 2 | 14 | 12 |
5 | 2 | 15 | 2 |
7 | 3 | 17 | 44 |
该语句的难点在于
SELECT * FROM A X
WHERE (
SELECT COUNT(*) FROM A Y
WHERE X."a2"=Y."a2" AND X."a1">y."a1"
)
<2 ;