我使用的是SQL Server 2012,我有这样的表
| Country | Age |
+---------+------+
| SWEDEN | 43 |
| SWEDEN | 17 |
| SWEDEN | 43 |
| SWEDEN | 43 |
| SWEDEN | 17 |
| GERMANY | 17 |
| GERMANY | 17 |
| GERMANY | 17 |
| GERMANY | 44 |
| GERMANY | 44 |
| SWEDEN | 43 |
| SWEDEN | 17 |
| GERMANY | 13 |
| SWEDEN | 17 |
| SWEDEN | 43 |
我可以像这样在乡下数年龄
| Country | Age | CountOfAge |
+----------+------+------------+
| GERMANY | 13 | 1 |
| GERMANY | 17 | 3 |
| SWEDEN | 17 | 4 |
| SWEDEN | 43 | 5 |
| GERMANY | 44 | 2 |
我想在这样国家里达到最大年龄
| Country | Age | CountOfAge |
+----------+------+------------+
| GERMANY | 17 | 3 |
| SWEDEN | 43 | 5 |
我尝试使用以下SQL语句:
SELECT
X.country, X.age, X.countOfAge
FROM
(SELECT country, age, COUNT(age) AS countOfAge
FROM MOCK
GROUP BY country, age) X
我计算了年龄,但无法筛选计算的年龄的最大值
1条答案
按热度按时间sd2nnvve1#
在这种情况下,RANK()或DENSE_RANK()都将提供相同的结果,但请阅读它们的文档以了解两者之间行为的差异-特别是RANK()返回的数字中的差距。