在一个查询中比较Postgres中的两个值

时间:2016-04-24 04:28:27

标签: sql database postgresql greatest-n-per-group

所以这是我目前的查询:

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

这是输出:

enter image description here

现在我要做的只是取回课程中最常见的成绩。所以我需要用B的等级来削减软件开发I,因为A是班级中最常见的等级。但我不知道如何比较计数值。任何帮助都会很精彩。

4 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

如果您不需要计数(根据您的评论),您可以尝试这样的事情

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;

以下查询也会获得计数

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;

答案 2 :(得分:0)

此问题的另一个常见解决方案是将表格重新连接到自身。 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

但我认为@cableload的答案要好得多,如果可以调整它来考虑成绩的最大出现次数。

答案 3 :(得分: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;