How to select NEWID () for multiple rows

SELECT looks like this:

SELECT  DISTINCT  
     Header.EntryGUID,
     Header.ImportDate
FROM table_Name Header
WHERE Header.ID 
ORDER BY EntryGUID

      

OUTPUT:

EntryGUID   -------------------------------------------------  ImportDate

B7FFC239-2370-4E65-A184-7F2DD18A196E-----2015-02-24 00:00:00.000

B7FFC239-2370-4E65-A184-7F2DD18A196E-----2015-03-15 00:00:00.000

      

My question is how to select the second row NEWID ()

+3


source to share


1 answer


You can do this with window functions:

select ImportDate,
       case when row_number() over(partition by EntryGUID order by ImportDate) = 1
            then EntryGUID else newid() end as EntryGUID
from(
SELECT DISTINCT  
     Header.EntryGUID,
     Header.ImportDate
FROM table_Name Header
WHERE Header.ID = 'variable')t

      



This will order the lines by date, leave the first line untouched, and replace the other lines with new ones.

+2


source







All Articles