Google Answers Logo
View Question
 
Q: SQL Query Help Needed - Easy For an Expert ( No Answer,   4 Comments )
Question  
Subject: SQL Query Help Needed - Easy For an Expert
Category: Computers > Programming
Asked by: toddtwods-ga
List Price: $5.00
Posted: 28 Apr 2006 12:31 PDT
Expires: 28 May 2006 12:31 PDT
Question ID: 723767
Query One:

SELECT field_one, field_two,field_three
FROM table_owner.table_one WHERE
	field_four = (SELECT field_four
FROM table_owner.table_two WHERE field_five = 'B11') AND field_six = 1
AND field_seven = 1;



the above query yields me 54 results, one of which is shown below:

field_one	field_two	field_three
1		72		166



When I use the following query (Query Two) with those results:

SELECT	field_eight
FROM table_owner.table_three WHERE
    field_four = (SELECT field_four
FROM table_owner.table_two WHERE field_five = 'B11') AND field_one = 1
AND field_two = 72 AND field_three = 166;

I get the desired information -- the field_eight value.

How can I combine the first and second query into one big query so I
don't have to write 54 versions of the second query?

Thanks.
Answer  
There is no answer at this time.

Comments  
Subject: Re: SQL Query Help Needed - Easy For an Expert
From: frankcorrao-ga on 28 Apr 2006 13:28 PDT
 
I've not thought about this carefully, but it seems like you might
want to just you WHERE field_one IN (subquery) instead of WHERE
field_one = <value>.  e.g.

SELECT	field_eight
FROM table_owner.table_three 
WHERE field_four = 
    (SELECT field_four
     FROM table_owner.table_two WHERE field_five = 'B11') 
     AND field_one IN
         (SELECT field_one
          FROM table_owner.table_one 
          WHERE field_four = 
              (SELECT field_four 
               FROM table_owner.table_two 
               WHERE field_five = 'B11') 
          AND field_six = 1
          AND field_seven = 1)

etc. Can't test this now, but i think this strategy will work, if you
expand it to have subqueries for the other two fields.  I'm sure there
is a more efficient way.
Subject: Re: SQL Query Help Needed - Easy For an Expert
From: sukarvin-ga on 29 Apr 2006 07:00 PDT
 
I guess you need to join the three tables to write a efficient querry.

SELECT field_eight
FROM table_owner.table_one,table_owner.table_two,table_owner.table_three
WHERE
table_owner.table_one.field_four = table_owner.table_two.field_four 
AND table_owner.table_two.field_four = table_owner.table_three.field_four
AND table_owner.table_two.field_five = 'B11'
AND table_owner.table_three.field_one =table_owner.table_one.field_one
AND table_owner.table_three.field_two =table_owner.table_one.field_two
AND table_owner.table_three.field_three =table_owner.table_one.field_three

Please try this query and please let me know the result. Think it is
helpfull to u. :)
Subject: Re: SQL Query Help Needed - Easy For an Expert
From: oraclelagoon-ga on 12 May 2006 02:15 PDT
 
I believe this should help...

SELECT	field_eight
FROM table_owner.table_three WHERE
field_one,field_two,field_three in
(SELECT field_one, field_two,field_three
FROM table_owner.table_one WHERE
	field_four = (SELECT field_four
FROM table_owner.table_two WHERE field_five = 'B11') AND field_six = 1
AND field_seven = 1)

let me know if it works. ;o)
Subject: Re: SQL Query Help Needed - Easy For an Expert
From: dbest-ga on 18 May 2006 14:41 PDT
 
are fields one, two, three unique in the list of 54 if so get rid of
this "AND field_one = 1
AND field_two = 72 AND field_three = 166" out of 2nd query and create
a relational join on fields_two, three and one.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy