Skip to content
Advertisement

Update the Unique number for the co-related records between two columns in the group

I need to identify and update co-related records associated rank under Req_Result column as depicted below. Table name is tblSource.

Item and Key are co-related columns and DenseRankWrtKey is created by using Dense rank with respect to key. I need to assign the same DenseRankWrtKey values to all the co-related values.

Scenario explained:

Item a has the key value 1 and 1 is co-related with c as well, so all related values for a and 1 are a,b,c,f,2,3,7,6,5 hence all these values are assigned as 1 by referring DenseRank column, d and e are not further related to any other values hence its value is kept as is from DenseRank column.

I tried the queries

which is not sufficient.

Just try for this table too : DECLARE @Table AS TABLE ( Id INT IDENTITY(1,1) PRIMARY KEY ,Item varchar(100) ,[key] INT ,DenseRankWrtKey INT ,Req_Res INT ) INSERT INTO @Table ( Item ,[key] ,DenseRankWrtKey ) VALUES ('p', 10 ,1 ), ('q', 10 ,1 ), ('r', 20 ,2 ), ('s', 30 ,3 ), ('t', 30 ,3 ), ('u', 40 ,4 ), ('v', 40 ,4 ), ('w', 40 ,4 ), ('p', 50 ,5 ), ('q', 50 ,5 ), ('r', 50 ,5 ), ('s', 50 ,5 ), ('t', 50 ,5 ), ('u', 50 ,5 ), ('v', 50 ,5 ), ('w', 50 ,5 )

Advertisement

Answer

I find this way easier to read and maintain

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement