2013-11-04 16 views
1

我有一個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       

我想如下填​​充ranktotal值:

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 

然而ranktotal都是平等的第一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 

所以,問題與更新,它只是採取的第一行和更新基礎上的所有表。

如何遍歷每一行來更新它?

回答

1

嘗試做update這樣的:

with toupdate (
    select *, 
      row_number() over (partition by [Country] order by newid()) as tempRank, 
      count(*) over (partition by [Country]) as tempTotal 
    from Users 
    ) 
update toupdate 
    set rank = tempRank, total = tempTotal; 

這使得使用SQL Server,你可以更新和「更新CTE」的一個非常不錯的功能。

您的查詢的問題是您有兩個未連接的表。我想你可以把它簡化爲:

update u 
    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 
     ) u; 

否則,你需要加入他們在一起:

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 
     ) u 
    where users.id = u.id; 
0

試試這個:

Update u set 
    rank = (select count(*) from users 
      where country = u.country 
       and id <= u.Id), 
    total = (Select count(*) from users 
      where country = u.country) 
From users u 
相關問題