0

以下のような表があります。

Id   ||  ParentId ||  Other Columns ... 
=======================================
1    ||  1        || ...
2    ||  1        || ...
3    ||  1        || ...
1    ||  2        || ...
2    ||  2        || ...
3    ||  2        || ...
1    ||  3        || ...
2    ||  3        || ...

列には、 (独自の数値計算)[Id]に基づいて自動インクリメントされた値が必要です。[ParentId]

この目標を達成するための最良の方法は何ですか?

4

2 に答える 2

2

これを試して

select ROW_NUMBER() over(partition by parentId order by <any other column>) ID,
ParentId,<other columns>  
from  yourtable

edit1:

where句でIDを使用する場合

with cte as(                                
select ROW_NUMBER() over(partition by parentId order by <any other column>) ID,
ParentId,<other columns>  
from  yourtable)
select * from cte where ID=(some value)
于 2012-07-26T10:23:24.997 に答える
0

デモ

http://sqlfiddle.com/#!3/32645/6

更新されたhttp://sqlfiddle.com/#!3/32645/13

create table dummy(parentId int)

insert into dummy values(1)
insert into dummy values(1)
insert into dummy values(2)
insert into dummy values(1)
insert into dummy values(4)
insert into dummy values(4)
insert into dummy values(5)
insert into dummy values(1)

select row_number() over (order by parentID) as ID,parentID from dummy
于 2012-07-26T10:10:36.723 に答える