I have the following query:
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
What I now want to do is create a new column that will group all the ID2’s together..i.e ID2’s having 931 should have value 1 in the new column, 1101 should have 2, 1165 should be 3 and finally all 1288 should have 4… Can I get help for that please?
You can use
DENSE_RANK()to attain the result. It returns the rank of rows within the partition of a result set, without any gaps in the ranking. The rank of a row is one plus the number of distinct ranks that come before the row in question. Refer link DENSE_RANK (Transact-SQL) for more details. Please try: