以下是PL / SQL查询的代码,我必须获取有资格教授特定课程的教师姓名,以及他们教授此课程和最后一次的时间(他们教这门课程的年份和学期。
我已经完成了大部分问题,但无法弄清楚如何获得max(o.co_year)的相应详细信息,即术语
declare
gname varchar2(20);
count_id number(2);
id varchar(20);
year1 number(4);
cursor abc // cursor 1
is
SELECT i.i_gname
into gname
FROM INSTRUCTOR I
WHERE i.i_id in (
SELECT t.i_id FROM TeachingQualification T
WHERE t.c_id in (SELECT c.c_id FROM COURSE C
WHERE c.c_title = 'Advanced Database App')) ;
cursor bcd // cursor two
is
select o.i_id, count(o.i_id), max(o.co_year)
into id, count_id, year1
from courseoffering o
where (o.i_id = i_id and o.c_id = 1234567)
group by o.i_id;
Begin
open abc;
open bcd;
loop
FETCH abc into gname;
exit when abc%NOTFOUND;
FETCH bcd into id, count_id, year1;
exit when bcd%NOTFOUND;
DBMS_OUTPUT.PUT_LINE ('NAME: ' || gname || ' Number of times taught ' || count_id || ' Year ' || year1 || ); // want to output corresponding column details for the year1 attribute.
end loop;
close bcd;
close abc;
end;
PL / SQL我想要相应的MAX列(O.CO_YEAR)。怎么做?
答案 0 :(得分:2)
是的,我知道,但它是一项大学任务,我想在PL / SQL中实现。
布拉赫!我宁愿您的大学教您正确使用PLSQL和SQL,并提供了有意义的任务。如果您可以在SQL中执行某些操作,请在SQL中执行此操作
另外:为什么您的游标包含INTO
关键字?你为什么需要循环?您似乎只期望1返回值?
我稍微剖析了你的代码:
--instructors who are qualified to teach a course
SELECT i.i_gname --instructor name
FROM INSTRUCTOR I
WHERE i.i_id IN ( SELECT t.i_id --instructor_id
FROM TeachingQualification T
WHERE t.c_id in (SELECT c.c_id --course_id
FROM COURSE C
WHERE c.c_title = 'Advanced Database App'));
--instructors who taught a course, with amount and last time
SELECT o.i_id instructor_id, count(o.i_id) times_taught, max(o.co_year) last_time_taught
FROM courseoffering o
WHERE o.c_id = 1234567 --course_id
GROUP BY o.i_id;
--all instructors (ID) who taught advanced database app, how many times, and last time
--consider that this may produce NO_DATA_FOUND
SELECT o.i_id instructor_id, count(o.i_id) times_taught, max(o.co_year) last_time_taught
FROM course c
JOIN courseoffering o
ON c.c_id = o.c_id
WHERE c.c_title = 'Advanced Database App'
GROUP BY o.i_id;
-- i don't think teachingqualification is required. The last select providers instructor IDs.
-- There is no need to go through that table, unless it would contain extra data you'd want to
-- filter by. Since courseoffering is being queried, and it has instructors, it stands to reason
-- that those instructor are qualified to teach the course.
SELECT (SELECT i_gname FROM instructor WHERE i_id = o.i_id) instructor
,count(o.i_id) times_taught
, max(o.co_year) last_time_taught
FROM course c
JOIN courseoffering o
ON c.c_id = o.c_id
WHERE c.c_title = 'Advanced Database App'
GROUP BY o.i_id;
-- Look, if you do want a PLSQL block for this, go ahead.
BEGIN
FOR r IN (SELECT (SELECT i_gname FROM instructor WHERE i_id = o.i_id) instructor
,count(o.i_id) times_taught
, max(o.co_year) last_time_taught
FROM course c
JOIN courseoffering o
ON c.c_id = o.c_id
WHERE c.c_title = 'Advanced Database App'
GROUP BY o.i_id)
LOOP
DBMS_OUTPUT.PUT_LINE ('NAME: ' || r.instructor || ' Number of times taught ' || r.times_taught || ' Year ' || r.last_time_taught);
END LOOP;
END;
哦,请为你的专栏提供有意义的名字。例如,将其称为instructor_id
,而不是i_id
。
我设置了一些示例数据:
create table course (id number(5,0), cname varchar2(50), constraint course_pk primary key (id))
/
create table courseoffering(id number(5,0), course_id number(5,0), instructor_id number(5,0), course_year number(5,0), constraint offering_pk primary key (id), constraint course_fk foreign key (course_id) references course (id))
/
insert into course values (1, 'Tech I');
insert into course values (2, 'Basic SQL');
insert into course values (3, 'Advanced SQL');
--Instructor 1
insert into courseoffering values (1, 1, 1, 2009); --Tech I
insert into courseoffering values (2, 1, 1, 2010); --Tech I
insert into courseoffering values (3, 1, 1, 2011); --Tech I
insert into courseoffering values (4, 2, 1, 2011); --Basic SQL
insert into courseoffering values (5, 2, 1, 2012); --Basic SQL
--Instructor 2
insert into courseoffering values (6, 2, 2, 2008); --Basic SQL
insert into courseoffering values (7, 2, 2, 2009); --Basic SQL
insert into courseoffering values (8, 2, 2, 2010); --Basic SQL
insert into courseoffering values (9, 3, 2, 2010); --Advanced SQL
insert into courseoffering values (10, 3, 2, 2011); --Advanced SQL
insert into courseoffering values (11, 3, 2, 2012); --Advanced SQL
insert into courseoffering values (12, 1, 2, 2009); --Tech I
insert into courseoffering values (13, 1, 2, 2010); --Tech I
commit;
运行此:
SELECT c.cname, o.instructor_id, count(o.instructor_id) times_taught, max(o.course_year) last_time_taught
FROM course c
JOIN courseoffering o
ON c.id = o.course_id
GROUP BY c.cname, o.instructor_id
ORDER BY c.cname, o.instructor_id;
产地:
CNAME INSTRUCTOR_ID TIMES_TAUGHT LAST_TIME_TAUGHT
Advanced SQL 2 3 2012
Basic SQL 1 2 2012
Basic SQL 2 3 2010
Tech I 1 3 2011
Tech I 2 2 2010
您甚至可以轻松地将所需数据转换为视图。 不需要PLSQL。 SQL中只有几行。如果你想在PLSQL中使用它,你仍然可以使用一个循环来覆盖每个课程的多个教师,或者如果你将它缩小到一个课程和一个教师,一些变量。始终最小化SQL和PLSQL上下文之间的切换。
答案 1 :(得分:0)
Oracle中的游标可以有参数。有关详细信息,请参阅http://docs.oracle.com/cd/B19306_01/appdev.102/b14261/sqloperations.htm#BABHICAF。
可能在您的代码中如何工作:
cursor bcd(p_c_id courseoffering.c_id%type) // cursor two
is
select o.i_id, count(o.i_id), max(o.co_year)
into id, count_id, year1
from courseoffering o
where (o.i_id = i_id and o.c_id = p_c_id)
group by o.i_id;