Author |
Topic |
mohan123
Constraint Violating Yak Guru
252 Posts |
Posted - 2012-11-29 : 04:38:20
|
Hello all i have table like users UserId DefinitionId ConditionId UserName 1 1 1 joseph2 1 1 khan3 1 1 gary4 1 1 brown5 2 2 Jim6 2 2 jaymy output should come like this one : UserId DefinitionId ConditionId UserName 1 1 1 joseph2 1 2 khan3 1 3 gary4 1 4 brown5 2 1 Jim6 2 2 jayhere condition id should get numbers like 1,2,3,4 etc for definition ID which as 1,1,1,1,2,2P.V.P.MOhan |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2012-11-29 : 04:49:31
|
[code]DECLARE @tab TABLE(UserId int, DefinitionId int, ConditionId int, UserName varchar(10))INSERT INTO @tabSELECT 1, 1, 1, 'joseph' union allSELECT 2, 1, 1, 'khan' union allSELECT 3, 1, 1, 'gary' union allSELECT 4, 1, 1, 'brown' union allSELECT 5, 2, 2, 'Jim' union allSELECT 6, 2, 2, 'jay';with cte AS (SELECT *, ROW_NUMBER() over(partition by definitionid order by userid) rn FROM @tab)update t SET t.ConditionId = c.rnFROM @tab t JOIN cte c ON t.UserId = c.UserIdSELECT * FROM @tab[/code]--Chandu |
|
|
mohan123
Constraint Violating Yak Guru
252 Posts |
Posted - 2012-11-29 : 05:16:11
|
hey chandu asd in your scenario its correct but i gave my question wrong..conditionid is all 1,1,1,1,1 not 1,1,1,1,2,2 see condition ID suggest meP.V.P.MOhan |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2012-11-29 : 05:31:24
|
This code is independent of ConditionIdsDECLARE @tab TABLE(UserId int, DefinitionId int, ConditionId int, UserName varchar(10))INSERT INTO @tabSELECT 1, 1, 1, 'joseph' union allSELECT 2, 1, 1, 'khan' union allSELECT 3, 1, 1, 'gary' union allSELECT 4, 1, 1, 'brown' union allSELECT 5, 2, 1, 'Jim' union allSELECT 6, 2, 1, 'jay';with cte AS (SELECT *, ROW_NUMBER() over(partition by definitionid order by userid) rn FROM @tab)update t SET t.ConditionId = c.rnFROM @tab t JOIN cte c ON t.UserId = c.UserIdSELECT * FROM @tab--OUTPUTUserId DefinitionId ConditionId UserName1 1 1 joseph2 1 2 khan3 1 3 gary4 1 4 brown5 2 1 Jim6 2 2 jay --Chandu |
|
|
|
|
|