2013-01-16 2 views
1

나는 다음과 같은 쿼리를 가지고 :ROW_NUMBER()

declare @temp1 table 
(ID1 int not null, 
ID2 int not null) 

set nocount off 

insert into @temp1 values(1453,931) 
insert into @temp1 values(1454,931) 
insert into @temp1 values(1455,931) 

insert into @temp1 values(2652,1101) 
insert into @temp1 values(2653,1101) 
insert into @temp1 values(2654,1101) 
insert into @temp1 values(2655,1101) 
insert into @temp1 values(2656,1101) 

insert into @temp1 values(3196,1165) 

insert into @temp1 values(3899,1288) 
insert into @temp1 values(3900,1288) 
insert into @temp1 values(3901,1288) 
insert into @temp1 values(3902,1288) 

--select * from @temp1 

select ID1,ID2, ROW_NUMBER() over(partition by ID2 order by ID1) as RowNum1 
from @temp1 

내가 이제 새 열을 생성 할 일은 원하는 것 그룹의 모든 ID2의 together..ie ID2의 값 1을 가져야한다 (931)를 가진 새로운 칼럼에서 1101은 2, 1165는 3이어야하고 마침내 1288은 4를 가져야합니다 ... 제발 저에게 도움을받을 수 있습니까?

+1

다음 번에 시도하겠습니다. :) –

답변

7

DENSE_RANK()을 사용하면 결과를 얻을 수 있습니다. 결과 집합의 파티션 내에서 행 순위를 반환하며 순위에는 아무런 차이가 없습니다. 행의 순위는 해당 행 앞에 오는 별개의 순위의 수에 1을 더한 것입니다. 자세한 내용은 링크 DENSE_RANK (Transact-SQL)을 참조하십시오. 시도해보십시오.

select ID1, ID2, DENSE_RANK() over(order by ID2) as RowNum1 
from @temp1 
+0

그게 전부입니다! 정말 고맙습니다 –