Mega Code Archive

 
Categories / MSSQL Tutorial / Sequence Indentity
 

Query that Returns Result Row Numbers, Starting with 1 and Incrementing by 3

4> 5> CREATE TABLE MyTable ( 6>  key_col int NOT NULL IDENTITY (1,1), 7>  abc     char(1) NOT NULL 8> ) 9> INSERT INTO MyTable VALUES ('a') 10> INSERT INTO MyTable VALUES ('b') 11> INSERT INTO MyTable VALUES ('c') 12> SELECT * FROM MyTable ORDER BY key_col 13> GO (1 rows affected) (1 rows affected) (1 rows affected) key_col     abc ----------- ---           1 a           2 b           3 c (3 rows affected) 1> 2> SELECT 3>  1 + 4>  3 * 5>  (SELECT 6>    count(*) 7>   FROM 8>    MyTable AS T2 9>   WHERE 10>    T2.abc < T1.abc) AS rownum, 11>  abc 12> FROM 13>  MyTable AS T1 14> ORDER BY 15>  abc 16> 17> drop table MyTable 18> GO rownum      abc ----------- ---           1 a           4 b           7 c (3 rows affected) 1>