如题
我通过多个表 join 得到一些数据,但是在结果中还有很多重复的数据例如:
id          department      modified
3.1.1.0      abcde          2006-06-10
3.1.1.1      fghij          2006-06-11
3.1.1.2      klmnp          2006-06-12
3.1.1.2      null           2006-06-23
3.1.1.2      fdsfdfs        2006-07-10
           
我想得到是id          department      modified
3.1.1.0      abcde          2006-06-10
3.1.1.1      fghij          2006-06-113.1.1.2      fdsfdfs        2006-07-10
取出id中不重复和时间中显示最大一条记录

解决方案 »

  1.   

    select * from test a where not exists
    (
    select 1 from test where id=a.id and modified>a.modified
    )
      

  2.   

    select * from [Table] a where not exists(select 1 from [Table] where id=a.id and modified>a.modified)
      

  3.   

    create table test(id varchar(10),department varchar(10),modified datetime)
    insert test select '3.1.1.0','abcde','2006-06-10'
    union all select '3.1.1.1','fghij','2006-06-11'
    union all select '3.1.1.2','klmnp','2006-06-12'
    union all select '3.1.1.2',null,'2006-06-23'
    union all select '3.1.1.2','fdsfdfs','2006-07-10'select * from test a where not exists
    (
    select 1 from test where id=a.id and modified>a.modified
    )id         department modified                                               
    ---------- ---------- ------------------------ 
    3.1.1.0    abcde      2006-06-10 00:00:00.000
    3.1.1.1    fghij      2006-06-11 00:00:00.000
    3.1.1.2    fdsfdfs    2006-07-10 00:00:00.000
      

  4.   

    select 
    t.id,t.department,t.modified
    from table t
    inner join 
    (select id,modified = max(modified) from table group by id)
    a on a.id = t.id and a.modified = t.modified哈哈,我测试了一下。估计比not exists慢点。
      

  5.   

    Select * From news A
    Where (Select Count(*) From news Where id=a.id And modified>a.modified) < 1
    Order By id , modified--方法二:
    select * from test a where not exists
    (
    select 1 from test where id=a.id and modified>a.modified
    )--方法三:
    Select * From news A
    Where AddDate In (Select TOP 1 modified From news Where id=a.id  Order By modified Desc)
    Order By id, modified 我也学学鱼兄
      

  6.   

    select a.ID,department,modified
    from #temp a,
    (
    select ID,max(modified) maxDate
    from #temp
    group by ID
    ) b
    where a.ID=b.ID
    and a.modified=b.maxDate