1 year ago

#357980

test-img

JarradH

SQL - Insert an ID column in a certain order & return matched values

I am hoping to alter a table from

enter image description here

Sorting the table by two columns, and adding a sequential ID column, one starting from 0, the other from 1

enter image description here

With the aim of being able to match the ID0 and ID1 columns, providing another column comparing the previous rows value, basically shifting a column off by one. In this example matching ID1 to ID0 and putting the corresponding ID0 value in the new column.

enter image description here

SELECT *, ROW_NUMBER() OVER (ORDER BY PersonID, CurrPosStartDate ASC)
FROM WF_Data_Temp

This almost does what I want, but its not an actual new column.

ALTER TABLE WF_Data_Temp ADD ID_0 INT IDENTITY(1,1)

This creates the new column as I need, but I am not sure how to specify an ORDER BY for an ALTER

Thanks

sql

indexing

sql-order-by

alter-table

0 Answers

Your Answer

Accepted video resources