要求查询如下:
1、请查询出所有部门的最新信息(deptcode,efectivedate,deptname),写出SQL语句
 
注:部门编码是部门的唯一标识,不会改变;部门名称信息应以最后生效的部门为准;
 
2、请查询出包含下级部门的并发生过更名的部门。 

解决方案 »

  1.   

    1.select top 1 deptcode,efectivedate,deptname from tb order by efectivedate desc
      

  2.   

    晕  错误
    select 
      * 
    from 
      tb t 
    where 
      not exists(select 1 from tb where deptcode=t.deptcode and deptname=t.deptname and efectivedate>t.efectivedate )
      

  3.   

    第2题是BOM问题 没有看见上层与下层怎么联系的  不会
      

  4.   


    1. select * from dept t not exists(select 1 from dept where t.deptcode=deptcode and t.effectivedate<effectivedate)2. select * from dept t
    where reason='更名'
    and exists(select 1 from dept where charindex(t.deptcode,deptcode)>0 and t.deptcode<>deptcode)
      

  5.   

    没看懂,第一个from后的tb和t分别是啥?希望试验过后再发啊?
      

  6.   

    1、请查询出所有部门的最新信息(deptcode,efectivedate,deptname),写出SQL语句 select t.* from tb t where efectivedate = (select max(efectivedate) from tb where deptcode = t.deptcode )
    select t.* from tb t where not exists (select 1 from tb where deptcode = t.deptcode and efectivedate > t.efectivedate)
      

  7.   

    其实是tb as t 少了一个as  t是tb的别名