以下是示例模式:
| 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)
3
而不是2
SELECT university_id, COUNT(university_id) FROM table GROUP BY university_id
是否有可能做我想做的事情,如果是这样的话?
答案 0 :(得分:3)
您应该type_id
唯一不是university_id
SELECT university_id, COUNT(DISTINCT type_id) TotalCount
FROM tableName
GROUP BY university_id
答案 1 :(得分:1)
count(distinct)
列需要type_id
:
select university_id, count(distinct type_id)
from table
group by university_id