表A  档案号,姓名
      001    AA
      002    BB
      003    CC
表B  档案号,日期,目的地
      001   10-09-10  上海
      002   10-09-13  北京
      003   10-09-15  西安
      001   10-09-20  昆明
      002   10-09-25  成都
      003   10-09-22  合肥
      001   10-10-08  广州求最后一次出差列表
      001   10-10-08 广州
      002   10-09-25 成都
      003   10-09-22 合肥这怎么写?    

解决方案 »

  1.   

    select b.* from 表A a,表B b where not exists(select 1 from 表B where 档案号=b.档案号 and 日期>b.日期)
      

  2.   

    select 
        b.* 
    from 
        表A a,表B b 
    where 
        a.档案号=b.档案号 and not exists(select 1 from 表B where 档案号=b.档案号 and 日期>b.日期)
      

  3.   

    SELECT 档案号,日期,目的地
    FROM B m
    WHERE NOT EEXISTS(SELECT NULL FROM B WHERE m.档案号=档案号 AND datediff(dd,m.日期,日期)<0)
      

  4.   

    select b.档案号,max(b.日期),b.目的地 from b group by 档案号,日期
      

  5.   


    --方法一
    select * from B t
    where not exists
    (select 1 from B where 档案号=t.档案号 and 日期>t.日期)--方法二
    select * from B t where 日期=(select max(日期) from B where 档案号=t.档案号)
      

  6.   


    --方法一
    select * from B t
    where not exists
    (select 1 from B where 档案号=t.档案号 and 日期>t.日期)--方法二
    select * from B t where 日期=(select max(日期) from B where 档案号=t.档案号)--方法三
    select a.*,b.目的地
    from(select 档案号,max(日期) 日期 from B group by 档案号)a,b
    where a.档案号=b.档案号--方法四select * from B
    where row_number()over(partition by 档案号 order by 日期 desc)=1
      

  7.   


    --更正
    --方法一
    select * from B t
    where not exists
    (select 1 from B where 档案号=t.档案号 and 日期>t.日期)--方法二
    select * from B t where 日期=(select max(日期) from B where 档案号=t.档案号)--方法三
    select a.*,b.目的地
    from(select 档案号,max(日期) 日期 from B group by 档案号)a,b
    where a.档案号=b.档案号--方法四with cte as
    (select id=row_number()over(partition by 档案号 order by 日期 desc),* from B
    group by 档案号,日期,目的地)
    select 档案号,日期,目的地 from cte where id=1