Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL Group By并返回有限的结果_Sql - Fatal编程技术网

SQL Group By并返回有限的结果

SQL Group By并返回有限的结果,sql,Sql,我最近在一次采访中被问到一个SQL问题。 要求是在包含学生ID、课程编号和年份的表格中找到“最热情的学生”。查询应返回每年上课最多的学生,并仅返回这些记录。(我有点忽略了不同学生在某一年上相同数量的课的情况,因为我没有得到要求。可能有其他要求来满足这一要求,所以我避免了这种情况,我只是专注于让一个查询在这个没有条件的特殊情况下工作。)同样热情的学生(每年) 后来我建立了一个小表格,试图解决这个问题。 我的表格有三个字段: CREATE TABLE students ( student_no

我最近在一次采访中被问到一个SQL问题。 要求是在包含学生ID、课程编号和年份的表格中找到“最热情的学生”。查询应返回每年上课最多的学生,并仅返回这些记录。(我有点忽略了不同学生在某一年上相同数量的课的情况,因为我没有得到要求。可能有其他要求来满足这一要求,所以我避免了这种情况,我只是专注于让一个查询在这个没有条件的特殊情况下工作。)同样热情的学生(每年)

后来我建立了一个小表格,试图解决这个问题。 我的表格有三个字段:

CREATE TABLE students (
student_no    INT,
course_no     INT,
year          varchar(4));
(这是一个非常简单的表格。我知道年份不应该像这样存储,等等。我不想挂在表格的格式上。这只是为了尽可能简单地支持这个练习。)

我创建的数据是:

'1', '1', '2000'
'1', '2', '2000'
'1', '3', '2000'
'2', '1', '2000'
'2', '2', '2000'
'2', '3', '2000'
'2', '4', '2000'
'1', '2', '2001'
'1', '1', '2001'
'1', '3', '2001'
'1', '4', '2001'
'2', '1', '2001'
'2', '2', '2001'
'2', '3', '2001'
…因此,学生2是2000年最热情的学生,有4门课程**,学生1是2001年最热情的学生,有4门课程**

'1', '4', '2001'
'2', '4', '2000'
我最终得到的问题是:

Select * from (
   select max(coursecount), year from
    (select student_no, count(course_no) as coursecount, year
      from students
      group by student_no, year) as internal group by year) as maxes,
        (select student_no, count(course_no) as coursecount, year
          from students
          group by student_no, year) as students
 where maxes.coursecount = students.coursecount
 and maxes.year = students.year
…但我觉得有更好的办法

有人能告诉我1)使用ANSI SQL执行此操作的更优雅的方法,2)使用Oracle中的分析函数执行此操作的另一种方法吗


提前感谢您的帮助。

假设您使用的是SQL Server 2005+

你可以试试

;WITH Vals AS (
    SELECT year, student_no, COUNT(course_no) Cnt
    FROM students
    GROUP BY  year, student_no
 )
, RowIDS AS (
    SELECT *, ROW_NUMBER() OVER(PARTITION BY year ORDER BY CNT DESC) RowID
    FROM Vals
)
  SELECT *
  FROM RowIDS 
  WHERE RowID = 1

在标准SQL中,您可以尝试:

select year,student_no
from students
group by year,student_no
having count(*) = (
   select max( cnt ) 
   from (
     select count(*) cnt
     from students
     group by year,student_no
   ) x
  )

演示:

您正在查看哪些RDBMS?我在家使用MySQL,所以我尝试使用ANSI SQL,但也熟悉Oracle,只是目前无法访问Oracle数据库。