题目如下:
一个企业信息表company,有如下数据。
主建(id) 部门编号(dept_no) 部门名称(name) 修改时间(chanage_date)
1 101           财务部                2008/07/04
2 10101           财务结算部     2005/05/06
3 10102           财务管理部     2008/08/09
4 102           人事部                2005/06/04
5 10201           公关部                2006/05/08
6 101           财务公司                2010/08/08
7 10101           财务结算公司      2010/10/10
8 10201           人力资源咨询公司      2009/09/09
9 103           研发部                 2006/05/07
10 103           研发中心亚洲分院       2009/11/10
11 104           市场部                  2009/02/21如上数据。由于公司发展壮大,公司将财务部门名称进行了修改。
查询 部门编号,最新的部门名称,最后修改的时间 ,用SQL语句实现。
查询结果为:
          101           财务公司                2010/08/08
          102           人事部                2005/06/04
          10101           财务结算公司      2010/10/108
          10201           人力资源咨询公司      2009/09/09
          103           研发中心亚洲分院       2009/11/10
          104           市场部                  2009/02/21
          10102           财务管理部     2008/08/09                     

解决方案 »

  1.   

    select decode(dept_no,'财务部','财务公司') from company应该用decode 
      

  2.   

    select * 
    from company a
    where not exists(
    select 1 from company where id = a.id and chanage_date>a.chanage_date
    )
      

  3.   

    嗯 ,不错!select id ,dept_no,name,chanage_date from (
    select id ,dept_no,name,chanage_date,row_number()over(partition by dept_no order by chanage_date desc) rn from company
    )
    where rn=1;
      

  4.   

    select a.* 
    from aa a,
         (select dept_no,max(vdate) vdate from aa group by dept_no )b 
    where a.dept_no=b.dept_no and a.vdate=b.vdate
      

  5.   

    select t.* from company t where chanage_date = (select max(chanage_date) from company where dept_no = t.dept_no) order by t.dept_noselect t.* from company t where not exists (select 1 from company where dept_no = t.dept_no and chanage_date > t.chanage_date) order by t.dept_no
      

  6.   

    今天出了3次这样的题目啦,顺便介绍下类似的函数:row_number()
    1 2 3 4 ...
    rank()
    1 2 2 4 ...
    dense_rank()
    1 2 2 3 ...
      

  7.   


    select dept_no,name,chanage_date
    from
    (select dept_no,name,chanage_date,row_number() over(partition by dept_no order by chanage_date desc) rn
    from company) a
    where a.rn=1--or
    select dept_no,name,chanage_date
    from company a
    where not exists(select 1 from company b where a.dept_no=b.dept_no and a.chanage_date<b.chanage_date)
      

  8.   

    修改用update
    update company set name='财政公司',chang_data='2010/8/8' where id=101;
    其他类似
      

  9.   

    select dept_no,max(name),max(chanage_date) from s0 group by dept_no