两个表 结构如下 假如 A有数据,AB没数据,就证明A是最下级,那么取A,父ID(pid)就是0。   假如A有数据,AB有数据,ABC没数据,那么证明AB是最下级,那么取AB他的父ID就是A的id,那么父id(pid)就是1 这个sql该如何来写呢?

解决方案 »

  1.   

    试试coalesce(abcd,abc,ab,a)函数
      

  2.   

    是不是这个意思:
    select *,(select PID from B where B.id = coalesce(abcd,abc,ab,a)) as PID
    from A  那么如何去找到父id呢?
      

  3.   

    谢谢你 我有两个表 第一个表是别人的表,我要转换成我的表就是第二个表 我这个图片有点点问题 就是第二个表的id=5的这条数据 的pid=0 应该是这样 你的这个写法是不可行的
      

  4.   


      情况是这样的,他们有个HR系统 HR系统里面的组织机构的层级关系 就是我的表1结构  我们系统的组织机构是我的表2机构, 说白了我是要把1 转换成一个无限极的机构,因为很多原因,我放弃在代码中来完成转换,现在需要在sql里面转换
      

  5.   

    不很清楚需求 不知道是不是这样create table #Tmp
    (
    ID int identity(1,1)
    ,A varchar(100) null
    ,AB varchar(100) null
    ,ABC varchar(100) null
    ,ABCD varchar(100) null
    )
    insert into #Tmp(A) values('123')
    insert into #Tmp(A,AB) values('123','1234')
    insert into #Tmp(A,AB,ABC) values('123','1234','12345')
    insert into #Tmp(A,AB,ABC,ABCD) values('123','1234','12345','123456')
    insert into #Tmp(A) values('234')
    insert into #Tmp(A,AB) values('234','2345')
    insert into #Tmp(A,AB,ABC) values('234','2345','23456')
    insert into #Tmp(A,AB,ABC,ABCD) values('234','2345','23456','234567');with ttt as
    (
    select ID,ISNULL(ABCD,ISNULL(ABC,ISNULL(AB,ISNULL(A,0))))as 'ABCD'
    ,case when AB IS Null then 0 else null end as 'A_PID'
    ,case when ABC IS Null then (select ID from #Tmp t where tt.A=t.A and t.AB IS null and tt.AB IS not null) 
    else null end as 'AB_PID'
    ,case when ABCD IS Null then(select ID from #Tmp t where tt.AB=t.AB and t.ABC IS NULL and tt.ABC IS not null) --ABC_PID
    else (select ID from #Tmp t where tt.ABC=t.ABC and t.ABCD IS NULL)  end as 'ABCD_PID' --ABCD_PID
    from #Tmp tt ),
    t2 as
    (
    select t.ID,t.ABCD,ISNULL(t.ABCD_PID,ISNULL(t.AB_PID,ISNULL(t.A_PID,null)))as 'PID'
    from ttt t
    )

    select * from t2
    select * from #Tmpdrop table #Tmp
      

  6.   

    select id, 
      case when ab is null then A 
           when abc is null then AB
           when abcd is null then abc
           else abcd end as ABCD,
      case when ab is null then '' 
           when abc is null then A
           when abcd is null then AB
           else ABC end as Parent
    into #tempinsert into #temp values(0, '', '')seletct a.id, a.abcd, b.id as pid
    from #temp a inner join #temp b on (a.parent=b.abcd)
      

  7.   


    create table #Tmp
    (
        ID int identity(1,1)
        ,A varchar(100) null
        ,AB varchar(100) null
        ,ABC varchar(100) null
        ,ABCD varchar(100) null
    )
    insert into #Tmp(A) values('123')
    insert into #Tmp(A,AB) values('123','1234')
    insert into #Tmp(A,AB,ABC) values('123','1234','12345')
    insert into #Tmp(A,AB,ABC,ABCD) values('123','1234','12345','123456')
    insert into #Tmp(A) values('234')
    insert into #Tmp(A,AB) values('234','2345')
    insert into #Tmp(A,AB,ABC) values('234','2345','23456')
    insert into #Tmp(A,AB,ABC,ABCD) values('234','2345','23456','234567')
     
     with t as
     (
     select id,coalesce(ABCD,ABC,AB,A) ABCD
     ,CASE 
      WHEN ABCD IS NOT NULL THEN ABC
      WHEN ABC IS NOT NULL THEN AB
      WHEN AB IS NOT NULL THEN A
      WHEN A IS NOT NULL THEN null
      END as pname
     from #Tmp
     )
     select a.id,a.ABCD,isnull(b.id,0) pid
     from t a
     left join  t b
     on a.pname=b.ABCD