2011-09-08 89 views

回答

3
;with C as 
(
    select DisplayIndex, 
     row_number() over (partition by [Group] order by DateCreated desc) as rn 
    from Product 
) 
update C set 
    DisplayIndex = rn 
+0

+1整齐,无联接需要! – Andomar

+0

这很好。谢谢。 –

2

你可以使用row_numberGroup行数:

update p 
set  DisplayIndex = pn.rn 
from Product p 
join (
     select row_number() over (partition by [Group] 
            order by DateCreated desc) as rn 
     ,  * 
     from Product 
     ) as pn 
on  p.[Group] = pn.[Group] 
     and p.[Name] = pn.[Name] 

Example on SE Data.