Solved

Query question

Posted on 2012-04-03
11
333 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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
 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
powershell code to list items in dropdown 6 64
mysql disables rename 4 68
SQL Server 2012 rs - Field1 and Field2 add 1 to Total 4 18
Complex MySQL Query 2 16
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This article describes some very basic things about SQL Server filegroups.
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…

770 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