row_number()函数纠结的用法。

cheniqit1 2011-04-26 11:31:40
table title
id name
1 2
2 3

table line
id title_id name key_index
1 1 A 2
2 1 B 4
3 2 C 1


用row_number()函数根据key_index顺序,注意此字段不连续、
出现如下结果
line_id name row_name
1 A 1
2 B 2
3 C 1
...全文
95 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
快溜 2011-04-26
  • 打赏
  • 举报
回复
declare @Table table (line_ID int,TITLE_ID int,NAME varchar(4),KEY_INDEX int)
insert into @Table
select 1,1,'A',2 union all
select 2,1,'B',4 union all
select 3,2,'C',1

select line_ID,name,row_name=row_number() over(partition by title_id order by key_index)
from @Table
/*
line_ID name row_name
----------- ---- --------------------
1 A 1
2 B 2
3 C 1
叶子 2011-04-26
  • 打赏
  • 举报
回复

declare @line table (id int,title_id int,name varchar(1),key_index int)
insert into @line
select 1,1,'A',2 union all
select 2,1,'B',4 union all
select 3,2,'C',1

select id,name,row_number() over(partition by title_id order by id) as row_name
from @line
/*
id name row_name
----------- ---- --------------------
1 A 1
2 B 2
3 C 1
*/
快溜 2011-04-26
  • 打赏
  • 举报
回复
select *,row_name=row_number() over(partition by title_id order by key_index)
from line
kingtiy 2011-04-26
  • 打赏
  • 举报
回复
row_number()over(order by key_index) as row_name
--小F-- 2011-04-26
  • 打赏
  • 举报
回复
select
id,name,row_number() over(partition by title_id order by id) as row_name
from
tb

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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