Question: I have an Update statement where it recognizes duplicate values in one field and for each one found I want to increment the time up by one minute in a date/time field. I can't change the duplicate value because it breaks other parts of the project.

My code is currently:

```
UPDATE table
SET Date = GETDATE()
Where Seq in (Select Seq from table 
Group By Seq
Having COUNT(Seq) > 1)

```

Currently it sets all the dates to the same time. How would I set the Date so that each gets a unique time?

I tried something like:

```
SET Date = DATEADD(Mi,1, Date)

```

This increments up by 1 minute on all records. How do I touch them all separately?

Answer: 
You can use `ROW_NUMBER` in a `CTE`:

```
WITH CTE AS(
    SELECT *,
        rn = ROW_NUMBER() OVER(ORDER BY(SELECT NULL))
    FROM Seq
    WHERE Seq IN(SELECT Seq FROM Seq GROUP BY Seq HAVING COUNT(Seq) > 1)
)
UPDATE CTE
    SET Date = DATEADD(MINUTE, rn, GETDATE())

```
Another approach using `RANK` and `ROW_NUMBER` to derive the numberof minutes to add to each Date in the group (if I've interpreted your question correctly). 

Should work on 2012 up (you don't mention which version of SQL Server you're using)

```
CREATE TABLE #Seq(seq INT,date DATETIME2);

DECLARE @Now DATETIME2 = SYSDATETIME();

INSERT INTO #Seq
    ( seq, date )
VALUES 
     (0, DATEADD(MINUTE, 1, @Now))
    ,(0, DATEADD(MINUTE, 2, @Now))
    ,(0, DATEADD(MINUTE, 3, @Now))
    ,(1, DATEADD(MINUTE, 10, @Now))
    ,(1, DATEADD(MINUTE, 10, @Now))
    ,(1, DATEADD(MINUTE, 10, @Now))
    ,(2, DATEADD(MINUTE, 20, @Now))
    ,(2, DATEADD(MINUTE, 21, @Now))
    ,(2, DATEADD(MINUTE, 22, @Now));

SELECT * FROM #Seq;

WITH CTE AS(
SELECT 
    SEQ
    ,Date
    ,ROW_NUMBER() OVER (PARTITION BY seq ORDER BY Date) rn
    ,RANK() OVER (PARTITION BY seq ORDER BY Date) rnk
FROM #seq
)
UPDATE CTE  SET Date = DATEADD(MINUTE, (rn-rnk), Date);

SELECT * FROM #Seq;

```