SQL中表A的结构如下:
             ID        Name       LoginTime        IdType
            111        aaa       20100418114020      1
            111        aaa       20090418114020      1
            111        aaa       20080418114020      1
            222        bbb       20100418114020      1
            222        bbb       20090418114020      1
            333        ccc       20100418114020      1
            333        ccc       20080418114020      1要求:同一个ID号只取出其中时间最大的那一条,本人的SQL语句可以实现,但因为数据量太大(几百万条),所以效率非常低,请各位大侠帮忙,有合适的,分数全送,谢谢!
本人的语句:select ID, Name max(LoginTime),Login,IdType form 表 A group by ID
结果应该如下:
             ID        Name       LoginTime        IdType
            111        aaa       20100418114020      1
            222        bbb       20100418114020      1
            333        ccc       20100418114020      1

解决方案 »

  1.   

    --try
    select * from tb a where not exists(select 1 from tn where ID =a.ID 
     and LoginTime>a.LoginTime )
      

  2.   

    select ID,Name,max(LoginTime),IdType 
    form 表 A 
    group by ID,Name,IdType
     
      

  3.   

    非常谢谢,我SQL基本上不懂,先用你的试试!
      

  4.   

    select ID, Name, max(LoginTime),IdType form 表 A group by ID,name,idtype
      

  5.   

    我觉得一下的方法都不是比较快,据说第二种在05下是最快的
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([ID] int,[Name] varchar(3),[LoginTime] bigint,[IdType] int)
    insert [tb]
    select 111,'aaa',20100418114020,1 union all
    select 111,'aaa',20090418114020,1 union all
    select 111,'aaa',20080418114020,1 union all
    select 222,'bbb',20100418114020,1 union all
    select 222,'bbb',20090418114020,1 union all
    select 333,'ccc',20100418114020,1 union all
    select 333,'ccc',20080418114020,1--------------------------------查询开始------------------------------
    --1
    select * from [tb] a where not exists(select 1 from tb where [ID]=a.[ID] and [LoginTime]>a.[LoginTime])
    --2
    select * from [tb] a where [LoginTime]=(select max([LoginTime]) from tb where [ID]=a.[ID])/*
    ID          Name LoginTime            IdType
    ----------- ---- -------------------- -----------
    333         ccc  20100418114020       1
    222         bbb  20100418114020       1
    111         aaa  20100418114020       1(3 行受影响)
    */
      

  6.   

    以上两种方法可以在id和LoginTime建索引,会提升很多效率的
      

  7.   

    请问Beirut:你的意思是说第二种效率很高吗?我搞的个数据库有300万条记录,用我自己的语句太慢!
      

  8.   

    select * from (select ID,Name,LoginTime,IdType,row_number() over(partition by ID order by LoginTime desc ) rn
    form 表A) a
    where rn=1select * from 表A a where exists(select 1 from 表A where id=a.id and a.LoginTime>LoginTime)
      

  9.   

    --如果你的ID,name,idtype都是相同,直接用max , group by 即可
    select ID, Name ,max(LoginTime),Login,IdType form A group by ID , name , idtype--否则用如下select t.* from a t where LoginTime = (select max(LoginTime) from a where id = t.id)select t.* from a t where not exists (select 1 from a where id = t.id and LoginTime > t.LoginTime)
      

  10.   

    我也是见很多专家这么说的,没有测试过,实际上300w的数据对sql2005来说太小意思了
      

  11.   

    05下可以使用分区排名函数试试,索引的原因与使用max需要实际测试下,楼主可以两种方法都试下比较下。1、Max:
       select * from table_name a where a.logintime=(select max(b.loginttime) from table_name b where b.id=a.id)
    2、分区排名函数(05新增,05对分区排名有专门优化的):
       select * from (select *,row_number() over(partition by id order by logintime desc) as r) a where r=1.个人比较倾向分区排名函数. 
      

  12.   


    用这个  效率最高select * from [tb] a where [LoginTime]=(select max([LoginTime]) from tb where [ID]=a.[ID])
      

  13.   

    --------------------------------查询开始------------------------------
    --1
    select * from [tb] a where not exists(select 1 from tb where [ID]=a.[ID] and [LoginTime]>a.[LoginTime])
    --2
    select * from [tb] a where [LoginTime]=(select max([LoginTime]) from tb where [ID]=a.[ID])
    这两种方法都不能去掉相同的行啊,还要加点什么吧?
      

  14.   

    --处理表重复记录(查询和删除)
    /******************************************************************************************************************************************************
    1、Num、Name相同的重复值记录,没有大小关系只保留一条
    2、Name相同,ID有大小关系时,保留大或小其中一个记录
    整理人:中国风(Roy)日期:2008.06.06
    ******************************************************************************************************************************************************/--1、用于查询重复处理记录(如果列没有大小关系时2000用生成自增列和临时表处理,SQL2005用row_number函数处理)--> --> (Roy)生成測試數據if not object_id('Tempdb..#T') is null
        drop table #T
    Go
    Create table #T([ID] int,[Name] nvarchar(1),[Memo] nvarchar(2))
    Insert #T
    select 1,N'A',N'A1' union all
    select 2,N'A',N'A2' union all
    select 3,N'A',N'A3' union all
    select 4,N'B',N'B1' union all
    select 5,N'B',N'B2'
    Go'注意这条备注'
    --I、Name相同ID最小的记录(推荐用1,2,3),方法3在SQl05时,效率高于1、2 
    方法1:
    Select * from #T a where not exists(select 1 from #T where Name=a.Name and ID<a.ID)方法2:
    select a.* from #T a join (select min(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID方法3:
    select * from #T a where ID=(select min(ID) from #T where Name=a.Name)方法4:
    select a.* from #T a join #T b on a.Name=b.Name and a.ID>=b.ID group by a.ID,a.Name,a.Memo having count(1)=1 方法5:
    select * from #T a group by ID,Name,Memo having ID=(select min(ID)from #T where Name=a.Name)方法6:
    select * from #T a where (select count(1) from #T where Name=a.Name and ID<a.ID)=0方法7:
    select * from #T a where ID=(select top 1 ID from #T where Name=a.name order by ID)方法8:
    select * from #T a where ID!>all(select ID from #T where Name=a.Name)方法9(注:ID为唯一时可用):
    select * from #T a where ID in(select min(ID) from #T group by Name)本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/htl258/archive/2009/03/03/3948534.aspx
      

  15.   

    你的数据存在所有列完全相同?select distinct * from tb
      

  16.   

    楼上的所有专家说的有道理,select ID, Name max(LoginTime),Login,IdType form 表 A group by ID,IdType慢的原因很多,检查索引的利用率,表的使用情况(包括碎片程度、索引的填充因子等)
    如果你这是业务表,建议你对表的数据按照类型及时间进行分区,把每个区记录保存在不同的磁盘上,然后对group by 的字段建立组合索引;再结合上面须专家的SQL 写法技巧,速度应该有很大的提高;
      

  17.   

    SQL2005用这条,就没有你说的这个现象了。if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([ID] int,[Name] varchar(3),[LoginTime] bigint,[IdType] int)
    insert [tb]
    select 111,'aaa',20100418114020,1 union all
    select 111,'aaa',20090418114020,1 union all
    select 111,'aaa',20080418114020,1 union all
    select 222,'bbb',20100418114020,1 union all
    select 222,'bbb',20090418114020,1 union all
    select 333,'ccc',20100418114020,1 union all
    select 333,'ccc',20080418114020,1
    select ID,Name,LoginTime,IdType 
    from (
    select rn=ROW_NUMBER()over(partition by id order by [LoginTime] desc),* 
    from tb) t 
    where rn=1
    /*
    ID          Name LoginTime            IdType
    ----------- ---- -------------------- -----------
    111         aaa  20100418114020       1
    222         bbb  20100418114020       1
    333         ccc  20100418114020       1(3 行受影响)
    */
      

  18.   

    select ID,Name,max(LoginTime),IdType 
    form 表 A 
    group by ID,Name,IdType
    这个查出来的ID怎么还有重复的值啊?
      

  19.   

    select ID,Name,max(LoginTime),IdType  
    form 表 A  
    group by ID,Name,IdType
    好像除了这一句外其它的效率都没有我那条高啊,但是这句查出来的有重复值!