获取具有最大值的其他列(属性)
Get other columns(attribute) with max values
我的数据框
ID COURSE_ID SEC_ID SEMESTER YEAR GRADE
00128 CS-101 1 Fall 2009 A
00128 CS-347 1 Fall 2009 A-
12345 CS-101 1 Fall 2009 C
....
我想在 2009 年秋季获得最大注册人数 (count(id)) 的 course_id & sec_id。
所以,我试过了
select course_id, sec_id, enrollment
from (select course_id, sec_id, count(ID) as enrollment
from takes
where semester = 'Fall' and year = 2009
group by course_id, sec_id)
但是,这将导致每个 class 都有注册人。我只想显示注册人数达到 最大 的 classes。我想我需要使用 max,但现在我需要用这段代码的小节来解决它,from.(from subquery)
++ 可以用having子句解决吗?
如果可以的话,请告诉我,我将不胜感激。
感谢您的阅读。
您可以过滤 2009 年秋季的课程,按 course_id
和 sec_id
聚合,按每组的行数对结果进行排序,并使用行限制子句来获得最大参加课程:
select course_id, sec_id, count(*) no_registrants
from takes
where semester = 'Fall' and year = '2009'
group by course_id, sec_id
order by no_registrants desc
fetch first 1 rows with ties
这允许顶级关系(如果有的话)。如果只需要一行,可以将 fetch first 1 rows with ties
更改为 fetch first 1 rows only
。您可能还想添加第二个排序标准以使结果具有确定性(否则,它是未定义的,当然会出现在有联系的地方)。
在 Oracle < 12c 中,行限制子句不可用,您可以使用 rank()
代替(或 row_number()
以禁止并列):
select course_id, sec_id, no_registrants
from (
select
course_id,
sec_id,
count(*) no_registrants,
rank() over(order by count(*) desc) rn
from takes
where semester = 'Fall' and year = '2009'
group by course_id, sec_id
) t
where rn = 1
根据我对您的要求的理解,您的查询只需修改即可获得正确的结果。
只获得注册人数最多的一条记录。使用相同的查询只需添加以下提到的更改:
select course_id, sec_id, max(enrollment) as registrants
from (select course_id, sec_id, count(ID) as enrollment
from takes
where semester = 'Fall' and year = 2009
group by course_id, sec_id) as Rdet group by course_id,sec_id;
这将根据您的要求为您提供正确的结果。
您可以将子查询放在 CTE 中以重用它:
with
x as (
select course_id, sec_id, count(*) as enrollment
from takes
where semester = 'Fall' and year = 2009
group by course_id, sec_id
)
select *
from x
where enrollment = (select max(enrollment) from x)
我的数据框
ID COURSE_ID SEC_ID SEMESTER YEAR GRADE
00128 CS-101 1 Fall 2009 A
00128 CS-347 1 Fall 2009 A-
12345 CS-101 1 Fall 2009 C
....
我想在 2009 年秋季获得最大注册人数 (count(id)) 的 course_id & sec_id。
所以,我试过了
select course_id, sec_id, enrollment
from (select course_id, sec_id, count(ID) as enrollment
from takes
where semester = 'Fall' and year = 2009
group by course_id, sec_id)
但是,这将导致每个 class 都有注册人。我只想显示注册人数达到 最大 的 classes。我想我需要使用 max,但现在我需要用这段代码的小节来解决它,from.(from subquery)
++ 可以用having子句解决吗? 如果可以的话,请告诉我,我将不胜感激。
感谢您的阅读。
您可以过滤 2009 年秋季的课程,按 course_id
和 sec_id
聚合,按每组的行数对结果进行排序,并使用行限制子句来获得最大参加课程:
select course_id, sec_id, count(*) no_registrants
from takes
where semester = 'Fall' and year = '2009'
group by course_id, sec_id
order by no_registrants desc
fetch first 1 rows with ties
这允许顶级关系(如果有的话)。如果只需要一行,可以将 fetch first 1 rows with ties
更改为 fetch first 1 rows only
。您可能还想添加第二个排序标准以使结果具有确定性(否则,它是未定义的,当然会出现在有联系的地方)。
在 Oracle < 12c 中,行限制子句不可用,您可以使用 rank()
代替(或 row_number()
以禁止并列):
select course_id, sec_id, no_registrants
from (
select
course_id,
sec_id,
count(*) no_registrants,
rank() over(order by count(*) desc) rn
from takes
where semester = 'Fall' and year = '2009'
group by course_id, sec_id
) t
where rn = 1
根据我对您的要求的理解,您的查询只需修改即可获得正确的结果。
只获得注册人数最多的一条记录。使用相同的查询只需添加以下提到的更改:
select course_id, sec_id, max(enrollment) as registrants
from (select course_id, sec_id, count(ID) as enrollment
from takes
where semester = 'Fall' and year = 2009
group by course_id, sec_id) as Rdet group by course_id,sec_id;
这将根据您的要求为您提供正确的结果。
您可以将子查询放在 CTE 中以重用它:
with
x as (
select course_id, sec_id, count(*) as enrollment
from takes
where semester = 'Fall' and year = 2009
group by course_id, sec_id
)
select *
from x
where enrollment = (select max(enrollment) from x)