create table tb (id int identity(1,1), name varchar(50), addtime datetime)insert tb select 'aaa','2003-1-1' union all select 'bbb','2003-7-1' union all select 'ccc','2003-6-1' union all select 'ddd','2003-5-1' union all select 'eee','2003-4-1' union all select 'fff','2003-3-1' union all select 'ggg','2003-2-1' union all select 'hhh','2003-1-10' union all select 'iii','2003-1-21' union all select 'jjj','2003-1-17' union all select 'kkk','2003-1-13' union all select 'lll','2003-1-16' union all select 'mmm','2003-1-15' union all select 'ooo','2003-1-14' union all select 'aaa','2003-1-13' union all select 'ccc','2003-1-12' union all select 'aaa','2003-1-11' select top 10 * from tb where name in (select distinct(name) from tb) order by addtimedrop table tb/* 测试结果 id name addtime ----------- -------------------------------------------------- ------------------------------------------------------ 1 aaa 2003-01-01 00:00:00.000 8 hhh 2003-01-10 00:00:00.000 17 aaa 2003-01-11 00:00:00.000 16 ccc 2003-01-12 00:00:00.000 11 kkk 2003-01-13 00:00:00.000 15 aaa 2003-01-13 00:00:00.000 14 ooo 2003-01-14 00:00:00.000 13 mmm 2003-01-15 00:00:00.000 12 lll 2003-01-16 00:00:00.000 10 jjj 2003-01-17 00:00:00.000(所影响的行数为 10 行) */
create table tb (id int identity(1,1), name varchar(50), addtime datetime)insert tb select 'aaa','2003-1-1' union all select 'bbb','2003-7-1' union all select 'ccc','2003-6-1' union all select 'ddd','2003-5-1' union all select 'eee','2003-4-1' union all select 'fff','2003-3-1' union all select 'ggg','2003-2-1' union all select 'hhh','2003-1-10' union all select 'iii','2003-1-21' union all select 'jjj','2003-1-17' union all select 'kkk','2003-1-13' union all select 'lll','2005-1-16' union all select 'mmm','2003-1-15' union all select 'ooo','2003-1-14' union all select 'aaa','2003-1-13' union all select 'ccc','2007-1-12' union all select 'aaa','2003-1-11' select top 10 * from tb a where not exists(select * from tb b where a.name=b.name and a.id<b.id) order by addtime desc drop table tb/* id name addtime ----------- -------------------------------------------------- ------------------------------------------------------ 16 ccc 2007-01-12 00:00:00.000 12 lll 2005-01-16 00:00:00.000 2 bbb 2003-07-01 00:00:00.000 4 ddd 2003-05-01 00:00:00.000 5 eee 2003-04-01 00:00:00.000 6 fff 2003-03-01 00:00:00.000 7 ggg 2003-02-01 00:00:00.000 9 iii 2003-01-21 00:00:00.000 10 jjj 2003-01-17 00:00:00.000 13 mmm 2003-01-15 00:00:00.000(所影响的行数为 10 行) */
where not exists(select * from 表a b where a.name=b.name and a.id<b.id)
order by addtime
where name in
(
select distinct name from A
)
order by addtime desc
select top 10 addtime distinct name
from A
order by addtime desc 没有调试
你去试看
顶!!!
前面的都是垃圾
回复人:yfenfang(天上的小神) 句子写的没错不过
加上order by就是多此一举了,没理解楼主的意思!!!
(id int identity(1,1),
name varchar(50),
addtime datetime)insert tb
select 'aaa','2003-1-1' union all
select 'bbb','2003-7-1' union all
select 'ccc','2003-6-1' union all
select 'ddd','2003-5-1' union all
select 'eee','2003-4-1' union all
select 'fff','2003-3-1' union all
select 'ggg','2003-2-1' union all
select 'hhh','2003-1-10' union all
select 'iii','2003-1-21' union all
select 'jjj','2003-1-17' union all
select 'kkk','2003-1-13' union all
select 'lll','2003-1-16' union all
select 'mmm','2003-1-15' union all
select 'ooo','2003-1-14' union all
select 'aaa','2003-1-13' union all
select 'ccc','2003-1-12' union all
select 'aaa','2003-1-11'
select top 10 * from tb where name in (select distinct(name) from tb) order by addtimedrop table tb/*
测试结果
id name addtime
----------- -------------------------------------------------- ------------------------------------------------------
1 aaa 2003-01-01 00:00:00.000
8 hhh 2003-01-10 00:00:00.000
17 aaa 2003-01-11 00:00:00.000
16 ccc 2003-01-12 00:00:00.000
11 kkk 2003-01-13 00:00:00.000
15 aaa 2003-01-13 00:00:00.000
14 ooo 2003-01-14 00:00:00.000
13 mmm 2003-01-15 00:00:00.000
12 lll 2003-01-16 00:00:00.000
10 jjj 2003-01-17 00:00:00.000(所影响的行数为 10 行)
*/
create table tb
(id int identity(1,1),
name varchar(50),
addtime datetime)insert tb
select 'aaa','2003-1-1' union all
select 'bbb','2003-7-1' union all
select 'ccc','2003-6-1' union all
select 'ddd','2003-5-1' union all
select 'eee','2003-4-1' union all
select 'fff','2003-3-1' union all
select 'ggg','2003-2-1' union all
select 'hhh','2003-1-10' union all
select 'iii','2003-1-21' union all
select 'jjj','2003-1-17' union all
select 'kkk','2003-1-13' union all
select 'lll','2005-1-16' union all
select 'mmm','2003-1-15' union all
select 'ooo','2003-1-14' union all
select 'aaa','2003-1-13' union all
select 'ccc','2007-1-12' union all
select 'aaa','2003-1-11' select top 10 * from tb a
where not exists(select * from tb b where a.name=b.name and a.id<b.id)
order by addtime desc
drop table tb/*
id name addtime
----------- -------------------------------------------------- ------------------------------------------------------
16 ccc 2007-01-12 00:00:00.000
12 lll 2005-01-16 00:00:00.000
2 bbb 2003-07-01 00:00:00.000
4 ddd 2003-05-01 00:00:00.000
5 eee 2003-04-01 00:00:00.000
6 fff 2003-03-01 00:00:00.000
7 ggg 2003-02-01 00:00:00.000
9 iii 2003-01-21 00:00:00.000
10 jjj 2003-01-17 00:00:00.000
13 mmm 2003-01-15 00:00:00.000(所影响的行数为 10 行)
*/