oracle 分区条件加函数,请教SQL Server 中是否有类似ORACLE 中分区函数patition by 的机能...

SQL codecreate table article(id int,name varchar(10),context varchar(20),typeid int,dateandtime datetime)

insert into article values(1 , 'test1', '', 1 , '2008/03/24 18:00:00')

insert into article values(2 , 'test2', '', 1 , '2008/03/24 18:01:00')

insert into article values(3 , 'test3', '', 2 , '2008/03/24 18:00:00')

insert into article values(4 , 'test4', '', 2 , '2008/03/24 18:02:00')

insert into article values(5 , 'test5', '', 2 , '2008/03/24 18:03:00')

insert into article values(6 , 'test6', '', 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 国际新闻 2008-03-24 18:00:00.000

test2 国际新闻 2008-03-24 18:01:00.000

test4 国内新闻 2008-03-24 18:02:00.000

test5 国内新闻 2008-03-24 18:03:00.000

test6 国内新闻 2008-03-24 18:04:00.000

(所影响的行数为 5 行)

*/

------解决方案--------------------

sql2005SQL codeselect 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 b

2 a c

1 b a

(3 行受影响)*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值