Solved

Query question

Posted on 2012-04-03
11
330 Views
Last Modified: 2012-06-04
I have this query:

SELECT
    pd.person_id, pd.first,pd.email
FROM
    pd
INNER JOIN
    srf ON pd.person_key = srf.person_key
WHERE
    srf.survey_option_key IN (297,298,299)
AND
    srf.survey_question_key = '43'
AND
    pd.is_subscriber ='1'
ORDER BY
    pd.person_id ASC;

Open in new window


I would like to also grab data from a column called "option_text" in the sod table. I am having trouble figuring out how to join

sod.option_text ON sod.survey_option_key =  srf.survey_option_key and display the results . Any ideas?
0
Comment
Question by:lvollmer
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 24

Expert Comment

by:johanntagle
ID: 37803763
Should just be:

SELECT
    pd.person_id, pd.first,pd.email,sod.option_text
FROM
    pd
INNER JOIN
    srf ON pd.person_key = srf.person_key
INNER JOIN
    ON srf.survey_option_key =  sod.survey_option_key
WHERE
    srf.survey_option_key IN (297,298,299)
AND
    srf.survey_question_key = '43'
AND
    pd.is_subscriber ='1'
ORDER BY
    pd.person_id ASC

Open in new window


Or maybe you need LEFT OUTER JOIN instead of INNER JOIN, in case the entry on sod is optional.
0
 

Author Comment

by:lvollmer
ID: 37806924
I had previously tried your solution and when I do, i get a message :

Table 'sod.option_text' doesn't exist

and it does , in fact, exist.
0
 
LVL 24

Expert Comment

by:johanntagle
ID: 37808349
sorry typo.  line 8 above should be "sod ON srf.survey_option_key = sod.survey_option_key"
0
 

Author Comment

by:lvollmer
ID: 37812713
yeah I had caught that and fixed it but still got that error.
0
 
LVL 24

Expert Comment

by:johanntagle
ID: 37814281
Odd.  Can you share a dump of the tables so I can test it from my end?
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 9

Accepted Solution

by:
keyu earned 500 total points
ID: 37822525
SELECT
    pd.person_id, pd.first,pd.email,sod.option_text
FROM
    pd,sod
INNER JOIN
    srf ON pd.person_key = srf.person_key
WHERE
    srf.survey_option_key IN (297,298,299)
AND
    srf.survey_question_key = '43'
AND
    srf.survey_option_key =  sod.survey_option_key
AND
    pd.is_subscriber ='1'
ORDER BY
    pd.person_id ASC
0
 
LVL 32

Expert Comment

by:awking00
ID: 37824799
Is sod table in same schema as pd and srf?
0
 

Author Comment

by:lvollmer
ID: 37828907
I believe so,

PD and SRF share  a unique key that I am able to join on,  sod does not have that same key, but it does have a key that matches up with SRF

IE:

PD & SRF share "person_id"

SRF & SOD share "survey_option_key"
0
 
LVL 32

Expert Comment

by:awking00
ID: 37828986
Can you do a describe of the sod table (i.e. column names and datatypes)?
0
 

Author Comment

by:lvollmer
ID: 37828989
it will take me a bit but I will get this info and upload it
0
 
LVL 9

Expert Comment

by:keyu
ID: 37831910
I have tried my posted query and uts working exactly as you want can you please comment something on this
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

This article describes some very basic things about SQL Server filegroups.
Read about achieving the basic levels of HRIS security in the workplace.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now