4

So here is my query so far:

Select 
    class.title, studentClass.grade, count(studentClass.grade) 
from 
    classOffering 
inner join 
    studentClass on classOffering.classID = studentClass.classID
inner join 
    class on classOffering.classID = class.classID
group by 
    class.title, studentClass.grade
order by 
    count(studentClass.grade) desc

And here is the output:

enter image description here

Now what I am trying to do is only get back the most frequent grade for a class. So I need to cut out software development I with the grade of a B, because A is the most frequent grade in the class. But I do not know how to compare the count values. Any help would be wonderful.

0

4 Answers 4

1

If you dont need the count (based on your comments) you could try something like this

select a.title,a.grade from 
(Select class.title, studentClass.grade, 
row_number() over (partition by class.title order by studentClass.grade) as rn 
from classOffering inner join studentClass on classOffering.classID = studentClass.classID
inner join class on classOffering.classID = class.classID
group by class.title, studentClass.grade)a
where a.rn=1;

The below query will also get the count

select a.title,a.grade,a.gradeCount from 
    (Select class.title, studentClass.grade,
     count(studentClass.grade) over (partition by class.title) as gradeCount 
    row_number() over (partition by class.title order by studentClass.grade) as rn 
    from classOffering inner join studentClass on classOffering.classID = studentClass.classID
    inner join class on classOffering.classID = class.classID
    group by class.title, studentClass.grade)a
    where a.rn=1;
3
  • I like this one a lot. I had a DB admin show this solution to me a while back, and promptly forgot about it. I really need to commit the row_number() over (partition by ... order by ...) syntax to memory.
    – jwatts1980
    Apr 24, 2016 at 5:51
  • Hmm, I just noticed that you are not get the count of the grade occurrences. That is one of the keys to the required answer.
    – jwatts1980
    Apr 24, 2016 at 5:53
  • yes, you cannot, but OP mentioned in a comment that he wanted the grade instead of the count..so i proposed this...if you need the count you would have to join this query with another query that gets the title and count and then match them.Maybe there is another way to get the count by using the same query..i dont know...
    – cableload
    Apr 24, 2016 at 5:56
0

Another common solution to this problem is joining the table back onto itself. Here is a SQL Fiddle that tests this answer:

SELECT T3.title, T3.grade, T2.maxcount
FROM (
    SELECT title, max(count) as maxcount
    FROM (
        Select class.title, studentClass.grade, count(studentClass.grade) as count
        from classOffering 
        inner join studentClass on classOffering.classID = studentClass.classID
        inner join class on classOffering.classID = class.classID
        group by class.title, studentClass.grade
    ) AS T1
    GROUP BY title
) AS T2 
JOIN (
    Select class.title, studentClass.grade, count(studentClass.grade) as count
    from classOffering 
    inner join studentClass on classOffering.classID = studentClass.classID
    inner join class on classOffering.classID = class.classID
    group by class.title, studentClass.grade
) AS T3 ON T2.title = T3.title AND T2.maxcount = T3.count
ORDER BY T3.title, T3.grade, T2.maxcount

But I think @cableload 's answer is much better if it can be tweaked to consider the max number of occurrences of the grade.

0
WITH tableView1 as (Select class.title as title, studentClass.grade as grade, count(studentClass.grade) as count from classOffering inner join studentClass on classOffering.classID = studentClass.classID inner join class on classOffering.classID = class.classID group by class.title, studentClass.grade order by count(studentClass.grade) desc),
tableView2 as (select title,max(count) as count from tableView1 group by title),
tableView3 as (select tableView1.title as title,tableView1.grade as grade,tableView1.count as count from tableView1 inner join tableView2 on tableView1.title=tableView2.title AND tableView1.count=tableView2.count) select * from tableView3 order by title,grade,count;
0
Select tableView.title, tableView.grade from (Select class.title as title, studentClass.grade as grade, count(studentClass.grade) as count from classOffering 
inner join studentClass on classOffering.classID = studentClass.classID
inner join class on classOffering.classID = class.classID
group by class.title, studentClass.grade
order by count(studentClass.grade) desc)as tableView group by tableView.title, tableView.grade;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.