본문 바로가기

Dev/MS-SQL

[MS-SQL] row_number() over 사용하기

반응형
-- 특정 그룹별로 일련번호
select groupCode, row_number() over(PARTITION BY hCode order by groupCode desc) as rowNo
from ollagaza

* RESULT
A  1
A  2
A  3
A  4
B  1
B  2
C  1
C  2
C  3
-- 전체 일련번호
select groupCode, row_number() over(order by groupCode desc) as rowNo
from ollagaza
where hCode LIKE 'JPTYO%'

* RESULT
A  1
A  2
A  3
A  4
B  5
B  6
C  7
C  8
C  9
반응형