2013-05-02 48 views
0

下面是一個示例模式:選擇值的計數,由同一列編組,不同的另一

| type_id | university_id | 
--------------------------- 
| 1 |  u01  | 
| 2 |  u01  | 
| 2 |  u01  | 
| 3 |  u02  | 
| 4 |  u02  | 

而且我想是的具有相同university_id記錄數的計數中,university_id本身,它不能包括type_id重複的,所以結果看起來像這樣由於上述相同的數據:

| university_id | COUNT(university_id) | 
---------------------------------------- 
|  u01  |   2   | 
|  u02  |   2   | 

現在,我嘗試以下,但它顯然給我一個COUNT(u01)Ø ˚F3代替2

SELECT university_id, COUNT(university_id) FROM table GROUP BY university_id 

是否可以做我想做的,如果又如何?

回答

3

你應該type_id算唯一沒有被university_id

SELECT university_id, COUNT(DISTINCT type_id) TotalCount 
FROM tableName 
GROUP BY university_id 
1

您在type_id列需要count(distinct)

select university_id, count(distinct type_id) 
from table 
group by university_id 
相關問題