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
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
select * from tb a where not exists(select 1 from tn where ID =a.ID
and LoginTime>a.LoginTime )
form 表 A
group by ID,Name,IdType
--> 测试数据:[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 行受影响)
*/
form 表A) a
where rn=1select * from 表A a where exists(select 1 from 表A where id=a.id and a.LoginTime>LoginTime)
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)
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.个人比较倾向分区排名函数.
用这个 效率最高select * from [tb] a where [LoginTime]=(select max([LoginTime]) from tb where [ID]=a.[ID])
--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])
这两种方法都不能去掉相同的行啊,还要加点什么吧?
/******************************************************************************************************************************************************
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
如果你这是业务表,建议你对表的数据按照类型及时间进行分区,把每个区记录保存在不同的磁盘上,然后对group by 的字段建立组合索引;再结合上面须专家的SQL 写法技巧,速度应该有很大的提高;
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 行受影响)
*/
form 表 A
group by ID,Name,IdType
这个查出来的ID怎么还有重复的值啊?
form 表 A
group by ID,Name,IdType
好像除了这一句外其它的效率都没有我那条高啊,但是这句查出来的有重复值!