我有一个包含3列的表格,分别是“年”,“ students_count”和“ faculty”。 我想找出每个教员最多的“ students_count”数。
该表如下所示:
year| students_count| faculty
------------------------------------
2001| 50 | Business
2002| 60 | Business
2003| 40 | Business
2001| 20 | Engineering
2002| 10 | Engineering
2003| 50 | Law
SELECT year,max(students_count),faculty FROM table;
(不起作用)
year| students_count| faculty
-----------------------------------
2002| 60 | Business
2001| 20 | Engineering
2003| 50 | Law
答案 0 :(得分:1)
使用row_number()
select * from
(
select *,row_number() over(partition by faculty order by students_count desc) as rn
from tablename
)A where rn=1
答案 1 :(得分:0)
如果需要,可以按年和教职员工的最大需求分组。
SELECT year,max(students_count),faculty FROM table group by year,faculty
但是如果只需要教师,则可以使用相关的子查询
select top (1) with ties t.*
from table_name t
order by row_number() over (partition by faculty order by students_count desc)
答案 2 :(得分:0)
您想要dense_rank()
:
select t.*
from (select t.*,
dense_Rank() over (partition by faculty order by students_count desc) as seq
from table t
) t
where seq = 1;
答案 3 :(得分:0)
这可以做到:
select * from (select *,rank() over (partition by faculty order by sytudents_count desc
) rnk form tablename) where rnk=1;
答案 4 :(得分:0)
为此,我认为您只需要在sql中使用group by。 这也许可以解决这个问题。
Create Table School(
Year Number(4),
Students_Count Number(2),
Faculty Varchar2(20)
);
Insert Into School Values (2001,50,'Business');
Insert Into School Values (2002,60,'Business');
Insert Into School Values (2003,40,'Business');
Insert Into School Values (2001,20,'Engineering');
Insert Into School Values (2002,10,'Engineering');
Insert Into School Values (2003,50,'Law');
Select * From School;
Select Faculty,Max(Students_Count) From School
Group By Faculty;