SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 replace value used in query with IN SELECT (...)
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Sprout Coder
Starting Member

2 Posts

Posted - 06/13/2013 :  14:19:52  Show Profile  Reply with Quote
The following QUERY #1 returns the correct results when value "1010" is used:

SELECT *
FROM iterations $iterations2, experiments $experiments1, clients $clients1
WHERE $experiments1.id = $iterations2.experiment_id
AND
$clients1.property_id = $experiments1.property_id
AND
$experiments1.id = "1010"

now the following QUERY #2 returns the value "1010" that the previous Query was using:

SELECT $iterations1.experiment_id
FROM iterations $iterations1
WHERE $iterations1.id IN (SELECT $variations1.iteration_id
FROM variations $variations1
WHERE $variations1.id = "6039")

more specifically, it returns :

experiment_id
-------------
1010

However, when I execute QUERY #1 having replaced value "1010" with QUERY #2

as in the following query (QUERY #3) :

SELECT *
FROM iterations $iterations2, experiments $experiments1, clients $clients1
WHERE $experiments1.id = $iterations2.experiment_id
AND
$clients1.property_id = $experiments1.property_id
AND
$experiments1.id IN (SELECT $iterations1.experiment_id
FROM iterations $iterations1
WHERE $iterations1.id IN (SELECT $variations1.iteration_id
FROM variations $variations1
WHERE $variations1.id = "6039"))

I get no results. Why is that?

I know I'm not providing you with any information as to how the tables are related.

However, given that QUERY #1 & QUERY #2 work fine on their own, can you see why they do not work when combined into QUERY #3?

Thank you for any help

MuMu88
Aged Yak Warrior

547 Posts

Posted - 06/13/2013 :  15:59:02  Show Profile  Reply with Quote
On the surface everthing looks fine.
Would you be able to post your ddls and some example data.

Edited by - MuMu88 on 06/13/2013 15:59:34
Go to Top of Page

TG
Flowing Fount of Yak Knowledge

USA
6062 Posts

Posted - 06/13/2013 :  20:56:05  Show Profile  Reply with Quote
Is this MS Sql Server? I don't think you can use "$" as the start of your table aliases. Also you should enclose constant values in single quotes not double quotes.

Be One with the Optimizer
TG
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 06/14/2013 :  00:56:07  Show Profile  Reply with Quote
whats the datatype of $iterations1.experiment_id? may its char? try this too


SELECT * 
FROM iterations $iterations2, experiments $experiments1, clients $clients1
WHERE $experiments1.id = $iterations2.experiment_id
AND 
$clients1.property_id = $experiments1.property_id
AND 
$experiments1.id IN (SELECT RTRIM($iterations1.experiment_id)
FROM iterations $iterations1
WHERE $iterations1.id IN (SELECT $variations1.iteration_id
FROM variations $variations1
WHERE $variations1.id = "6039"))


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Sprout Coder
Starting Member

2 Posts

Posted - 06/15/2013 :  12:52:05  Show Profile  Reply with Quote
I tried that but I still get nothing. TO be clear, I don't get an error, just no results.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 06/15/2013 :  12:55:17  Show Profile  Reply with Quote
one more attempt


SELECT * 
FROM iterations $iterations2, experiments $experiments1, clients $clients1
WHERE $experiments1.id = $iterations2.experiment_id
AND 
$clients1.property_id = $experiments1.property_id
AND 
REPLACE(LTRIM(RTRIM($experiments1.id)),CHAR(160),'') IN (SELECT REPLACE(LTRIM(RTRIM($iterations1.experiment_id)),CHAR(160),'')
FROM iterations $iterations1
WHERE $iterations1.id IN (SELECT $variations1.iteration_id
FROM variations $variations1
WHERE $variations1.id = "6039"))


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000