SQL: Numbering rows based on logical test -


i have vba script numbers rows incrementally in csv file based on date & time rows (the file sorted date time) , want convert sql script can number rows directly in database instead, have no idea start though.

the vba script starts @ beginning , if date , time columns same assigns of rows same id number, when date or time changes increments number , on:

private sub eventid()  dim event integer event = 0  = 2 112543 if (sheet1.cells.item(i, 2) = sheet1.cells.item(i - 1, 2)) , (sheet1.cells.item(i, 3) =   sheet1.cells.item(i - 1, 3)) sheet1.cells.item(i, 1) = event else event = event + 1 sheet1.cells.item(i, 1) = event end if next     end sub 

how can in sql server 2012? have 1 table , eventid column empty (null) waiting filled in.

the best way load data table has eventid declared identity column. automatically increment values new data put in.

if not possible, can update such following:

with toupdate (       select t.*, row_number() on (order date, time) seqnum       table t      ) update toupdate     set eventid = seqnum; 

edit:

if date/time same , want same number, use dense_rank() instead of row_number():

with toupdate (       select t.*, dense_rank() on (order date, time) seqnum       table t      ) update toupdate     set eventid = seqnum; 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -