1

我在下面写了查询

SELECT DEPT_ID, COUNT(*) AS stud_count FROM TBL_STUDENT_DEPARTMENT_593932       
        GROUP BY DEPT_ID ORDER BY stud_count DESC

它输出:

DEPT_ID     STUD_COUNT
-------     ----------
Dep02                5
Dep03                4
Dep01                3

我想选择第二大的stud_count,即4。所以我使用rownum如下

SELECT stud_count FROM
(
    SELECT DEPT_ID, COUNT(*) AS stud_count FROM TBL_STUDENT_DEPARTMENT_593932       
        GROUP BY DEPT_ID ORDER BY stud_count DESC
)
WHERE ROWNUM = 2;

但是它不返回任何行,只是空表。我怎样才能做到这一点?

4

4 回答 4

4

ROWNUM 以一种特殊的方式运行,这就是您需要使用嵌套查询的原因。首先,您对所有数据进行分组并求和。在外部查询中,您分配行号。然后在最外层的查询中只选择第二行。

SELECT *
FROM
   (
   SELECT dept_id
   ,      stud_count
   ,      rownum as stud_num
   FROM
      (
      SELECT DEPT_ID
      ,      COUNT(*) AS stud_count 
      FROM   TBL_STUDENT_DEPARTMENT_593932       
      GROUP BY DEPT_ID 
      ORDER BY stud_count DESC
      )
   )
WHERE stud_num = 2;

如果您熟悉分析功能,则可以改用此语句:

SELECT *
FROM
   (
   SELECT DEPT_ID
   ,      COUNT(*) AS stud_count
   ,      ROW_NUMBER() over (order by COUNT(*) desc) stud_num
   FROM   TBL_STUDENT_DEPARTMENT_593932       
   GROUP BY DEPT_ID
)
WHERE stud_num = 2;
于 2013-01-10T10:45:27.057 回答
2

请检查:

SELECT * FROM
(
    SELECT DEPT_ID, COUNT(*) AS stud_count, ROW_NUMBER() over (order by COUNT(*) desc) ROW_NUM
    FROM TBL_STUDENT_DEPARTMENT_593932       
        GROUP BY DEPT_ID
)
WHERE ROW_NUM = 2;
于 2013-01-10T10:46:05.077 回答
1

你必须嵌套另一个级别

select dept_id, stud_count
  from (select rownum r, dept_id, stud_count
          from (select dept_id, count(*) as stud_count 
                  from TBL_STUDENT_DEPARTMENT_593932
                 GROUP BY DEPT_ID 
                 order by stud_count desc)
         where rownum <= 2
       )
 where r = 2;

但是使用 rownum 意味着如果两个或多个记录具有第二个计数,您只会得到一行。所以使用你想要第二高计数的所有行的 dense_rank() 。

   select dept_id, stud_count
     from (select dept_id, count(*) as stud_count,
                  dense_rank() over (order by count(*) desc) rnk
             from TBL_STUDENT_DEPARTMENT_593932
             GROUP BY DEPT_ID 
             order by stud_count desc)
     where rnk = 2;

例如:

SQL> select dept_id, count(*) stud_count from tbl_student_department_593932 group by dept_id;

DEPT_ STUD_COUNT
----- ----------
Dep03          4 <-- 
Dep01          3
Dep05          4 <-- 
Dep02          6

SQL> select dept_id, stud_count
  2    from (select rownum r, dept_id, stud_count
  3             from (select dept_id, count(*) as stud_count
  4                      from tbl_student_department_593932
  5                     group by dept_id
  6                     order by stud_count desc)
  7            where rownum <= 2)
  8   where r = 2;

DEPT_ STUD_COUNT
----- ----------
Dep03          4

对比;

SQL> select dept_id, stud_count
  2    from (select dept_id, count(*) as stud_count,
  3                  dense_rank() over(order by count(*) desc) rnk
  4             from tbl_student_department_593932
  5            group by dept_id
  6            order by stud_count desc)
  7   where rnk = 2;

DEPT_ STUD_COUNT
----- ----------
Dep03          4
Dep05          4
于 2013-01-10T10:49:39.533 回答
-1
SELECT TOP DEPT_ID, COUNT(*) AS stud_count 
FROM TBL_STUDENT_DEPARTMENT_593932 tb1
INNER JOIN tbl_student_department_593932 tb2
ON tb1.dept_id = tb2.dept_id
where tb1.count < tb2.count
于 2013-01-10T10:50:15.470 回答