select top 20 TagName from ( select distinct TagName from tag order by addtime desc )t
--我想选出最新的20条标签的名称,且去掉重复的:create table tag(tagid int,tagname varchar(20),addtime datetime) insert tag select 1,'星际','2005-9-10' union all select 2,'科技','2005-9-11' union all select 3,'聊天','2005-9-12' union all select 4,'下载','2005-9-13' union all select 5,'下载','2005-9-14' union all select 6,'体育','2005-9-15' union all select 7,'星际','2005-9-16' union all select 9,'星际','2005-9-11' union all select 8,'科技','2005-9-17'go select distinct tagname from ( select top 100 percent TagName from tag order by addtime desc)t drop table tag
declare @tag table(TagID int,TagName varchar(10),AddTime datetime) insert into @tag select '1','星際','2005-9-10'union all select '2','科技','2005-9-11'union all select '3','聊天','2005-9-12'union all select '4','下載','2005-9-13'union all select '5','下載','2005-9-14'union all select '6','體育','2005-9-15'union all select '7','星際','2005-9-16'union all select '8','科技','2005-9-17' select distinct tagname from ( select top 20 tagname from @tag order by addtime desc) as a tagname ---------- 下載 星際 科技 聊天 體育(影響 5 個資料列)
更正一下:select top 20 TagName from ( select distinct top 100 percent TagName from tag order by addtime desc )t
select top 20 a.TagName from tag a where a.TagID = (select top 1 TagID from tag where TagName=a.TagName order by AddTime desc) order by a.AddTime desc你照我说的这样去查询,肯定是你想要的,我试过!!!!!
from (
select distinct TagName
from tag
order by addtime desc
)t
insert tag
select 1,'星际','2005-9-10' union all
select 2,'科技','2005-9-11' union all
select 3,'聊天','2005-9-12' union all
select 4,'下载','2005-9-13' union all
select 5,'下载','2005-9-14' union all
select 6,'体育','2005-9-15' union all
select 7,'星际','2005-9-16' union all
select 9,'星际','2005-9-11' union all
select 8,'科技','2005-9-17'go
select distinct tagname from (
select top 100 percent TagName from tag order by addtime desc)t
drop table tag
insert into @tag
select '1','星際','2005-9-10'union all
select '2','科技','2005-9-11'union all
select '3','聊天','2005-9-12'union all
select '4','下載','2005-9-13'union all
select '5','下載','2005-9-14'union all
select '6','體育','2005-9-15'union all
select '7','星際','2005-9-16'union all
select '8','科技','2005-9-17'
select distinct tagname from (
select top 20 tagname from @tag order by addtime desc) as a
tagname
----------
下載
星際
科技
聊天
體育(影響 5 個資料列)
from (
select distinct top 100 percent TagName
from tag
order by addtime desc
)t
a.TagName
from
tag a
where
a.TagID = (select top 1 TagID from tag where TagName=a.TagName order by AddTime desc)
order by
a.AddTime desc你照我说的这样去查询,肯定是你想要的,我试过!!!!!