当前位置: 代码迷 >> Sql Server >> 请教SQL Server 中是否有类似ORACLE 中分区函数patition by 的机能
  详细解决方案

请教SQL Server 中是否有类似ORACLE 中分区函数patition by 的机能

热度:98   发布时间:2016-04-27 18:43:00.0
请问SQL Server 中是否有类似ORACLE 中分区函数patition by 的机能
现在有两个表article(文章) 和type(文章分类) 表结构如下
article:
  1. id int(4) 
  2. name varchar2(10)
  3. context ntext(200)
  4. typeid int(4)
  5. dateandtime smalldatetime(4)
type:
1. id int(4)
2. name varchar(10)

两个表通过article.typeid = type.id关联。现在想用1条sql检索 article中每个分类的最新三条记录。
例如;
Article 
  Id name context typeid dateandtime
  1 test1 <long text> 1 2008/03/24 18:00:00
  2 test2 <long text> 1 2008/03/24 18:01:00
  3 test3 <long text> 2 2008/03/24 18:00:00
  4 test4 <long text> 2 2008/03/24 18:02:00
  5 test5 <long text> 2 2008/03/24 18:03:00
  6 test6 <long text> 2 2008/03/24 18:04:00
Type
  Id name
1 国际新闻
2 国内新闻

那么我需要的检索结果是
Test6 <long text> 国内新闻 2008/03/24 18:04:00
Test5 <long text> 国内新闻 2008/03/24 18:03:00
Test4 <long text> 国内新闻 2008/03/24 18:02:00
Test2 <long text> 国内新闻 2008/03/24 18:01:00
Test1 <long text> 国内新闻 2008/03/24 18:00:00

------解决方案--------------------
SQL code
select a.name,a.context,b.name,a.dateandtime from article a, type cwhere a.id in (select top 3 id  from article b where a.typeid=b.typeid order by dateandtime desc)and  a.typeid=b.typeid
------解决方案--------------------
SQL code
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 nwhere m.typeid = t.id
------解决方案--------------------
SQL code
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 ,'国内新闻')goselect 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 nwhere m.typeid = n.iddrop table article, type/*name       context              name       dateandtime                                            ---------- -------------------- ---------- ------------------------------------------------------ test1      <long text>          国际新闻       2008-03-24 18:00:00.000test2      <long text>          国际新闻       2008-03-24 18:01:00.000test4      <long text>          国内新闻       2008-03-24 18:02:00.000test5      <long text>          国内新闻       2008-03-24 18:03:00.000test6      <long text>          国内新闻       2008-03-24 18:04:00.000(所影响的行数为 5 行)*/
------解决方案--------------------
sql2005

SQL code
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-------------------- ---- ----1                    a    b2                    a    c1                    b    a(3 行受影响)*/
  相关解决方案