sql >> Base de Datos >  >> RDS >> Sqlserver

SQL:reemplace filas repetidas con valores nulos mientras conserva el número de filas

Puede hacer esto enumerando las filas dentro de un año. Luego actualice todos menos el primero:

with toupdate as (
      select t.*, row_number() over (partition by [year] order by [date]) as seqnum
      from t
     )
update toupdate
    set [year] = NULL
    where seqnum > 1;

Si quieres esto como select declaración:

with ts as (
      select t.*, row_number() over (partition by [year] order by [date]) as seqnum
      from t
     )
select [date],
       (case when seqnum = 1 then [year] end) as [year]
from ts;