22,209
社区成员
发帖
与我相关
我的任务
分享
select
*
from
tb t
where
isnull(forenoticeid2,0)>0
and
id=(select max(id) from tb where name=t.name )
create table tb1 (id int,playTime datetime,name varchar(34),forenoticeid2 int)
insert into tb1
select 40102449,'2011-10-21 23:45:00.000','MTV国际音乐榜',null union all
select 40102448,'2011-10-21 23:00:00.000','东方夜新闻',75428 union all
select 40102447,'2011-10-21 22:30:00.000','夜新闻(天气)',null union all
select 40102446,'2011-10-21 21:18:00.000','《当婆婆遇上妈》包装',75709 union all
select 40102445,'2011-10-21 21:15:00.000','百里挑一',74173 union all
select 40102444,'2011-10-21 21:12:00.000','《当婆婆遇上妈》特别节目(10月21日)',75709 union all
select 40102443,'2011-10-21 20:29:00.000','黄金剧场:当婆婆遇上妈(29)',75709 union all
select 40102442,'2011-10-21 20:23:00.000','《当婆婆遇上妈》包装',75709 union all
select 40102441,'2011-10-21 19:35:00.000','黄金剧场:当婆婆遇上妈(28)',75709
select a.*
from tb1 a
inner join
(select name,max(playtime) mplaytime
from tb1
where forenoticeid2 is not null and forenoticeid2>0
group by name) b on a.name=b.name and a.playtime=b.mplaytime
order by a.playTime desc
id playTime name forenoticeid2
----------- ----------------------- ---------------------------------- -------------
40102448 2011-10-21 23:00:00.000 东方夜新闻 75428
40102446 2011-10-21 21:18:00.000 《当婆婆遇上妈》包装 75709
40102445 2011-10-21 21:15:00.000 百里挑一 74173
40102444 2011-10-21 21:12:00.000 《当婆婆遇上妈》特别节目(10月21日) 75709
40102443 2011-10-21 20:29:00.000 黄金剧场:当婆婆遇上妈(29) 75709
40102441 2011-10-21 19:35:00.000 黄金剧场:当婆婆遇上妈(28) 75709
create table [tb] (id int,playTime datetime,name varchar(34),forenoticeid2 int)
insert into [tb]
select 40102449,'2011-10-21 23:45:00.000','MTV国际音乐榜',null union all
select 40102448,'2011-10-21 23:00:00.000','东方夜新闻',75428 union all
select 40102447,'2011-10-21 22:30:00.000','夜新闻(天气)',null union all
select 40102446,'2011-10-21 21:18:00.000','《当婆婆遇上妈》包装',75709 union all
select 40102445,'2011-10-21 21:15:00.000','百里挑一',74173 union all
select 40102444,'2011-10-21 21:12:00.000','《当婆婆遇上妈》特别节目(10月21日)',75709 union all
select 40102443,'2011-10-21 20:29:00.000','黄金剧场:当婆婆遇上妈(29)',75709 union all
select 40102442,'2011-10-21 20:23:00.000','《当婆婆遇上妈》包装',75709 union all
select 40102441,'2011-10-21 19:35:00.000','黄金剧场:当婆婆遇上妈(28)',75709
go
select * from tb a where isnull(forenoticeid2,0)>0
and not exists(select 1 from tb where name=a.name and id>a.id)
/*
id playTime name forenoticeid2
----------- ----------------------- ---------------------------------- -------------
40102448 2011-10-21 23:00:00.000 东方夜新闻 75428
40102446 2011-10-21 21:18:00.000 《当婆婆遇上妈》包装 75709
40102445 2011-10-21 21:15:00.000 百里挑一 74173
40102444 2011-10-21 21:12:00.000 《当婆婆遇上妈》特别节目(10月21日) 75709
40102443 2011-10-21 20:29:00.000 黄金剧场:当婆婆遇上妈(29) 75709
40102441 2011-10-21 19:35:00.000 黄金剧场:当婆婆遇上妈(28) 75709
(6 行受影响)
*/
go
drop table tb
create table [tb] (id int,playTime datetime,name varchar(34),forenoticeid2 int)
insert into [tb]
select 40102449,'2011-10-21 23:45:00.000','MTV国际音乐榜',null union all
select 40102448,'2011-10-21 23:00:00.000','东方夜新闻',75428 union all
select 40102447,'2011-10-21 22:30:00.000','夜新闻(天气)',null union all
select 40102446,'2011-10-21 21:18:00.000','《当婆婆遇上妈》包装',75709 union all
select 40102445,'2011-10-21 21:15:00.000','百里挑一',74173 union all
select 40102444,'2011-10-21 21:12:00.000','《当婆婆遇上妈》特别节目(10月21日)',75709 union all
select 40102443,'2011-10-21 20:29:00.000','黄金剧场:当婆婆遇上妈(29)',75709 union all
select 40102442,'2011-10-21 20:23:00.000','《当婆婆遇上妈》包装',75709 union all
select 40102441,'2011-10-21 19:35:00.000','黄金剧场:当婆婆遇上妈(28)',75709
--开始查询
--SQL2005
;with cte as(
select rid=ROW_NUMBER() over (PARTITION by name order by playTime desc),*
from tb
where ISNULL(forenoticeid2,0)>0
)
select id,playTime,name,forenoticeid2 from cte where rid=1
order by 2 desc
--SQL2000
select *
from tb t
where not exists (select 1 from tb where name = t.name and playTime > t.playTime)
and forenoticeid2 is not null
order by playTime desc
--结束查询
drop table [tb]
/*****************
id playTime name forenoticeid2
----------- ----------------------- ---------------------------------- -------------
40102448 2011-10-21 23:00:00.000 东方夜新闻 75428
40102446 2011-10-21 21:18:00.000 《当婆婆遇上妈》包装 75709
40102445 2011-10-21 21:15:00.000 百里挑一 74173
40102444 2011-10-21 21:12:00.000 《当婆婆遇上妈》特别节目(10月21日) 75709
40102443 2011-10-21 20:29:00.000 黄金剧场:当婆婆遇上妈(29) 75709
40102441 2011-10-21 19:35:00.000 黄金剧场:当婆婆遇上妈(28) 75709
(6 行受影响)
--lz的需求和结果不符,正解如下:
--> 测试数据: [tb]
if object_id('[tb]') is not null drop table [tb]
create table [tb] (id int,playTime datetime,name varchar(34),forenoticeid2 int)
insert into [tb]
select 40102449,'2011-10-21 23:45:00.000','MTV国际音乐榜',null union all
select 40102448,'2011-10-21 23:00:00.000','东方夜新闻',75428 union all
select 40102447,'2011-10-21 22:30:00.000','夜新闻(天气)',null union all
select 40102446,'2011-10-21 21:18:00.000','《当婆婆遇上妈》包装',75709 union all
select 40102445,'2011-10-21 21:15:00.000','百里挑一',74173 union all
select 40102444,'2011-10-21 21:12:00.000','《当婆婆遇上妈》特别节目(10月21日)',75709 union all
select 40102443,'2011-10-21 20:29:00.000','黄金剧场:当婆婆遇上妈(29)',75709 union all
select 40102442,'2011-10-21 20:23:00.000','《当婆婆遇上妈》包装',75709 union all
select 40102441,'2011-10-21 19:35:00.000','黄金剧场:当婆婆遇上妈(28)',75709
--开始查询
;with cte as(
select rid=ROW_NUMBER() over (PARTITION by name order by playTime desc),*
from tb
where ISNULL(forenoticeid2,0)>0
)
select id,playTime,name,forenoticeid2 from cte where rid=1
order by 2 desc
--结束查询
drop table [tb]
/*
id playTime name forenoticeid2
----------- ----------------------- ---------------------------------- -------------
40102448 2011-10-21 23:00:00.000 东方夜新闻 75428
40102446 2011-10-21 21:18:00.000 《当婆婆遇上妈》包装 75709
40102445 2011-10-21 21:15:00.000 百里挑一 74173
40102444 2011-10-21 21:12:00.000 《当婆婆遇上妈》特别节目(10月21日) 75709
40102443 2011-10-21 20:29:00.000 黄金剧场:当婆婆遇上妈(29) 75709
40102441 2011-10-21 19:35:00.000 黄金剧场:当婆婆遇上妈(28) 75709
(6 行受影响)