dbTalk Databases Forums  

Use Results From a Select as the "From" for Another Select

microsoft.public.sqlserver.mseq microsoft.public.sqlserver.mseq


Discuss Use Results From a Select as the "From" for Another Select in the microsoft.public.sqlserver.mseq forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
RitaG
 
Posts: n/a

Default Use Results From a Select as the "From" for Another Select - 04-01-2005 , 01:09 PM






Hi.

I have to use a long, complicated "Where" clause in a SQL statement and
thought it would be better to break it up into 2 Selects but am not sure of
how to do it.

I'll make my example really simple - I want to select TextData (defined as
an Image in the SQL table) from a table Where TextData like '%ABC%' AND not
like '%DEF%'
AND not like '%GHI%' AND not like '%JKL%' etc.

Basically the question is how to code a Select within another Select where
the 2nd Select uses the results from the 1st Select.

Here's what I tried which doesn't work (I broke each piece down and they
work separately but not together):
Select ProviderName from (Select ProviderName from Provider where
PPONetworkID like '%9')
where ProviderName like '%EDEL%'

Any suggrstions will be greatly appreciated.
Rita

Reply With Quote
  #2  
Old   
Hugo Kornelis
 
Posts: n/a

Default Re: Use Results From a Select as the "From" for Another Select - 04-01-2005 , 03:16 PM






On Fri, 1 Apr 2005 10:09:03 -0800, RitaG wrote:

Quote:
Hi.

I have to use a long, complicated "Where" clause in a SQL statement and
thought it would be better to break it up into 2 Selects but am not sure of
how to do it.

I'll make my example really simple - I want to select TextData (defined as
an Image in the SQL table) from a table Where TextData like '%ABC%' AND not
like '%DEF%'
AND not like '%GHI%' AND not like '%JKL%' etc.

Basically the question is how to code a Select within another Select where
the 2nd Select uses the results from the 1st Select.

Here's what I tried which doesn't work (I broke each piece down and they
work separately but not together):
Select ProviderName from (Select ProviderName from Provider where
PPONetworkID like '%9')
where ProviderName like '%EDEL%'

Any suggrstions will be greatly appreciated.
Rita
Hi Rita,

You're nearly there! You are right that you can use a query instead of a
table or view (this is called a derived query). The only thing you
forgot is to provide an alias for the derived table. Since it has no
name of it's own, it MUST have an alias - even if you never refer to it.

Select ProviderName from (Select ProviderName from Provider where
PPONetworkID like '%9') AS x
where ProviderName like '%EDEL%'

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)


Reply With Quote
  #3  
Old   
RitaG
 
Posts: n/a

Default Re: Use Results From a Select as the "From" for Another Select - 04-01-2005 , 03:57 PM



I knew there was a way! Thanks so much Hugo.

"Hugo Kornelis" wrote:

Quote:
On Fri, 1 Apr 2005 10:09:03 -0800, RitaG wrote:

Hi.

I have to use a long, complicated "Where" clause in a SQL statement and
thought it would be better to break it up into 2 Selects but am not sure of
how to do it.

I'll make my example really simple - I want to select TextData (defined as
an Image in the SQL table) from a table Where TextData like '%ABC%' AND not
like '%DEF%'
AND not like '%GHI%' AND not like '%JKL%' etc.

Basically the question is how to code a Select within another Select where
the 2nd Select uses the results from the 1st Select.

Here's what I tried which doesn't work (I broke each piece down and they
work separately but not together):
Select ProviderName from (Select ProviderName from Provider where
PPONetworkID like '%9')
where ProviderName like '%EDEL%'

Any suggrstions will be greatly appreciated.
Rita

Hi Rita,

You're nearly there! You are right that you can use a query instead of a
table or view (this is called a derived query). The only thing you
forgot is to provide an alias for the derived table. Since it has no
name of it's own, it MUST have an alias - even if you never refer to it.

Select ProviderName from (Select ProviderName from Provider where
PPONetworkID like '%9') AS x
where ProviderName like '%EDEL%'

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.