如何把查出来的结果集每行加上序号?

wudileiyu 2008-01-23 12:33:04
Oracle中这么写:
SELECT rownum as 序列

在SqlServer中如何写呢?谢谢!
...全文
511 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wudileiyu 2008-01-23
  • 打赏
  • 举报
回复
select  a.type,b.type ,count(*) 
FROM a ,b
where b.r_id=a.r_id
GROUP BY a.type,b.type

把这段代码加上序列,按照二楼的方法要如何写呢?
-狙击手- 2008-01-23
  • 打赏
  • 举报
回复
2000

select 序列 = (select count(1)+ 1 from ta where a.id > id)
,*
froom ta a
-狙击手- 2008-01-23
  • 打赏
  • 举报
回复
2005中
row_number()
wudileiyu 2008-01-23
  • 打赏
  • 举报
回复
是2000的数据库
happyflystone
无枪狙击手 的
select 序列 = (select count(1)+ 1 from ta where a.id > id)
,*
froom ta a

这个怎么用呢?
dawugui 2008-01-23
  • 打赏
  • 举报
回复
--2000
--升序用<,降序用>
select px = (select count(1) from
(
select a.type,b.type ,count(*) cnt FROM a ,b where b.r_id=a.r_id GROUP BY a.type,b.type
) m where cnt < n.cnt) + 1,* from
(
select a.type,b.type ,count(*) cnt FROM a ,b where b.r_id=a.r_id GROUP BY a.type,b.type
) n
)
--直接顺序用px = identity(int,1,1)
select px=identity(int,1,1) ,a.type,b.type ,count(*) cnt into tmp FROM a ,b where b.r_id=a.r_id GROUP BY a.type,b.type
select * from tmp

--2005用row_number()
select a.type,b.type ,count(*) cnt , px = row_number() over(order by a.type , b.type) FROM a ,b where b.r_id=a.r_id GROUP BY a.type,b.type
wzy_love_sly 2008-01-23
  • 打赏
  • 举报
回复
declare @tb table (v varchar(80))
insert into @tb select 's'
insert into @tb select 'b'
insert into @tb select 'c'
insert into @tb select 'o'
select id=row_number() over (order by v),v from @tb


id v
1 b
2 c
3 o
4 s

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧