尝试对以下结果集运行更新:
Row# ProductRankID ProductID ProductCategoryID ProductTypeID Rank Score 1 3 11266 9 80 0 765 2 14 25880 9 80 0 656 3 12 25864 9 80 0 547 4 7 11252 9 80 0 457 5 8 25719 9 80 0 456 6 4 13425 9 80 0 456 7 11 25677 9 80 0 456 8 9 25716 9 80 0 432 9 15 25714 9 80 0 324 10 13 13589 9 80 0 234 11 20 25803 9 80 0 234 12 17 25715 9 80 0 213 13 5 21269 9 80 0 154 14 10 25867 9 80 0 123 15 16 25676 9 80 0 123 16 22 17861 9 80 0 67 17 19 13534 9 80 0 55 18 23 13659 9 80 0 54 19 29 13658 9 80 0 34 20 21 13591 9 80 0 32 21 6 11249 9 80 0 23 22 18 11253 9 80 0 12 23 28 11253 9 87 0 65 24 27 13664 9 87 0 45 25 25 13658 9 87 0 14 26 26 13657 9 87 0 13 27 24 13659 9 87 0 13 28 30 11252 9 87 0 12 29 2 12345 11 80 0 324
我想要" Rank"根据每一行设置1 ... 2..3..4等列。然后在更改ProductCategoryID + ProductTypeID时,我希望它重置为1 ... 2 ... 3 ... 4等。
所以结果看起来应该是这样的:
Row# ProductRankID ProductID ProductCategoryID ProductTypeID Rank Score 1 3 11266 9 80 1 765 2 14 25880 9 80 2 656 3 12 25864 9 80 3 547 4 7 11252 9 80 4 457 5 8 25719 9 80 5 456 6 4 13425 9 80 6 456 7 11 25677 9 80 7 456 8 9 25716 9 80 8 432 9 15 25714 9 80 9 324 10 13 13589 9 80 10 234 11 20 25803 9 80 11 234 12 17 25715 9 80 12 213 13 5 21269 9 80 13 154 23 28 11253 9 87 1 65 24 27 13664 9 87 2 45 25 25 13658 9 87 3 14 26 26 13657 9 87 4 13 27 24 13659 9 87 5 13 28 30 11252 9 87 6 12 29 2 12345 11 80 1 324
希望有道理吗?
谢谢, 里奇
答案 0 :(得分:1)
如果您想要select
:
select t.*,
row_number() over (partition by ProductCategoryID, ProductTypeID
order by score desc, productid
) as new_rank
from t;
如果您需要update
,请使用CTE:
with toupdate as (
select t.*,
row_number() over (partition by ProductCategoryID, ProductTypeID
order by score desc, productid
) as new_rank
from t
)
update toupdate
set rank = new_rank;