如何创建这样一个视图?

arfayr 2003-06-18 11:54:56
表:A(ID ParentID Type)

Type为0 和 1 表示单位和部门,我想把部门全部列出来如下:RootID为部门所属的单位。

B(ID ParentID RootID)



示例数据:
A X 0
B X 0
C X 0
A1 A 1
A2 A 1
A11 A1 1
A12 A1 1
A21 A2 1
得到B的视图:
A1 A A
A2 A A
A11 A1 A
A12 A1 A
A21 A2 A

如何创建B这样的视图?SQL语句如何写?请指教


...全文
83 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
arfayr 2003-06-23
  • 打赏
  • 举报
回复
这个例子是可以,但是如果数据不是A呢?视图是不应该跟随数据变化的。

不过问题总算解决了,thanks 各位

bobfang(匆匆过客)的解答最为圆满。
sdqhlyf 2003-06-23
  • 打赏
  • 举报
回复
上面说的可以通过,我试了!
beckhambobo 2003-06-22
  • 打赏
  • 举报
回复
按楼主理解的意思,大概表结构如下:

select * from (select id,parentid,prior id pid
from a
start with id='A'
connect by parentid=prior id)
where Type='1';

试试吧,还没测试过
arfayr 2003-06-22
  • 打赏
  • 举报
回复
to bobfang

1的意思是不是第一个字段?

connect by prior id=pid 这里的这两个字段属于?

能否解释一下connect by prior 和 start with ?
arfayr 2003-06-22
  • 打赏
  • 举报
回复
我知道但是打补丁是没法更改以前的东西的,除非推倒从来

bobfang(匆匆过客):

SQL执行成功,但为什么不能创建为试图呢?
arfayr 2003-06-19
  • 打赏
  • 举报
回复
没有办法么?
beckhambobo 2003-06-19
  • 打赏
  • 举报
回复
这与表结构有关,结构设计好与坏关系到sql.
bobfang 2003-06-19
  • 打赏
  • 举报
回复
不好意思,写错了。应该是:

select A.id, A.pid, B.id as Rootid
from TableA A,
(select id from TableA B1
where B1.Type='0'
and not exists(select 1 from TableA B2 where B2.id=B1.pid)) B
where A.type='1'
and exists(select 1
from TableA C
where C.ID=A.ID
connect by prior id=pid
start with C.id=B.id)
order by 1;
bobfang 2003-06-19
  • 打赏
  • 举报
回复
create table TableA (
id varchar2(5),
pid varchar2(5),
type char(1))
/

insert into TableA values ('A','X',0);
insert into TableA values ('B','X',0);
insert into TableA values ('C','X',0);
insert into TableA values ('CC','C',0);
insert into TableA values ('A1','A',1);
insert into TableA values ('A2','A',1);
insert into TableA values ('B1','B',1);
insert into TableA values ('A11','A1',1);
insert into TableA values ('A21','A2',1);
insert into TableA values ('A22','A2',1);
insert into TableA values ('C1','C',1);
insert into TableA values ('C11','C1',1);
insert into TableA values ('C12','C1',1);
commit;

select A.id, A.pid, B.id as Rootid
from TableA A,
(select id from TableA B1
where B1.Type='0'
and not exists(select 1 from TableA B2 where B2.id=B1.pid)) B
where A.type='1'
and exists(select 1
from TableA C
connect by prior id=pid
start with C.id=B.id);

前提条件表中的数据除了最高层的单位外,其他各单位、部门的pid在表中都能找到对应记录。
arfayr 2003-06-18
  • 打赏
  • 举报
回复
我不能限定单位的长度就是1阿,单位本身也是可以嵌套的,ID的长度也是不定
BlueskyWide 2003-06-18
  • 打赏
  • 举报
回复
select id,ParentID,substr(id,1,1) from A where length(trim(id))>1;
arfayr 2003-06-18
  • 打赏
  • 举报
回复
不能使用函数的
arfayr 2003-06-18
  • 打赏
  • 举报
回复
单位也是可以递归下去的,不能通过长度来限制

另外这是修改,不是开发,所以是没有办法的,就是为了减少工作量才凑出以前这种不好的数据结构的视图.

Lastdrop 2003-06-18
  • 打赏
  • 举报
回复
写一个函数,输入部门,得到其所属的单位,然后利用函数创建视图。
bzszp 2003-06-18
  • 打赏
  • 举报
回复
部门编码的问题

应该通过编码知道所属的部门

示例数据:
A X 0
B X 0
C X 0
A1 A 1
B1 B 1
A11 A1 1
A12 A1 1
B11 B1 1

其中A,B设置长度相同
这样就一目了然,而且方便处理
BlueskyWide 2003-06-18
  • 打赏
  • 举报
回复
数据库设计时,应把单位和部门分开:
A(单位id,单位名称);
B(部门id,部门名称);
使用时,C(单位id,部门id,...)。

要么使用数据库树型结构存放。

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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