---
title: "sql统计相同项个数并按名次显示"
author: "phoenix007"
date: "2008-11-17T00:25:45Z"
tags: ["Oracle", "SQL"]
slug: "sql"
draft: false
lastmod: "2008-11-17T00:26:02Z"
id: 268706
url: "https://phoenix007.iteye.com/blog/268706"
---

现在有如下这样一个表：  
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;
```

 

对这种语句不熟，故写在这里，以便查找。