Subquery referencing column in outer query

I have a simplified problem such as...
select     a.year
,     (     select     b.column
          from     table b
          ,     (     select     c.year
                    from     table c
                    where     c.year = b.year  <= ORA-00904 "B"."YEAR": invalid identifier
          where     b.year = a.year
from     table a
group by a.yearMy question is why am I getting the error? Is there a way around it?

You cannot use a correlated sub-query in the from clause, only in the SELECT list or the WHERE clause.
Oracle does not see table b when resolving the in-line view in the from clause you need something more like:
SELECT a.year,     (SELECT column
                FROM (SELECT b.column
                      FROM table b,     
                           (SELECT c.year
                            FROM table c)
                      WHERE b.year = c.year)
                WHERE b.year = a.year)
FROM table a
GROUP BY a.year

Similar Messages

  • Referencing a char column in another query

    HI
    I have 2 queries. My child query returns char data which needs to be referenced in the main query.
    Is it possible to access this data from my main query.
    I know its possible with numbers using summary cols.
    TIA
    TIM

    You can reference a single value of the child query using the "first/Last" summary functions in a summary column in the master query. The datatype of the column doesn't matter.

  • Referencing columns of a subselect query?

    Hello,
    I have the next query
    select columnA1, columnA2
    from tableA
    where columnA1in (select columnB1 from tableB);
    TableB has a column (i.e. columnB2) that it has appear in the results of the select. How can I reference to this column in the query.
    I would want to do something like that:
    select columnA1, columnA2, columnB2
    from tableA
    where columnA1in (select columnB1 from tableB);
    but, it doesn't work.
    Thanks.

    This is the query:
    SELECT t.agg_id,
    b.sec_id, op.portfolio_id, b.leg_id, bv.session_id,
    t.result_id,
    -1, -1, -1, b.sec_cod, b.counterparty_id, b.product_id,
    op.operation_id, bv.flag_guarantee
    FROM t0par_rkv_security b,
    t0par_rkv_positions c,
    t0par_rkv_operations op,
    t0par_rkv_base_valuation bv,
    t0par_tmp_counterparty_struct t
    WHERE bv.session_id = 625
    AND bv.sec_id = b.sec_id
    AND bv.operation_id = op.operation_id
    AND op.operation_id = c.operation_id
    AND b.flag_validate = 1
    AND b.sec_id = c.sec_id
    AND c.POSITION <> 0
    AND op.position_lst_id = 224
    and b.counterparty_id=t.counterparty_id_hijo
    and t.result_id=976
    And this is the explain plan:
    select statement hint=choose
    nested loops
    nested loops
    nested loops
    nested loops
    table access by index rowid t0par_tmp_counterparty_struct
    index range scan idx_counterp_struc_result
    table access by index rowid t0par_rkv_base_valuation
    index range scan i1par_rkv_base_valuation1
    table access by index rowid t0par_rkv_operations
    index uique scan pkpar_rkv_operations
    table access by index rowid t0par_rkv_security
    index uique scan pkpar_rkv_security
    table access by index rowid t0par_rkv_positions
    and-equal
    index range scan xak1t0par_rkv_positions
    index range scan     iepar_rkv_positions_sec
    I think the tables are being consulted with the indexes. I don't know how optimize the query

  • Using Subqueries...ERROR being displayed by Outer Query

    USing Sunqueries question
    I am using the flwg SubQuery
    and get the error:
    SQL Error (107) : The column prefix 'dt3' does not match with a
    table name or alias name used in the query.
    Either the table is not specified in the FROM clause or it has a correlation name which must be used instead.
    Query
    Select    tie.id_entity                     
                 tie.id_inst_code_type       
                  count(*)                             'Total',
                  dt2. PrimaryIndicator         'Primary Indicator',
                  dt3. ActiveIndicator           'Active  Indicator'
       from      trade_input_event tie,
       (Select ins.flg_primary_listing,count(*)  PrimaryIndicator
        from instrument ins
        where ins.id_inst in (   select id_inst  from instrument_xref insx
                                            where insx.id_inst_xref_type = 'SE'
                                            and  insx.id_inst_xref = dt3.id_inst_xref) ***ERR0R
        group by ins.flg_primary_listing ) dt2,
       ( Select ins.flg_active,count(*)  ActiveIndicator              
         from instrument ins
         where ins.id_inst in (  select id_inst  from instrument_xref
                                            where id_inst_xref_type = 'SE'
                                            and id_inst_xref = '5252624' ) 
         group by ins.flg_active ) dt3
         where tie.id_inst_code =  dt.id_inst_xref
         and tie.id_entity = 'AGL'
         group by tie.id_entity, tie.id_inst_code_type
    Wont the OUTER QUERY dt3 get executed 1st and then the value of this be used in dt2?

    I did that,but still the error
    dt3 has a query and an inner query and i want to return back the value of the inner
    query (which is id_inst_xref )
    ( Select ins.flg_active,count(*)  ActiveIndicator              
         from instrument ins
         where ins.id_inst in (  select id_inst  from instrument_xref
                                            where id_inst_xref_type = 'SE'
                                            and id_inst_xref = '5252624' )  ** WANT TO RETURN THIS
         group by ins.flg_active ) dt3
     

  • Retrieve varchar column from Oracle query in a resulset

    Hi, I'am a begginer in JSP Technologies.
    I've do a small jsp that opens a db connection to an oracle, execute a query a show results.
    When the columns of the query are int I've no problem, the jsp show the columns of the resulset, but when the columns selected are varchars the jsp doesn't show anything....
    Can anyone help me?
    Example:
    Table: CUSTOMERS
    Col1 : ID_COSTUMER int
    Col2: CUSTOMER_NAME varchar
    <%
    Connection canal = null;
    ResultSet tabla = null;
    Statement instruccion=null;
    try { Class.forName("oracle.jdbc.driver.OracleDriver");
    canal=DriverManager.getConnection("jdbc:oracle:thin:@XXXXXXXXXXXXXXXXXXXp");
    instruccion = canal.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
    } catch(java.lang.ClassNotFoundException e){};
    String Query = "SELECT ID_CUSTOMER FROM CUSTOMERS";
    try { tabla = instruccion.executeQuery(Query);
    out.println("<TABLE Border=10 CellPadding=5><TR>");
    out.println("<TD>CUSTOMER</TD></TR>");
    while(tabla.next()) {
    out.println("<TR>");
    out.println("<TD>"+tabla.getString(1)+"</TD>");
    out.println("</TR>"); };
    out.println("</TABLE></CENTER></DIV></HTML>");
    tabla.close(); instruccion.close(); canal.close();}
    catch(SQLException e) {};
    %>Results:
    CUSTOMER
    1
    2
    3
    4
    Doing the change in query:
    SELECT CUSTOMER_NAME FROM CUSTOMERS
    I have not results....
    Thank you.

    sorry, I misplaced an ending code bracket on last one
    I'm not really familiar with doing this inside a jsp.. but there are a few things that you should try to make sure that it isn't a database problem before assuming it is a problem on your jsp.... It is possible you have already tried the following but just to make sure.
    When you do the second query does the first two out.printlns that are before the while still output? If not then your query is incorrect.
    Second I would try
    String Query = "SELECT ID_CUSTOMER,CUSTOMER_NAME FROM CUSTOMERS";
    try { tabla = instruccion.executeQuery(Query);
    out.println("<TABLE Border=10 CellPadding=5><TR>");
    out.println("<TD>CUSTOMER_ID</TD><TD>CUSTOMER_NAME</TD></TR>");
    while(tabla.next()) {
    out.println("<TR>");
    out.println("<TD>"+tabla.getString(1)+"</TD>");
    out.println("<TD>"+tabla.getString(2)+"</TD>");
    out.println("</TR>"); };
    out.println("</TABLE></CENTER></DIV></HTML>");I suspect this will also return nothing.. if that is the case then you need to check your database. If it does infact return values for the ID and nothing for the NAME then I'm not sure at this point what the problem is.

  • Displaying dynamic columns in the query output.

    Hello,
    Depending on current quarter output of the query would vary.
    If current quarter is 'Q1'
    O/p : InvQ1  Del  BackQ1  BackQ2  BackQ3  BackQ4
    If current quarter is 'Q2'
    O/p : InvQ1  InvQ2  Del  BackQ2  BackQ3  BackQ4
    If current quarter is 'Q3'
    O/p : InvQ1  InvQ2  InvQ3  Del  BackQ3  BackQ4
    If current quarter is 'Q4'
    O/p : InvQ1  InvQ2  InvQ3  InvQ4  Del  BackQ4
    So, out of 9 columns, 6 columns would display at any given time depending on the current quarter.
    Invoice of 4 quarters, Deliquency and Backlog of 4 quarters.
    I am not getting any way how to display dynamic columns or hide unwanted/blank columns.
    For the time being I have added all 9 columns in the query, and in the output unwanted columns shows blank.
    Can anybody please suggest me how to display only 6 columns in the output depending on current quarter.
    Thanks and Regards
    Shilpa

    Hello,
    This shouldn't be too difficult. Assuming you have a variable with the selected quarter, you may choose to display Inv (whatever that is) for quarters 1 through variable, Del, and Back for quarters variable through 4. You also need a drilldown on quarter in the columns (after the key figure structure). You would probably need to restrict 'Del' on the variable as well if you haven't already done it.
    Best regards,
    Christoffer

  • Query hangs with outer query

    Hi,
    OS: SPARC 64 bit
    Oracle Version: 10.2.0.3.0 64 bit
    My query hangs when I include the outer query. When I execute without outer query It comes out in a fraction of second but when I includes outer query it starts hang
    Below is my original query which hangs
    select * from (select raw_sql_.**, rownum raw_rnum_ from (SELECT /*+ FIRST_ROWS */ NETWORK_ID||'","'||CALLER_NUMBER||'","'||CALLED_NUMBER FROM CDR FROM CDR WHERE 1 = 1
    AND day_of_year = 99 AND INTERFACE = 'TRAFIC' AND TIME_STAMP BETWEEN to_date('2009/04/08 00:00:00','yyyy/mm/dd hh24:mi:ss') AND to_date
    ('2009/04/09 00:00:00','yyyy/mm/dd hh24:mi:ss') AND NETWORK_ID IN (1025,1026) order by id desc) raw_sql_ WHERE rownum <= 10000) WHERE raw_rnum_ > 0;
    when I execute this it will come out immediately
    select raw_sql_.**, rownum raw_rnum_ from (SELECT /*+ FIRST_ROWS */ NETWORK_ID||'","'||CALLER_NUMBER||'","'||CALLED_NUMBER FROM CDR FROM CDR WHERE 1 = 1
    AND day_of_year = 99 AND INTERFACE = 'TRAFIC' AND TIME_STAMP BETWEEN to_date('2009/04/08 00:00:00','yyyy/mm/dd hh24:mi:ss') AND to_date
    ('2009/04/09 00:00:00','yyyy/mm/dd hh24:mi:ss') AND NETWORK_ID IN (1025,1026) order by id desc) raw_sql_ WHERE rownum <= 10000
    select * from (select raw_sql_.**, rownum raw_rnum_ from (SELECT /*+ FIRST_ROWS */ NETWORK_ID||'","'||CALLER_NUMBER||'","'||CALLED_NUMBER FROM CDR FROM CDR WHERE 1 = 1
    AND day_of_year = 99 AND INTERFACE = 'TRAFIC' AND TIME_STAMP BETWEEN to_date('2009/04/08 00:00:00','yyyy/mm/dd hh24:mi:ss') AND to_date
    ('2009/04/09 00:00:00','yyyy/mm/dd hh24:mi:ss') AND NETWORK_ID IN (1025,1026) order by id desc) raw_sql_ WHERE rownum <= 10000) WHERE raw_rnum_ > 0;
    but when I includes outer query I hangs it is really very weird for me?
    Here is the explain plan for that query
    Execution Plan
    Plan hash value: 3493649369
    | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Pstart| Pstop |
    | 0 | SELECT STATEMENT | | 1 | 2015 | 1 (0)| 00:00:01 | | |
    |* 1 | VIEW | | 1 | 2015 | 1 (0)| 00:00:01 | | |
    |* 2 | COUNT STOPKEY | | | | | | | |
    | 3 | PARTITION RANGE SINGLE | | 1 | 5894 | 1 (0)| 00:00:01 | 99 | 99 |
    | 4 | PARTITION LIST ALL | | 1 | 5894 | 1 (0)| 00:00:01 | 1 | 48 |
    |* 5 | TABLE ACCESS BY LOCAL INDEX ROWID| CDR | 1 | 5894 | 1 (0)| 00:00:01 | 4705 | 4752 |
    | 6 | BITMAP CONVERSION TO ROWIDS | | | | | | | |
    |* 7 | BITMAP INDEX SINGLE VALUE | IX_CDR_INTERFACE | | | | | 4705 | 4752 |
    Predicate Information (identified by operation id):
    1 - filter("RAW_RNUM_">0)
    2 - filter(ROWNUM<=10000)
    5 - filter("DAY_OF_YEAR"=99 AND "TIME_STAMP">=TO_DATE('2009-04-08 00:00:00', 'yyyy-mm-dd hh24:mi:ss') AND
    "TIME_STAMP"<=TO_DATE('2009-04-09 00:00:00', 'yyyy-mm-dd hh24:mi:ss') AND ("NETWORK_ID"=1025 OR
    "NETWORK_ID"=1026))
    7 - access("INTERFACE"='TRAFIC')
    can one explain why it happen? what could be the reason? Is it a bug or something? what should I do now? Issue is critical in the production enviromentent
    Waiting for your Valuable Reply
    Thanks In Advance
    With Regards
    Boo

    user454189 wrote:
    I suspect the issue with the where clause (raw_rnum_ > 0)
    Actually I using bitmap index on ' INTERFACE' column. Does it causing a prob or raw_rnum_ causing a problem?Boo,
    a couple of comments:
    1. Why do you use the deprecated FIRST_ROWS hint? From 9i on the FIRST_ROWS_n mode should be used instead, and since you're using a top-N query format the hint should be superfluous anyway since the optimizer should switch to FIRST_ROWS_n mode automatically.
    2. Why do you use a BITMAP index and not a conventional b*tree index? Let me hazard a guess: Because the INTERFACE column has a small number of distinct values? That would be in most cases a bad idea.
    3. What seems to be odd that you're accessing the data via the BITMAP index on INTERFACE, but request an ORDER BY ID DESC. But there is no order by in the execution plan posted. I wonder where the order should come from? Can you show us the DDL of the bitmap index?
    4. What does the plan look like if you run only the inner query without the outer query?
    You can always gain more information by tracing the statement execution or using the GATHER_PLAN_STATISTICS hint together with the DBMS_XPLAN.DISPLAY_CURSOR function.
    Please read this HOW TO: Post a SQL statement tuning request - template posting that explains what you should provide if you have SQL statement tuning question and how to format it here so that the posted information is readable by others.
    This accompanying blog post shows step-by-step instructions how to obtain that information.
    Regards,
    Randolf
    Oracle related stuff blog:
    http://oracle-randolf.blogspot.com/
    SQLTools++ for Oracle (Open source Oracle GUI for Windows):
    http://www.sqltools-plusplus.org:7676/
    http://sourceforge.net/projects/sqlt-pp/

  • Referenced column import

    Hi there
    Suppose I export table
    and then drop table.
    and then decided to import back.
    I figured out that if there is a foreign key in some other table that has referenced column in the table I dropped, this foreign key will not recreated after import !!!!
    I think I should consider this, before dropping the table.
    Is there anything like this I should consider regarding the export/import issue....
    Cheers

    Is there anything like this I should consider regarding the export/import issue.is all SQL, including DDL, maintained by some Source Code Control System?

  • Multiple Key figure in a single column of a query

    Hi,
    Is it possible to use muliple key figure like Amount and Qty field together in a column of a query?
    Please advise,
    Best Regards,
    UR

    Hi,
    You will be able to do it with the help of cell definitions.
    Say,you have a structure in Rows :
    A
    B
    For A ..in corresponding column cell,use cell definition with KF1
    For B.....in corresponding column cell,use cell definition with KF2
    this way,you will be able to see both in one column
    I m not sure how much it is relavant to your requirement.But this is one way of gettin it

  • Error in using aggregate function in Outer Query in Siebel Analytics

    Hi,
    When I am using aggregate function in outer query in Siebel Analytics I am facing error.
    Error Codes: OPR4ONWY:U9IM8TAC:OI2DL65P:OI2DL65P
    State: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 59111] The SQL statement must include a GROUP BY clause. (HY000)
    Bellow is the code.
    SELECT test1.username saw_0, test1.desg saw_1,COUNT (test2.querydate) saw_2
    FROM (SELECT POSITION.CBL username,
    POSITION.CBP desg
    FROM "CM"
    WHERE (POSITION.BPTCD = 'Marketing')
    AND (POSITION.EDate =TIMESTAMP '1899-01-01 00:00:00'
    ) test1,
    (SELECT users.UN username,
    measures."Query Count" querycount,
    measures."Max Total Time" secs,
    topic.db dashboardname,
    "Query Time".DATE querydate
    FROM "Plan"
    WHERE (topic."Dashboard Name" IN ('DS'))) test2
    WHERE test2.username = LOWER (test1.username)
    AND test2.dashboardname = 'DS'
    GROUP BY test1.username, test1.desg

    Should your query be a valid SQL query?
    I can't think that the query you have would be valid in a SQL plus window.
    Chris

  • Comment column in a query

    Hello,
    In a BW query, I need to add a column of text comments (for example, the description of a calculation, the unit used, ...). These texts are static, the queries will only be used as static reports (download scheduler), in excel and web, so we can not use any VB script.
    Is it a way to add some text in a cell of a column, as in the following example (columns 2 and 3) ?
    KF1    |    Comment for KF1    |    Euros    |    12
    KF2    |    Comment for KF2    |    %          |    47
    Thanks in advance
    Michaë

    Hi guys,
    finally how you fix this problem?, I need to show a comment in a query column and i don't know if this is posible ¿?, I have a query inside of a wad and I want to show a comment in the columns  of the query with the formula details.
    Please let me know if you this is possible.
    Thanks!, Kinds Regards

  • How to find out query name using Elements of the query builder.

    Hi SDNers,
    how to find out query name using Elements of the query .
    thanks,
    satyaa

    Hi,
    For having a look at the relation between BEx tables,check the link below:
    http://wiki.sdn.sap.com/wiki/display/BI/ExploretherelationbetweenBEx+Tables
    -Vikram

  • Creating an empty column in a query

    Is there a way to place an empty column in  a query? I would like to put acolumn with no information into the middle of a query. The reason is that the information that goes in that column is not available on any tables in my SQL databases.
    SELECT
    hier.dir_name 
    ,hier.am_name 
    ,hier.fm_name     INSERT BLANK COLUMN HERE
    ,hier.tech_name
    hier.tech_id

    Hi
    "no information" in database is usually NULL value. Is this what you are looking for?
    SELECT
    hier.dir_name
    ,hier.am_name
    ,NULL as fm_name
    --,hier.fm_name INSERT BLANK COLUMN HERE
    ,hier.tech_name
    hier.tech_id
    [Personal Site] [Blog] [Facebook]

  • Access Summary column in a query

    Hi All,
    I am new to oracle reports. I am using report builder 10.1.2.0.2.
    I have a summary column in my report. I would like to use this summary column in another query.
    if is use this column directly in the query, i am getting the below error:
    "Field 'F1' references column '<summary column name>' at a frequency below its group. "
    Both these fields are in the same frame. In the object navigator, both are displayed under the same group name.
    please let me know if there any way to access summary columns in another query.
    Thanks.

    You can use a field from one query in another query as parameter, i.e. preceded by colon. To do this first create link of "Group to Query" type between the first query's group where the summary column is, and the second query.

  • Set Column width in query (not using SQL*Plus)

    How can I Set Column width in query
    I understand you can set column width using
    column col1 FORMAT A5
    select col1 from table1;But this only works in SQL*Plus
    I want to be able to do this in a regular SQL query window (not in SQL*Plus), how can I do it.....
    I am using a 'SQL window' in PL/SQL Developer IDE
    and when I use this syntax it says:
    ORA-00900: Invalid SQL statement
    Any suggestions are appreciated...
    thanks,
    M.

    Did you try using RPAD or LPAD functions? They fill the unfilled part of a string with character you provide... either on right or left side depending on what function you use.
    e.g.
    SELECT RPAD('Smith', 10, ' ') Name FROM dual;http://www.adp-gmbh.ch/ora/sql/rpad.html
    Edited by: Zaafran Ahmed on Nov 10, 2010 11:50 AM

Maybe you are looking for

  • Internal DVD disappears including from ATA and Burning in Mac info

    I have an open ticket on this issue, my internal DVD appears and disappears. I'm not sure if this is a loose internal connection or a firmware issue. If it is a loose connection, I guess that I will have to send it back to Apple for repair. I underst

  • Picture quality when burning disc

    When i burn a slideshow and view it on a tv the picture becomes very bright just before it move to the next slide. Any suggestions?

  • Error: Trying to use Photoshop automation

    I am running Windows 7, 32-bit. I tried to add a reference from Visual Studio but Photoshop was not visible. So I decided to try to register the type library manually. I am trying to automate photoshop but I cannot get the type library registered. Th

  • Blue screen on Z10 during start up

    Hi all, I'm experiencing some issues with my sister's Z10. What I think has happened is that I was updating OS when it ran out of battery (yeah I know, but some friend took the charger to charge her phone and I didn't notice it...), and now the phone

  • Best Practices - WOrking with lot of fields

    I am working on new modules based on a migrated project. And I have many fields to work with, which is making the code large and repetitive (kind of). If I follow the existing structure (from migration) it works fine but I am looking for better ways