Solved

Query question

Posted on 2012-04-03
11
325 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 31

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 31

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
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…

708 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

14 Experts available now in Live!

Get 1:1 Help Now