0

我有一个电话号码字段,例如:

**phonenumbers**
0729643482
0723412678
0734231567
0745297334
0729643482
0720606706
0729643482
0720606706

有成千上万的条目。我想获得数量最多的前 10 个电话号码。这可以显示为

**phonenumber     count**
0729643482      3
0720606706      2
.
.
.
(entry 10)      1

从我了解到的一些相关问题中,我可以使用 rank() 然后分组,但我以前从未这样做过。这是我所拥有的:

select phonenumber,cnt FROM 
(select phonenumber, cnt, rank() over (partition by phonenumber order by cnt desc) rnk
from (select distinct phonenumber, count(phonenumber) cnt
            from ozekiout
            group by phonenumber
            order by phonenumber, count(phonenumber) desc) 
)
where rnk = 1;
4

3 回答 3

2

您不需要 rank 功能,您可以使用正常计数TOP 10

SELECT  TOP 10 phonenumber, [count] = COUNT(*)
FROM    ozekiout
GROUP BY Phonenumber
ORDER BY [count] DESC;

如果您想包含超过 10 个结果(如果有平局),例如

Phonenumber  count
01111111111   18
01111111112   15
01111111113   15
01111111114   14
01111111115   13
01111111116   13
01111111117   12
01111111118   12
01111111119   10
01111111120   10
01111111121   10
01111111122   10

.... CUT OFF

01111111122   9

您可以使用:

SELECT  TOP 10 WITH TIES phonenumber, [count] = COUNT(*)
FROM    ozekiout
GROUP BY Phonenumber
ORDER BY [count] DESC;
于 2013-09-12T09:59:51.407 回答
0

SELECT TOP 10 phonenumber, [count] = COUNT(*) FROM ozekiout GROUP BY Phonenumber ORDER BY [count] DESC;

工作ryt....谢谢你们

于 2013-09-12T10:17:20.583 回答
0

尝试这个 :

select phonenumber,Count(*) as count from ozekiout group by phonenumber order by count desc limit 10;
于 2013-09-12T10:04:25.243 回答