Oracle:显示课程中的最大描述

时间:2018-03-13 00:23:18

标签: sql oracle aggregate-functions

我正在编写将返回最长描述的课程的查询。在这里,我在想什么,但它不会工作?

SELECT Max(DESCRIPTION) 
FROM course

4 个答案:

答案 0 :(得分:0)

这将为您提供课程表的最大描述。

select description
from (select description, length(description) as len
from course 
order by len desc) t
where rownum = 1

答案 1 :(得分:0)

内部查询选择最大描述长度,外部查询列出具有最大长度的课程。

select * from course 
where length(description) = (select max(length(description)) from course);

如果您只想要说明,请使用select description from ...代替*

答案 2 :(得分:0)

在子查询中使用分析max()函数。这样,基表只读了一次。

select description
from   ( 
         select description, length(description) as len,
                max(length(description)) over () as max_len
         from   course
       )
where  len = max_len
;

答案 3 :(得分:0)

您可以length comparison使用outer jointies包含course descriptions

create table course
(
  course_id int,
  description varchar2(100)
);     
insert all 
  into course(course_id,description) values (1,'Mathematics')
  into course(course_id,description) values (2,'Physics')
  into course(course_id,description) values (3,'Mathematics')
  into course(course_id,description) values (4,'Biology')
  into course(course_id,description) values (5,'Chemistry')
select * from dual;    
select c1.course_id, c1.description 
  from course c1  left outer join 
       course c2 on length(c2.description) > length(c1.description)
 where length(c2.description) is null;

COURSE_ID      DESCRIPTION
-----------    --------------
1              Mathematics
3              Mathematics     

Demo