dbTalk Databases Forums  

Howto embed select statements into other select statements

comp.databases comp.databases


Discuss Howto embed select statements into other select statements in the comp.databases forum.



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

Default Howto embed select statements into other select statements - 05-07-2007 , 06:40 AM






Hi,

I want to calculate the fraction of two counts and I have created the
following select statement:

select
field1,
field2,
(select table1.co/table2.co
from (select count(1) co, id from t_table1 where seal>=1000 group
by id) table1,
(select count(1) co, id from t_table2 group by id) table2
where table1.id=j.id and table2.id=j.id),
field3
from t_table3 j

This works as long as there are corresponding entries in t_table1 and
t_table2, otherwise the fraction becomes null, which might make sense
when there is nothing in t_table2 but not when t_table1 has no
matching entries.

Instead I have tried:

select
field1,
field2,
(select table1.co/table2.co
from (select count(1) co, id from t_table1 where seal>=1000 and
id=j.id) table1,
(select count(1) co, id from t_table2 where id=j.id) table2),
field3
from t_table3 j

But then the database (Oracle 10) complains that it can not find j.id.
How can I write this select correctly?

Best regards
Niels Dybdahl


Reply With Quote
  #2  
Old   
Ed Prochak
 
Posts: n/a

Default Re: Howto embed select statements into other select statements - 05-07-2007 , 08:48 AM






On May 7, 6:40 am, Niels Dybdahl <niels.dybd... (AT) gmail (DOT) com> wrote:
Quote:
Hi,

I want to calculate the fraction of two counts and I have created the
following select statement:

select
field1,
field2,
(select table1.co/table2.co
from (select count(1) co, id from t_table1 where seal>=1000 group
by id) table1,
(select count(1) co, id from t_table2 group by id) table2
where table1.id=j.id and table2.id=j.id),
field3
from t_table3 j

This works as long as there are corresponding entries in t_table1 and
t_table2, otherwise the fraction becomes null, which might make sense
when there is nothing in t_table2 but not when t_table1 has no
matching entries.

Instead I have tried:

select
field1,
field2,
(select table1.co/table2.co
from (select count(1) co, id from t_table1 where seal>=1000 and
id=j.id) table1,
(select count(1) co, id from t_table2 where id=j.id) table2),
field3
from t_table3 j

But then the database (Oracle 10) complains that it can not find j.id.
How can I write this select correctly?

Best regards
Niels Dybdahl

Why make your statement so complicated?
Make an inline view. (basically move the subquery to the FROM clause.)
select
field1,
field2,
ratio,
field3
from t_table3 j,
(select table1.co/table2.co ratio
from (select count(1) co, id from t_table1 where seal>=1000 group
by id) table1,
(select count(1) co, id from t_table2 group by id) table2
where table1.id=table2.id) ratioview
where ratioview.id=j.id ;

For dealing with the NULL conditions, consider the NVL() function and
possibly an outer join between table1 and table2.

HTH,
Ed

PS ( for ORACLE specific questions like this, you should consider
posting to one of the oracle newsgroups. For SQL questions like this,
use comp.databases.oracle.server)



Reply With Quote
  #3  
Old   
Niels Dybdahl
 
Posts: n/a

Default Re: Howto embed select statements into other select statements - 05-08-2007 , 02:37 AM



Hi Ed,

Quote:
Why make your statement so complicated?
Make an inline view. (basically move the subquery to the FROM clause.)
select
field1,
field2,
ratio,
field3
from t_table3 j,
(select table1.co/table2.co ratio
from (select count(1) co, id from t_table1 where seal>=1000 group
by id) table1,
(select count(1) co, id from t_table2 group by id) table2
where table1.id=table2.id) ratioview
where ratioview.id=j.id ;
Thanks for your suggestion, but the problem is that the query:

select count(1) co, id from t_table1 where seal>=1000 group by id

Does not return any record for those ids that do not have records with
seal>=1000. So I get ratio=null in those cases instead of ratio=0.
Instead I have replaced it by the query:

select sum(case when (seal>=1000) then 1 else 0 end) co, id from
t_table1 group by id

That works much better. I still get a ratio=null for those ids that do
not have entries in table2, but I can live with that.

Quote:
For dealing with the NULL conditions, consider the NVL() function
I tried NVL, but it seems not to help, because the problem is that I
do not get any record, while it would have helped if I got a record
containing a null value.

Quote:
and possibly an outer join between table1 and table2.
Would that work when I am already counting on table2?

Best regards
Niels Dybdahl



Reply With Quote
  #4  
Old   
Tonkuma
 
Posts: n/a

Default Re: Howto embed select statements into other select statements - 05-08-2007 , 08:36 AM



How about this?
select
field1,
field2,
(select count(*) from t_table1 where id=j.id and seal>=1000) /
(select NULLIF(count(*),0) from t_table2 where id=j.id) ratio,
field3
from t_table3 j;


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 - 2012, Jelsoft Enterprises Ltd.