`

sql统计相同项个数并按名次显示

 
阅读更多

现在有如下这样一个表:
A

ID Name time
------------------------------
0001 aaa 2006-11-18
0002 ccc 2006-11-18
0003 eee 2006-11-18
0004 aaa 2006-11-18
0005 eee 2006-11-18
0004 aaa 2006-11-18
0002 ccc 2006-11-19

我现想实现的统计结果如下表

按日期当天(2006-11-18)统计后为:

Name
次数 排名(按出现的次数)

-------------------------------------------
aaa 3 1
ccc 1 3
eee 2 2

 

oracle sql语句如下:

 select aa.*, rownum as mc

from   ( 

       select name,count(id) as cnt

       from ncsuser.sql_example 

       where time=to_date('2006-11-18','YYYY-MM-DD') 

       group by name 

       order by cnt desc 

    ) aa 

order by name; 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics