我有一个这样的 sql 表:
表用户
Id Name Country rank total
+----+---------------+---------------+-----------------+-------------+
1 John Canada
2 Kate Canada
3 Mark Canada
4 Max Argentina
5 Sam Argentina
6 Stacy China
7 Ken China
8 jack China
9 Don China
我想填写rank
和total
值如下:
Id Name Country rank total
+----+---------------+---------------+-----------------+-------------+
1 John Canada 1 3
2 Kate Canada 2 3
3 Mark Canada 3 3
4 Max Argentina 1 2
5 Sam Argentina 2 2
6 Stacy China 1 4
7 Ken China 2 4
8 jack China 3 4
9 Don China 4 4
总数基本上是每个国家/地区的总数,排名只是该国家/地区的计数 1,2,3,4....(每个国家都会重置)。
为此,我尝试了以下方法:
update Users
set rank=u.tempRank, total=u.tempTotal
from
(select *,
row_number() over (partition by [Country] order by newid()) as tempRank,
count(*) over (partition by [Country]) as tempTotal
from Users) as u
然而rank
和total
都等于第一个 tempRank 和 tempTotal,我得到了这张表
Id Name Country rank total
+----+---------------+---------------+-----------------+-------------+
1 John Canada 1 3
2 Kate Canada 1 3
3 Mark Canada 1 3
4 Max Argentina 1 3
5 Sam Argentina 1 3
6 Stacy China 1 3
7 Ken China 1 3
8 jack China 1 3
9 Don China 1 3
如果我尝试单独调试内部查询:
select *,
row_number() over (partition by [Country] order by newid()) as tempRank,
count(*) over (partition by [Country]) as tempTotal
from Users
无需更新,只需选择,我就会得到正确的结果:
Id Name Country tempRank tempTotal
+----+---------------+---------------+-----------------+-------------+
1 John Canada 1 3
2 Kate Canada 2 3
3 Mark Canada 3 3
4 Max Argentina 1 2
5 Sam Argentina 2 2
6 Stacy China 1 4
7 Ken China 2 4
8 jack China 3 4
9 Don China 4 4
所以问题出在更新上,它只取第一行并根据它更新所有表。
如何遍历每一行来更新它?