親テーブルのレコードのサブセットを持つテーブルがあります。 各サブセットには、[rank]
フィールドに格納されているオーダーがあります。 フィールドotherRank
に保持されている新しい注文に基づいて、このテーブルの特定のサブセットに対してこのフィールドを更新する必要があります。T-SQL - Row_Number()でレコードのサブセットを更新する
それをカバーし、次の処理を行います。
update mytable t1 set
[rank] = t2.new_rank_num
from (select t2.id, new_rank_num = row_number() over (order by t2.otherRank)
from mytable t2 where t2.parentID = 628) t2
where t1.id = t2.id
か、私が必要になります。
update mytable t1 set
[rank] = t2.new_rank_num
from (select t2.id, new_rank_num = row_number() over (order by t2.otherRank)
from mytable t2 where t2.parentID = 628) t2
where t1.id = t2.id and t1.parentID = 628
私の特定の問題は、私はPARENTID 628
の送金の外の更新には、何もしたくないです編集 これを実行しようとするとエラーが発生します:
t2
近くt1
正しくない構文の近くに不正な構文ので、私は構文があることが必要であると思っています:
update mytable set
[rank] = t2.new_rank_num
from (select id, new_rank_num = row_number() over (order by otherRank)
from mytable where parentID = 628) t2
where id = t2.id and parentID = 628
編集2
OK、私は」 SqlZimの推奨するCTEソリューションを利用してください。
;with cte as (
select t2.id, new_rank_num = row_number() over (order by t2.otherRank)
from mytable t2 where t2.parentID = 628
)
update t1 set
[rank] = t2.new_rank_num
from mytable t1
inner join cte t2 on t1.id = t2.id
あなたがそれをしようとするとどうなりますか? –
私は主にクエリの構文に関するアドバイスが必要なので、まだ試してみませんでした。 – scgough
私は文句に間違って何も表示されません。希望の結果が得られた場合は、それに従ってください。 –