Home » SQL & PL/SQL » SQL & PL/SQL » Correlated Sql Query (Oracle 10g,Win7 23 bit)
Correlated Sql Query [message #615778] Mon, 09 June 2014 03:48 Go to next message
Xandot
Messages: 235
Registered: January 2014
Location: India
Senior Member
select  name,data,pid,Did,Ref_Type,value,
        case Ref_Type
            when 1 then
                        (   -- Start sub-query a
                            select data
                            from   (   -- Start sub-query b
                                       select  data,row_number() over(order by data)-1 val
                                       from    tab4 d
                                       [b]where   d.Eid = b.Did[/b] )   -- end sub-query b
                            where  val=a.value
                       )    -- End sub-query a
            when 2 then
                        to_char(value)
            when 3 then
                        to_char(value)
       end as value
from
    (
        select  b.Name,b.Did,
                b.pid,b.Ref_Type,a.value
        from    tab1 a, tab2 b, tab3 c
        where a.IDX = 31101
        and a.ID = b.ID
        and a.IDX = c.IDX
    ) 


When i am execute this query So oracle raise
"ORA-00904: "b.Did": invalid identifier"...(Column name in bold later)

What's wrong in this query please suggest me ...


Thanks ,
Xandot
Re: Correlated Sql Query [message #615780 is a reply to message #615778] Mon, 09 June 2014 03:55 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Two related problems:
1) You can only correlate with things declared in the level above the current sub-query. So sub-query b can only reference things declared in sub-query a.
2) Table references used within an inline view can not be referenced outside it, so this wouldn't work either:
select  b.did
from
    (
        select  b.Name,b.Did,
                b.pid,b.Ref_Type,a.value
        from    tab1 a, tab2 b, tab3 c
        where a.IDX = 31101
        and a.ID = b.ID
        and a.IDX = c.IDX
    ) 


EDIT: added extra sentence to point 1.

[Updated on: Mon, 09 June 2014 03:56]

Report message to a moderator

Re: Correlated Sql Query [message #615781 is a reply to message #615780] Mon, 09 June 2014 03:58 Go to previous messageGo to next message
Xandot
Messages: 235
Registered: January 2014
Location: India
Senior Member
Thanks for reply Cookiemonster ..

Is there any solution for this query ....
Re: Correlated Sql Query [message #615783 is a reply to message #615781] Mon, 09 June 2014 04:08 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
It's pretty hard to write a working query when the only specification you have is a query that doesn't compile.
If you write out the requirement and post a Test case - create table statements and insert statements for a small, representative sample of data, then we'll be able to work with your tables and data and come up with something that works.
Re: Correlated Sql Query [message #615814 is a reply to message #615781] Mon, 09 June 2014 08:16 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
Xandot wrote on Mon, 09 June 2014 04:58
Is there any solution for this query ....


Something like:

with t1 as (
            select  b.Name,
                    b.Did,
                    b.pid,
                    b.Ref_Type,
                    a.value
              from  tab1 a,
                    tab2 b,
                    tab3 c
              where a.IDX = 31101
                and a.ID = b.ID
                and a.IDX = c.IDX
           ),
     t2 as (
            select  Eid Did,
                    data,
                    row_number() over(partition by Eid order by data) - 1 val
              from  tab4 d
              where d.Eid in (
                              select Did from t1
                             )
           )
select  name,
        data,
        pid,
        t1.Did,
        Ref_Type,
        value,
        case Ref_Type
            when 1 then data
            when 2 then to_char(value)
            when 3 then to_char(value)
        end as value
  from  t1,
        t2
  where t2.did(+) = t1.did
    and t2.val(+) = t1.value
/


SY.

[Updated on: Mon, 09 June 2014 08:24]

Report message to a moderator

Re: Correlated Sql Query [message #615875 is a reply to message #615814] Tue, 10 June 2014 07:58 Go to previous message
Xandot
Messages: 235
Registered: January 2014
Location: India
Senior Member
Thanks solomon...
Previous Topic: what is a nested loop join?
Next Topic: ORA-01840: input value not long enough for date format
Goto Forum:
  


Current Time: Tue Apr 23 03:57:08 CDT 2024