子查询SQL Count函数

时间:2012-10-04 02:18:41

标签: sql count

Jurisdiction      Route
------------      -----
Portland           65
Portland           55
Cornelius          75
Cornelius          75
Cornelius          75

我如何计算科尼利厄斯的路线数量?答案是1

2 个答案:

答案 0 :(得分:2)

您只需使用COUNT()DISTINCT

即可
select count(distinct Jurisdiction) total, Jurisdiction
from table1
where Jurisdiction = 'Cornelius'
group by Jurisdiction

请参阅SQL Fiddle with Demo

答案 1 :(得分:0)

SELECT COUNT(*) 
FROM your_table 
GROUP BY Jurisdiction, Route 
WHERE Jurisdiction = 'Cornelius'

或者

SELECT COUNT(DISTINCT Route) 
FROM your_table 
GROUP BY Jurisdiction 
WHERE Jurisdiction = 'Cornelius'