Joining 3 tables in SAS, using PROC SQL - no results returned

Hi,
I've ran similar queries to this before without any real problems, I may be missing something really obvious here but when I run this code it returns 0 results. When taking the third table out of the equation (justing a join on 2 tables, downsizing my query) it works as expected and returns the records I am after.

Can someone please highlight where I may be going wrong on this?

CODE.txt
jj_84Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lcohanDatabase AnalystCommented:
That means the Join with the third table filters out the record(s) you get in the first join.
To double check that just take the ID of record(s) from the result set by joining the 2 tables and run a select like below against the 3rd table using the JOIN ON column(s)

select * from 3rd_table where ID = xxxx
0
jj_84Author Commented:
That's right, when checking ID's in the 3rd table (as suggessted) it returns the data as expected.

Any suggestions on how I can get around this?

Thanks.
0
lcohanDatabase AnalystCommented:
Hmm....do you have that particular column in the join?
Other option would be to put the first two in a sub-query than join with the third table on the output from that subquery.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

jj_84Author Commented:
I did yes, as part of the 'where' clause i.e. my first 'and' condition i.e. my 1st condition is joining table 1 & 2 on the ID and then my 2nd condition is attempting to join table 2 to table 3 using the same ID value i it is common across all three tables.  

I  can try the 2nd option as my solution in the morning.
Thanks again.
0
lcohanDatabase AnalystCommented:
Why not write a script like below:

select t1.col1,t2.col2, t3.col3, t3.col4,t2.col5
      from table1 as t1
            inner join table2 as t2 on t1.id = t2.id
            inner join table3 as t3 on t2.id=t3.id and t1.id=t3.id

0
jj_84Author Commented:
Tried that now, and the code is no taking longer to execute than which is promising...

I presume I can take your script further and add a 'where' clause to filter on further conditions, if so, do I need to ensure that these too are included in my 'select' clause?
0
d507201Database Marketing ConsultantCommented:
"When taking the third table out of the equation ... it works as expected".

Run another test by removing the WHERE criteria
        AND      C.KEY_COD_ADDR_TYPE = ''
and see what you get.

If you get records then none of the records meet that particular criteria and because you're doing an equi-join you get zero records in the results set.  If that's the case then the solution is to do an outer join.

I don't use SAS SQL very much--I'm a data step kind of guy but I think I'd try two in-line views in the FROM statement that each do a left join.  Kirk Lafler's book "Proc SQL: Beyond the Basics" is a pretty good reference.

The query here is untested but you get the idea.
PROC SQL;
CREATE TABLE PROJECT_1 AS
SELECT 	A.KEY_NUM_ACCT, A.DAT_OPEN, A.NUM_APPL, A.NUM_CUST,
	B.TXT_TITLE_1, B.TXT_FIRST_NM, B.TXT_LAST_NM, B.TEL_HOME_PHONE, B.TEL_BUS_PHONE, B.TEL_PHONE_3, 
         B.KEY_NUM_CUST,
	C.TXT_ADDR_L1, C.TXT_ADDR_L2, C.TXT_ADDR_L3, C.TXT_CITY, C.COD_POSTAL, C.KEY_NUM_CUST
FROM 	(select A.KEY_NUM_ACCT, A.DAT_OPEN, A.NUM_APPL, A.NUM_CUST,
	      B.TXT_TITLE_1, B.TXT_FIRST_NM, B.TXT_LAST_NM, B.TEL_HOME_PHONE, B.TEL_BUS_PHONE, 
               B.TEL_PHONE_3, B.KEY_NUM_CUST
	     from accounts A, cust B
        WHERE	A.NUM_CUST = B.KEY_NUM_CUST
        AND	A.COD_PROD = 'PROD1'
        AND 	A.COD_ACCT_TYPE = 'A') as ab,
        (select B.KEY_NUM_CUST, C.TXT_ADDR_L1, C.TXT_ADDR_L2, C.TXT_ADDR_L3, C.TXT_CITY, C.COD_POSTAL, 
                C.KEY_NUM_CUST
        from cust B left join address C
        on  B.KEY_NUM_CUST  l= C.KEY_NUM_CUST
        where	C.KEY_COD_ADDR_TYPE = '') as bc
WHERE	AB.NUM_CUST = BC.KEY_NUM_CUST;
quit;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jj_84Author Commented:
Thanks, it was exactly that i.e. failing to meet all criteria of the equijoin.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.