如何将学生选课信息另存成事务类型记录的数据裱??????

HashCodeWithJava 2006-03-31 08:26:08
我现在在做一个学校学生选课的数据库项目。
现在有这样一个难题,教务处的数据库保存的选课表是如下这样的:

class1 student1
class1 student2
class2 student1
class2 student3
class2 student2
....

但是,我需要一张这样的事务类型的数据表来处理:

student1 class1,class2,class5,...
student2 class1,class2,class4,...
....

不知道,如何通过SQL或者PL/SQL来得到这张事务类型的记录的表 ???????


谢谢!!!!!!!!
...全文
207 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
HashCodeWithJava 2006-04-02
  • 打赏
  • 举报
回复
自己UP一下。希望能够更简单的结果。
sailorsailor 2006-04-01
  • 打赏
  • 举报
回复
t_x 如下
class student
-----------------
课程1 student1
课程1 student2
课程2 student1
课程2 student2
课程3 student1
课程3 student2
课程4 student1
课程4 student2

t_t 如下
student class
--------------------
student1
student2

-------------------用以下的存储过程就可以-----
declare
cursor c_v is select * from t_x;
begin
for v in c_v loop
update t_t set class=class||','||v.class where student=v.student;
end loop;
commit;
end;
开发者开聊 2006-03-31
  • 打赏
  • 举报
回复
连接查询结果:
表a 列 a1 a2
记录 1 a
1 b
2 x
2 y
2 z
用select能选成以下结果:
1 ab
2 xyz

下面有两个例子:
1.使用pl/sql代码实现,但要求你组合后的长度不能超出oracle varchar2长度的限制
create or replace type strings_table is table of varchar2(20);
/
create or replace function merge (pv in strings_table) return varchar2
is
ls varchar2(4000);
begin
for i in 1..pv.count loop
ls := ls || pv(i);
end loop;
return ls;
end;
/
create table t (id number,name varchar2(10));
insert into t values(1,'Joan');
insert into t values(1,'Jack');
insert into t values(1,'Tom');
insert into t values(2,'Rose');
insert into t values(2,'Jenny');

column names format a80;
select t0.id,merge(cast(multiset(select name from t where t.id = t0.id) as strings_table)) names
from (select distinct id from t) t0;

drop type strings_table;
drop function merge;
drop table t;


2.纯粹用sql:
表dept, emp
要得到如下结果
deptno, dname, employees
---------------------------------
10, accounting, clark;king;miller
20, research, smith;adams;ford;scott;jones
30, sales, allen;blake;martin;james;turners
每个dept的employee串起来作为一条记录返回

This example uses a max of 6, and would need more cut n pasting to do more than that:

SQL> select deptno, dname, emps
2 from (
3 select d.deptno, d.dname, rtrim(e.ename ||', '||
4 lead(e.ename,1) over (partition by d.deptno
5 order by e.ename) ||', '||
6 lead(e.ename,2) over (partition by d.deptno
7 order by e.ename) ||', '||
8 lead(e.ename,3) over (partition by d.deptno
9 order by e.ename) ||', '||
10 lead(e.ename,4) over (partition by d.deptno
11 order by e.ename) ||', '||
12 lead(e.ename,5) over (partition by d.deptno
13 order by e.ename),', ') emps,
14 row_number () over (partition by d.deptno
15 order by e.ename) x
16 from emp e, dept d
17 where d.deptno = e.deptno
18 )
19 where x = 1
20 /

DEPTNO DNAME EMPS
------- ----------- ------------------------------------------
10 ACCOUNTING CLARK, KING, MILLER
20 RESEARCH ADAMS, FORD, JONES, ROONEY, SCOTT, SMITH
30 SALES ALLEN, BLAKE, JAMES, MARTIN, TURNER, WARD

17,075

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧