School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 309,235 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,770 people online right now. Registration is fast and FREE... Join Now!




Joins Help?

 

Joins Help?

stumitchvt

14 Aug, 2009 - 11:06 AM
Post #1

New D.I.C Head
*

Joined: 7 Aug, 2009
Posts: 3


My Contributions
CODE

SELECT DISTINCT name.PER_ID as PERID, name.ENTITY_NAME as PER, id.ID_TYPE_CD, idssn.PER_ID_NBR, rel_l.DESCR12, name2.ENTITY_NAME as RLPER, name2.PER_ID as PERID2
FROM CM_RELSHIP rel
INNER JOIN CI_PER_NAME name ON rel.per_id1 = name.per_id
INNER JOIN CI_PER_ID id ON rel.per_id1 = id.per_id
INNER JOIN CI_PER_NAME name2 ON rel.per_id2 = name2.per_id
INNER JOIN CI_PER_REL_TYPE_L rel_l ON rel.per_rel_type_cd = rel_l.per_rel_type_cd
WHERE rel.PER_REL_TYPE_CD <> 'SPPR'


So here is my challenge right now...
This works but returns multiple lines per user because there are a possibility of a user having up to three different types of entrys in the in the CI_PER_ID table. I need it to now return all of the users ID's at once instead of listing them on separate lines. I messed with arrays but could not fine a way to pass all the values from a select distinct into an array so I thought I'd try different joins and I basically ended up with only the 20 or so users that actually have all 3 id's out of the 13000+ users. Any thoughts on how I may be able to do this? Is it possible to do entirely within SQL? Below is another attempt that gave me the result mentioned above.

[code]
SELECT DISTINCT name.PER_ID as PERID, name.ENTITY_NAME as PER, id1.ID_TYPE_CD, id1.PER_ID_NBR, id2.ID_TYPE_CD, id2.PER_ID_NBR, id3.ID_TYPE_CD, id3.PER_ID_NBR, rel_l.DESCR12, name2.ENTITY_NAME as RLPER, name2.PER_ID as PERID2
FROM CM_RELSHIP rel
INNER JOIN CI_PER_NAME name ON rel.per_id1 = name.per_id
INNER JOIN CI_PER_ID id1 ON rel.per_id1 = id1.per_id
INNER JOIN CI_PER_ID id2 ON rel.per_id1 = id2.per_id
INNER JOIN CI_PER_ID id3 ON rel.per_id1 = id3.per_id
INNER JOIN CI_PER_NAME name2 ON rel.per_id2 = name2.per_id
INNER JOIN CI_PER_REL_TYPE_L rel_l ON rel.per_rel_type_cd = rel_l.per_rel_type_cd
WHERE rel.PER_REL_TYPE_CD <> 'SPPR' and id1.id_type_cd = 'Val1' and id2.id_type_cd = 'Val2' and id3.id_type_cd = 'Val3'
[\code]

And to try to describe the tables breifly to clairfy...

CI_PER_NAME contains a users name and user_number.
CM_RELSHIP contains 2 user_numbers and relationship id.
CI_PER_ID contains a user_number, user_id_type and user_id (user_numbers may be in here 3 times with different user_id_type)
CI_PER_REL_TYPE contains a relationship type and relationship id.

and what I need out is User1Name, User1ID1, User1ID2, User1ID3, relationship, user2Name


User is offlineProfile CardPM
+Quote Post

 
Reply to this topicStart new topic
Replies(1 - 3)

P4L

RE: Joins Help?

16 Aug, 2009 - 08:16 PM
Post #2

Geek 4 Life
Group Icon

Joined: 7 Feb, 2008
Posts: 2,182



Thanked: 16 times
Dream Kudos: 125
My Contributions
Try something like this. It will make things easier using the last line, and will ONLY pull the ID's with less more then three times.

CODE

SELECT DISTINCT name.PER_ID as PERID,
name.ENTITY_NAME as PER,
id.ID_TYPE_CD,
idssn.PER_ID_NBR,
rel_l.DESCR12,
name2.ENTITY_NAME as RLPER,
name2.PER_ID as PERID2

FROM CM_RELSHIP rel,
CI_PER_NAME name,  
CI_PER_ID id


WHERE rel.PER_REL_TYPE_CD <> 'SPPR'
AND rel.per_id = name.per_id
AND rel.per_id = id.per_id

Having COUNT(name.ENTITY_NAME) >3

User is offlineProfile CardPM
+Quote Post

stumitchvt

RE: Joins Help?

18 Aug, 2009 - 06:19 AM
Post #3

New D.I.C Head
*

Joined: 7 Aug, 2009
Posts: 3


My Contributions
hmmm.... Not really what I was looking for. Let me try to explain it again in a simpler setting.

Say Table 1 is 2 cols Username and Usernumber and Table 2 is 3 cols Usernumber(FK), UserIDType, UserID.
There are 3 possible UserIDTypes say StudentNo, SSN and EmployeeNo.

So I am looking for a way to get this for one person:

-------------------------------------------------------------------------------
- UserName - UserNumber - StudentNo - SSN - EmployeeNo -
-------------------------------------------------------------------------------
(all info on one line)

Rather then
------------------------------------------------------
- UserName - UserNumber - StudentNo -
- UserName - UserNumber - SSN -
- UserName - UserNumber - EmployeeNo -
------------------------------------------------------

Everyone will have at LEAST 1 UserID in the system but may have all three.

User is offlineProfile CardPM
+Quote Post

P4L

RE: Joins Help?

18 Aug, 2009 - 10:22 AM
Post #4

Geek 4 Life
Group Icon

Joined: 7 Feb, 2008
Posts: 2,182



Thanked: 16 times
Dream Kudos: 125
My Contributions
That's all in how you do the SELECT statement, NOT the joins. It would be something like this.

CODE

SELECT tb1.USERNAME,
tb1.USERNUMBER,
tb2.STUDENTNO,
tb3.EMPLOYEENO

From tbl1, tbl2, tbl3

Where tbl1.Field = tb2.Field
and tbl2.Field = tbl3.Field


You need to make sure that there is a field that you can link to that is unique.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/26/09 08:58AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month