Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table 'Dependants' having column FamilyID,MemberID,MemberName,gender and age

FamilyID    MemberID  MemberName     Gender   Age
001                    dharm            M
002                    mukesh           M
002                     sagr            M
002                    sfsfsf           M
003                    dfdff
003                    dfdfgd           F
004                     dffff           F
004                     safaf           F
004                     sdafsa          F


Now, i want, if there are two or more row of a same familyid, then the memberid should be 1 and 2 and3 for the same familyid.
After that table should be look like this:

FamilyID    MemberID  MemberName     Gender   Age
001           1          dharm            M
002           1         mukesh           M
002           2          sagr            M
002           3         sfsfsf           M
003           1         dfdff
003           2         dfdfgd           F
004           1         dffff            F
004           2          safaf           F
004           3          sdafsa          F


I have this table in the sql

Pls, give me the query for this problem
Posted
Updated 6-Jun-13 21:20pm
v2
Comments
Maciej Los 7-Jun-13 3:21am    
Do not SHOUT!

Hi,

Check the following code....
SQL
UPDATE D SET D.MemberID=T.MemberID
FROM Dependants D
INNER JOIN (SELECT ROW_NUMBER() OVER(PARTITION BY FamilyID ORDER BY MemberName) 'MemberID', FamilyID, MemberName
            FROM Dependants) T ON T.FamilyID=D.FamilyID AND T.MemberName=D.MemberName

Regards,
GVPrabu
 
Share this answer
 
v2
Comments
Maciej Los 7-Jun-13 3:54am    
The same answer ;)
+5!
gvprabu 7-Jun-13 3:59am    
yes, SQL developers will think almost same like ours :-)
Maciej Los 7-Jun-13 4:03am    
By the way: you're quick as a F-16[^] ;)
gvprabu 7-Jun-13 4:05am    
hi... this it too tooo much... I am a small Kid. ok
gvprabu 7-Jun-13 4:01am    
hi friend.... share your mail ID. Mine is [removed]mail[/removed]
Try this:

SQL
DECLARE @fam TABLE (FamilyID VARCHAR(3), MemberID INT NULL, MemberName VARCHAR(30), Gender VARCHAR(2) NULL, Age INT NULL)

INSERT INTO @fam (FamilyID, MemberID, MemberName, Gender, Age)
SELECT '001' AS FamilyID, NULL AS MemberId, 'dharm' AS MemberName, 'M' AS Gender, NULL AS Age
UNION ALL SELECT '002', NULL, 'mukesh', 'M', NULL
UNION ALL SELECT '002', NULL, 'sagr', 'M', NULL
UNION ALL SELECT '002', NULL, 'sfsfsf', 'M', NULL
UNION ALL SELECT '003', NULL, 'dfdff', NULL, NULL
UNION ALL SELECT '003', NULL, 'dfdfgd', 'F', NULL
UNION ALL SELECT '004', NULL, 'dffff', 'F', NULL
UNION ALL SELECT '004', NULL, 'safaf', 'F', NULL
UNION ALL SELECT '004', NULL, 'sdafsa', 'F', NULL

UPDATE t1 SET t1.MemberID = t2.MemberId
FROM @fam AS t1 INNER JOIN (
		SELECT FamilyID, ROW_NUMBER() OVER(PARTITION BY FamilyID ORDER BY MemberName) AS MemberID, MemberName
		FROM @fam
	) AS t2 ON t1.FamilyID = t2.FamilyId AND t1.MemberName = t2.MemberName  

SELECT *
FROM @fam
 
Share this answer
 
Comments
gvprabu 7-Jun-13 3:51am    
hi friend... In my machine no SQL server :-(,
So I can't able to test any SQL Query...
Maciej Los 7-Jun-13 3:54am    
Thank you, Gopal ;)
gvprabu 7-Jun-13 3:57am    
welcome...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900