简体   繁体   中英

How to row number in SQL Server with counter number?

I have data

NoUrut| Nama
1       PSX
1       GE
1       SX
2       SZ
2       GEX
2       HT

how to get output like this with row number

Rownum | NoUrut| Nama
 1        1       PSX
 2        1       GE
 3        1       SX
 1        2       SZ
 2        2       GEX
 3        2       HT

thank's before

Use row_number() function

select *,
       row_number() over (partition by NoUrut order by NoUrut) as Rownum 
from table t;

Try this solution:

SELECT row_number() over (partition by NoUrut order by (SELECT 0)) as Rownum,
       NoUrut,Nama
FROM Table1;

OUTPUT:

Rownum  NoUrut  Nama
1        1      PSX
2        1      GE
3        1      SX
1        2      SZ
2        2      GEX
3        2      HT

Link to the demo:

http://sqlfiddle.com/#!18/2227b/3

Since You don't want to give order by on any column so use SELECT 0 in order by clause to accomplish the above result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM