这个因为需要用到在insert into select,而且比较只是一两个字段比较,
所以不能用group by写的头晕死了,发个贴子换换脑大概数据是这样的明细表:
PK  name  service    time
1    A01   B01       Time11
2    A01   B01       Time12
3    A01   B02       Time21   
4    A01   B03       Time31这是人员A01的明细,他在在业务B01,有两条记录,是重复了,所以根据Time来选择出最大的时间,作为它的最新数据得到类型的结果集:
PK  name  service    time
2    A01   B01       Time12
3    A01   B02       Time21   
4    A01   B03       Time31大概是这样,记录除了A01,还有A02...
还有不同的service,除了这些,字段还比较多,所以distinct, group by又没法用,不知各位有没什么好法子。
谢谢。

解决方案 »

  1.   

    select * from tb t where  time =(select max( time ) from tb where name=t.name and service=t.service)
      

  2.   

    这个用于就是某大表的明细insert操作。数据量比较多,而且重复比较多。多谢先,我先试下。
      

  3.   


    create table mx
    (
    PK int,
    [name] varchar(8),
    [service] varchar(8),    
    [time] datetime 
    )
    goinsert into mx
    select 1,    'A01',  'B01',     '2009-10-1' union all
    select 2,    'A01',  'B01',      '2009-10-2' union all 
    select 3,    'A01',  'B02',      '2009-10-4' union all  
    select 4,    'A01',  'B03',      '2009-10-4'
    go select * from mx m where not exists(select * from mx where mx.[service]=m.[service] and mx.[time]>m.[time])
      

  4.   

    --> 测试数据:@table
    declare @table table([PK] int,[name] varchar(3),[service] varchar(3),[time] varchar(10))
    insert @table
    select 1,'A01','B01','2009-11-01' union all
    select 2,'A01','B01','2009-11-10' union all
    select 3,'A01','B02','2009-11-05' union all
    select 4,'A01','B03','2009-11-08'union all
    select 5,'A02','B03','2009-11-08'union all
    select 6,'A02','B03','2009-11-08'select * from @table t
    where not EXISTS (select * from @table where t.service = service and 
    name = t.name and time > t.time)
    --结果
    --------------------------
    2 A01 B01 2009-11-10
    3 A01 B02 2009-11-05
    4 A01 B03 2009-11-08
    5 A02 B03 2009-11-08
    6 A02 B03 2009-11-08
      

  5.   

    -- =============================================
    -- Author:      T.O.P
    -- Create date: 2009/11/25
    -- Version:     SQL SERVER 2005
    -- =============================================
    declare @tb table([PK] int,[name] varchar(3),[service] varchar(3),[time] varchar(6))
    insert @tb
    select 1,'A01','B01','Time11' union all
    select 2,'A01','B01','Time12' union all
    select 3,'A01','B02','Time21' union all
    select 4,'A01','B03','Time31'select * 
    from @tb a
    where not exists(select 1 from @tb where a.name=name and a.service=service and a.time<time)
    --测试结果:
    /*
    PK          name service time
    ----------- ---- ------- ------
    2           A01  B01     Time12
    3           A01  B02     Time21
    4           A01  B03     Time31(3 row(s) affected)*/
      

  6.   

    谢谢各位,原来比较简单,想了半天都没搞出来。脑子有点乱了。测试了下,time = (select max(time)...)这种情况效率较快,基本10秒就出来,20W条数据,
    where not exists的情况,CPU,时间都占用较大,可能我写的不好。多谢各位了。晚点我再结,做完事先。