选择两列上的非重复计数

时间:2016-12-27 18:05:08

标签: sql sql-server-2012 ssms

我的数据如下:

school       district        crs_sbj  crs_num  crs_sec
CANYON HIGH  IRON DISTRICT   ENGL     2010     213
CANYON HIGH  IRON DISTRICT   ENGL     2010     214
CANYON HIGH  IRON DISTRICT   ENGL     1010     110
CANYON HIGH  IRON DISTRICT   MATH     1010     400
WAYNE HIGH   WAYNE DISTRICT  MATH     1010     321
WAYNE HIGH   WAYNE DISTRICT  MATH     1010     322
WAYNE HIGH   WAYNE DISTRICT  ENGL     1010     500

我想计算每所高中提供的独特课程。 例如,我想看看:

   count   school
    3       CANYON HIGH
    2       WAYNE HIGH

我该怎么做呢?我理解一栏的概念,但两个怎么样?

3 个答案:

答案 0 :(得分:1)

试试这个:

select school, count(distinct crs_num) _count
from table 
group by school;

答案 1 :(得分:0)

Select School, count(distinct crs_num)
from  table
group by School

答案 2 :(得分:0)

我不确定什么是独特的课程。

; with aardvark (select distinct school, district, crs_sbj, crs_num
    from T)
select district, school, count(*)
from aardvark
group by school, district

由于相同的学校名称可以跨区域使用,我将该区域包括在分组中。