提取最大计数然后进行比较

时间:2014-10-18 21:00:16

标签: sql oracle10g compare max

SELECT c.student_name,COUNT(c.student_name)
FROM MEMBER c INNER JOIN NOMTABLE d ON  c.depart_name=d.depart_name  
WHERE (c.student_role = 'head actor' AND d.section='best head actor') OR (c.student_role = 'supporting actor' AND d.section='best supporting actor') OR 
      (c.student_role = 'head actress' AND d.section='best head actress') OR (c.student_role = 'supporting actress' AND d.section='best supporting actress')
GROUP BY c.student_name;

这是我的查询,它将提供学生的姓名和学生姓名出现的次数。我想提取具有最大计数的学生姓名,然后我想将该student_name与Table Student中的student_name进行比较,并提取该计数最多的学生的student_name和date_of_birth。

我知道我可以这样做Select * From(查询上方)rownum< = 1然后在那之后我无法将student_name与student表中的student_name进行比较。

对此的任何帮助都会非常有帮助。非常感谢你。

2 个答案:

答案 0 :(得分:2)

我会使用子查询和row_number()

来处理此问题
SELECT s.*, cnt
FROM (SELECT c.student_name, COUNT(*) as cnt,
             ROW_NUMBER() OVER (PARTITION BY c.student_name ORDER BY COUNT(*) DESC) as seqnum
      FROM MEMBER c INNER JOIN
           NOMTABLE d
           ON c.depart_name=  d.depart_name  
      WHERE (c.student_role = 'head actor' AND d.section='best head actor') OR
            (c.student_role = 'supporting actor' AND d.section='best supporting actor') OR 
            (c.student_role = 'head actress' AND d.section='best head actress') OR
            (c.student_role = 'supporting actress' AND d.section='best supporting actress')
      GROUP BY c.student_name
     ) cd JOIN
     students s
     on cd.student_name = s.student_name
WHERE seqnum = 1;

答案 1 :(得分:0)

尝试使用HAVING子句

SELECT s.student_name,s.date_of_birth 
FROM student s
WHERE s.student_name = (SELECT c.student_name
                       FROM MEMBER c INNER JOIN NOMTABLE d ON  c.depart_name=d.depart_name  
                       WHERE (c.student_role = 'head actor' AND d.section='best head actor') 
                          OR (c.student_role = 'supporting actor' AND d.section='best supporting actor') 
                          OR (c.student_role = 'head actress' AND d.section='best head actress')              
                          OR (c.student_role = 'supporting actress' AND d.section='best supporting actress')
                      GROUP BY c.student_name
                      HAVING COUNT(c.student_name) = (SELECT COUNT(c.student_name) student_count
                                                      FROM MEMBER c INNER JOIN NOMTABLE d ON  c.depart_name=d.depart_name  
                                                      WHERE (c.student_role = 'head actor' AND d.section='best head actor') 
                                                      OR (c.student_role = 'supporting actor' AND d.section='best supporting actor') 
                                                      OR (c.student_role = 'head actress' AND d.section='best head actress')              
                                                      OR (c.student_role = 'supporting actress' AND d.section='best supporting actress') 
                                                      AND ROWNUM <=1 
                                                      GROUP BY c.student_name
                                                      ORDER  BY student_count DESC 
                                                      ));