Click here to Skip to main content
15,909,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
user_orgs.userid	user_orgs.orgsequence	user_orgs.orgid	user_orgs.membershiptype
BSS000002556	1	BSS000007403	1
BSS000002556	2	BSS000017078	1
BSS000002556	3	BSS000024650	1
BSS000002556	4	BSS000027067	2

orgstructure.orgid	orgstructure.parentid	orgstructure.orgcode	orgstructure.description
BSS000007403	BSS000000001	ABCCorp	ABC Corp
BSS000017078	BSS000007403	Asia	Asia
BSS000024650	BSS000017078	China	China
BSS000027067	BSS000024650	Beijing	Beijing - Head Office

users.userid	users.givenname	users.familyname	users.level1id	users.level2id	users.level3id	users.level4id	users.level5id	users.leaf_orgid
BSS000002556	John 	Doe	ABCCorp	Asia	China	Beijing	NULL	BSS000027067

How do I get these 3 tables into 1 row like this?
users.givenname	users.familyname	orgstructure.orgid1	orgstructure.description1	orgstructure.orgid2	orgstructure.description2	orgstructure.orgid3	orgstructure.description3	orgstructure.orgid4	orgstructure.description4
John 	Doe	BSS000007403	ABCCorp	BSS000017078	Asia	BSS000024650	China	BSS000027067	Beijing - Head Office
Posted
Updated 24-Sep-13 18:52pm
v2

Usually with a couple of JOINs; but you could do it other ways if you want.
 
Share this answer
 
SELECT	Usersgivenname
,	Usersfamilyname
,	Tem2.orgstructureorgid	  AS	'Orgstructureorgid1'
,	Tem2.orgstructureorgcode  AS	'Orgstructuredescription1'
,	T1.orgstructureorgid	  AS	'Orgstructureorgid2'
,	T1.orgstructureorgcode	  AS	'Orgstructuredescription2'
,	T2.orgstructureorgid	  AS	'Orgstructureorgid3'
,	T2.orgstructureorgcode	  AS	'Orgstructuredescription3'
,	T3.orgstructureorgid	  AS	'Orgstructureorgid4'
,	T3.orgstructureorgcode    AS	'Orgstructuredescription4'
	FROM Tem3  
	INNER JOIN Tem1 
	ON Tem3.usersuserid = Tem1.user_orgsuserid
	INNER JOIN Tem2 
	ON Tem2.orgstructureorgid = Tem1.user_orgsorgid
	AND Tem2.orgstructureorgcode = Tem3.userslevel1id
	INNER JOIN Tem2 AS T1 
	ON T1.orgstructureorgcode = Tem3.userslevel2id
	INNER JOIN Tem2 AS T2
	ON T2.orgstructureorgcode = Tem3.userslevel3id
	INNER JOIN Tem2 AS T3
	ON T3.orgstructureorgcode = Tem3.userslevel4id
 
Share this answer
 
v2

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