Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to count distinct values with a composite primary key

I have to generate a report that displays the name of different locations and the number of times unique course offerings are offered in these locations. The report needs to only indicate the city and the no. of unique course offerings and be sorted by the highest number of unique offerings. The problem I have is the offering table has a composite primary key (offering begin date and course code)

I run this SQL query and it comes up with an error as you can't count multiple columns. But I am not sure how to split the count.

SELECT 
    offlocation AS city, COUNT(distinct offbegindate,crscode) as no. of unique course offerings
FROM
    offering
GROUP BY offlocation
ORDER BY COUNT(distinct offbegindate,crscode) as no. of unique course offerings DESC;

Any help will be greatly appreciated.

like image 368
Daniel Morris Avatar asked Apr 28 '15 08:04

Daniel Morris


1 Answers

Just minor adjustments, "Row types" for count distinct:

SELECT 
    offlocation AS city,
    COUNT(distinct (offbegindate,crscode)) as no_of_unique_course_offerings
FROM
    offering
GROUP BY offlocation
ORDER BY no_of_unique_course_offerings DESC;
like image 83
jarlh Avatar answered Oct 17 '22 01:10

jarlh