[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2011-10-05
8
Medium Priority
?
873 Views
Last Modified: 2013-11-16
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
0
Comment
Question by:jj_84
  • 4
  • 3
8 Comments
 
LVL 40

Expert Comment

by:lcohan
ID: 36920207
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
 

Author Comment

by:jj_84
ID: 36920331
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
 
LVL 40

Expert Comment

by:lcohan
ID: 36920387
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

by:jj_84
ID: 36920454
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
 
LVL 40

Expert Comment

by:lcohan
ID: 36920732
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
 

Author Comment

by:jj_84
ID: 36921021
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
 
LVL 7

Accepted Solution

by:
d507201 earned 2000 total points
ID: 36921023
"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
 

Author Closing Comment

by:jj_84
ID: 36952529
Thanks, it was exactly that i.e. failing to meet all criteria of the equijoin.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question