2011-09-08 10 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.