开发者

How to order recordset by number of occurrences of a field value and sort by it

I'm running a query select * from transactions which gives me the recordsets

ppno ---------name---------------amnt
1309 ---------Rajasekar----------12000
1210 ---------Mani     ----------21000
1309 ---------Sank     ----------90012
1100 ---------still    ----------12232
1309 ---------Jack     ----------23344

I want to group the resultset by number of occurrences of the same ppno and sort it based on the number of occurrences. For example, I want something like this.

ppno ---------name---------------amnt
1309 ---------Rajasekar----------12000
1309 ---------Sank     ----------90012
1309 ---------Jack     ----------23344
1210 ----开发者_运维问答-----Mani     ----------21000    
1100 ---------still    ----------12232


select t.*
from transactions t
order by (
    select count(1) from transactions t2 where t2.ppno = t.ppno
) desc, t.ppno desc


SELECT t.ppno, t.name, t.amnt
    FROM (SELECT ppno, COUNT(*) AS ppnoCount
              FROM transactions
              GROUP BY ppno) c
        INNER JOIN transactions t
            ON c.ppno = t.ppno
    ORDER BY c.ppnoCount DESC, t.ppno
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜