1  select * from table1
   where dwbm='1001' and id=x3 and insert_date=max(insert_date)

解决方案 »

  1.   

    1、where dwbm='1001'
    只筛选出日期最近的,id=x3的记录(只一行就可以了)select 
        a.* 
    from 
        table1 a 
    where 
        a.dwbm='1001' 
        and 
        not exists(select * from table1 where dwbm=a.dwbm and insert_date>a.insert_date)
      

  2.   

    2、只取出日期最靠前记录,每一个单位一条
    id=x3和id=x4
    另外,单位不止2个,insert_date是字符型字段.select 
        a.* 
    from 
        table1 a 
    where 
        not exists(select * from table1 where dwbm=a.dwbm and insert_date>a.insert_date)
      

  3.   

    1、
    select
        a.*
    from
        table1 a,
        (select dwbm,max(insert_date) as insert_date from table1 group by dwbm) b
    where
        a.dwbm='1001'
        and
        a.dwbm=b.dwbm 
        and 
        a.insert_date=b.insert_date2、
    select
        a.*
    from
        table1 a,
        (select dwbm,max(insert_date) as insert_date from table1 group by dwbm) b
    where
        a.dwbm=b.dwbm 
        and 
        a.insert_date=b.insert_date
      

  4.   

    2
    select aa.* from table1 aa,(select dwbm,max(insert_date) from table1 group by dwbm) bb
    where aa.dwbm=bb.dwbm and aa.insert_date=bb.insert_date