62,269
社区成员
发帖
与我相关
我的任务
分享
if object_id('news') is not null drop table news
create table news (ID int identity(1,1),[type] int,[update] datetime)
insert into news
select 1,'2009-01-01' union all
select 1,'2009-01-02' union all
select 2 ,'2009-01-03' union all
select 3 ,'2009-01-04' union all
select 3,'2009-01-05' union all
select 3 ,'2009-01-06' union all
select 2 ,'2009-01-07' union all
select 2 ,'2009-01-08'
select * from news aa
where ID in (select top 1 ID from news where [type]=aa.[type] order by [update] desc)
order by aa.[type]
select * from @news aa
where id in (select top 1 id from @news where type=aa.type order by [update] desc)
order by aa.type asc,aa.id descdeclare @news table(
[id] [int] IDENTITY (1, 1) NOT NULL ,
[type] [int] NOT NULL ,
[update] [datetime] NOT NULL
)
insert into @news
select 1,'2009-1-6'
union select 2,'2009-2-4'
union select 3,'2009-2-2'
union select 2,'2009-2-3'
union select 1,'2009-2-1'
union select 2,'2009-1-4'
union select 3,'2009-1-5'
select * from @news aa
where id in (select top 1 id from @news where type=aa.type order by [update] desc)
order by aa.type
/*结果
2 1 2009-02-01 00:00:00.000
5 2 2009-02-04 00:00:00.000
7 3 2009-02-02 00:00:00.000
*/select * from news aa
where id in (select top 1 id from news where type=aa.type order by [update] desc)
order by aa.typeselect * from news aa
where id in (select top 1 id from news where type=aa.type order by update desc)
order by aa.typeselect * from news aa
where id in (select top 1 id from news where type=aa.type order by update desc)
order by aa.type
select * from Table_1 t1 where [update] =
(select max([update]) from Table_1 t2 where t2.[type]=t1.[type])
and id=(select MAX(id) from Table_1 t3 where t3.[type]=t1.[type])
order by id
select * from Table_1 t1 where [update] =
(select max([update]) from Table_1 t2 where t2.[type]=t1.[type])
order by id