日期:2014-05-18 浏览次数:20395 次
select a.name,a.context,b.name,a.dateandtime from article a, type c where a.id in (select top 3 id from article b where a.typeid=b.typeid order by dateandtime desc) and a.typeid=b.typeid
------解决方案--------------------
select m.name,m.context,n.name , m.dateandtime from ( select t.* from article t where dateandtime in (select top 3 dateandtime from article where typeid = t.typeid order by dateandtime desc) ) m , type n where m.typeid = t.id
------解决方案--------------------
create table article(id int,name varchar(10),context varchar(20),typeid int,dateandtime datetime) insert into article values(1 , 'test1', '<long text>', 1 , '2008/03/24 18:00:00') insert into article values(2 , 'test2', '<long text>', 1 , '2008/03/24 18:01:00') insert into article values(3 , 'test3', '<long text>', 2 , '2008/03/24 18:00:00') insert into article values(4 , 'test4', '<long text>', 2 , '2008/03/24 18:02:00') insert into article values(5 , 'test5', '<long text>', 2 , '2008/03/24 18:03:00') insert into article values(6 , 'test6', '<long text>', 2 , '2008/03/24 18:04:00') create table type(Id int , name varchar(10)) insert into type values(1 ,'国际新闻') insert into type values(2 ,'国内新闻') go select m.name,m.context,n.name , m.dateandtime from ( select t.* from article t where dateandtime in (select top 3 dateandtime from article where typeid = t.typeid order by dateandtime desc) ) m , type n where m.typeid = n.id drop table article, type /* name context name dateandtime ---------- -------------------- ---------- ------------------------------------------------------ test1 <long text> 国际新闻 2008-03-24 18:00:00.000 test2 <long text> 国际新闻 2008-03-24 18:01:00.000 test4 <long text> 国内新闻 2008-03-24 18:02:00.000 test5 <long text> 国内新闻 2008-03-24 18:03:00.000 test6 <long text> 国内新闻 2008-03-24 18:04:00.000 (所影响的行数为 5 行) */
------解决方案--------------------
sql2005
select row_number() over (partition by a order by b)as row ,* from(select 'a' a,'b' b union select 'a','c' union select 'b','a')a /* row a b -------------------- -