select id,ParentID,substr(id,1,1) from A where length(trim(id))>1;

解决方案 »

  1.   

    数据库设计时,应把单位和部门分开:
    A(单位id,单位名称);
    B(部门id,部门名称);
    使用时,C(单位id,部门id,...)。要么使用数据库树型结构存放。
      

  2.   

    部门编码的问题应该通过编码知道所属的部门示例数据:
    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设置长度相同
    这样就一目了然,而且方便处理
      

  3.   

    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在表中都能找到对应记录。
      

  4.   

    不好意思,写错了。应该是: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;
      

  5.   

    这与表结构有关,结构设计好与坏关系到sql.
      

  6.   

    我知道但是打补丁是没法更改以前的东西的,除非推倒从来 bobfang(匆匆过客):SQL执行成功,但为什么不能创建为试图呢?
      

  7.   

    to bobfang1的意思是不是第一个字段?connect by prior id=pid 这里的这两个字段属于?能否解释一下connect by prior 和 start with ?
      

  8.   

    按楼主理解的意思,大概表结构如下:select * from (select id,parentid,prior id pid
    from a 
    start with id='A'
    connect by parentid=prior id)
    where Type='1';试试吧,还没测试过
      

  9.   

    这个例子是可以,但是如果数据不是A呢?视图是不应该跟随数据变化的。不过问题总算解决了,thanks 各位bobfang(匆匆过客)的解答最为圆满。