Reputation: 24721
I have written below query
SELECT DEPT_ID, COUNT(*) AS stud_count FROM TBL_STUDENT_DEPARTMENT_593932
GROUP BY DEPT_ID ORDER BY stud_count DESC
It outputs:
DEPT_ID STUD_COUNT
------- ----------
Dep02 5
Dep03 4
Dep01 3
I want to select second largest stud_count, i.e. 4. So I used rownum as follows
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;
However it does not return any row, just empty table. How can I do this?
Upvotes: 1
Views: 872
Reputation: 3325
ROWNUM behaves in a special way, that's why you need to use a nested query. First you group by and sum all your data. In the outer query you assign the row numbers. In the outermost query you then pick out only the second row.
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;
If you are familiar with analytics functions, you can use this statement instead:
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;
Upvotes: 4
Reputation: 2880
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
Upvotes: -1
Reputation: 21973
you have to nest another level
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;
but using rownum means that if two or more records had the second count, you'd only get one row. so use dense_rank() of you wanted all rows of second highest count.
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;
eg:
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
vs;
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
Upvotes: 1
Reputation: 18629
Please check:
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;
Upvotes: 2