简体   繁体   中英

Select distinct count over two columns

I have data that looks like this:

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

I want to count the unique classes offered at each individual high school. For example, I want to see:

   count   school
    3       CANYON HIGH
    2       WAYNE HIGH

How would I go about doing this? I understand the concept of one column, but how about two?

Try this:

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

I am unsure what constitutes a unique class.

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

Since the same school name can be used across districts, I included the district in the grouping.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM