Restructre an inline query

Is there any better way to restructure this query or in anyother way it can be written
SELECT FH.CURRENT_FT
FROM FACTOR_HISTORY FH, REPORTING_ISSUE_ID APD
WHERE FH.INTERNAL_ISSUE_ID = APD.INTERNAL_ISSUE_ID
AND FH.FACTOR_TYPE_CD = 'CurrentFaceFactor'
AND FH.AS_OF_DT = (SELECT MAX(FH1.AS_OF_DT)
                   FROM FACTOR_HISTORY FH1
                   WHERE FH1.INTERNAL_ISSUE_ID =FH.INTERNAL_ISSUE_ID
                   AND FH1.FACTOR_TYPE_CD ='CurrentFaceFactor'
                   AND FH1.AS_OF_DT <= APD.EFFECTIVE_DT)
[pre]
Maximus                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

This one does it, not sure though.
SELECT CURRENT_FT
from (select fh.current_ft, row_number() over (order by fh.as_of_dt) rn
      from factor_history fh
          ,reporting_issue_id apd
      where fh.internal_issue_id = apd.internal_issue_id
      and fh.factor_type_cd = 'CurrentFaceFactor'
      and fh.as_of_dt <= apd.effective_dt
where rn = 1Cheers
Sarma.

Similar Messages

  • LOV inline query

    Hello,
    I'm using version 2.0 and I've to create a LOV query like the following:
    select kev_name d, kev_nr r from TAB1 where kev_nr in
    (select event from CRMUP where crm_user = v('USER'))
    This query fails with the error message "LOV query is invalid, a display and a return value are needed, the column names need to be different. If your query contains an in-line query, the first FROM clause in the SQL statement must not belong to the in-line query."
    Could please anybody help me to transform the query above to a LOV-able inline query ?
    Thanks in advance - Peter

    Hi Yog,
    thank's, but this doesn't work too.
    I've the feeling, that there is a problem in regard of the last sentence of the error message "If your query contains an in-line query, the first FROM clause in the SQL statement must not belong to the in-line query."
    Any idea ?
    Peter

  • Passing values dynamically Froman 'INLINE Query' to a 'SUB QUERY'

    If I am using an line query which uses a SELF JOIN subquery
    how can I pass the values from the Inline Query to Subquery ?
    Is this possible?
    SQL...Code
    and Inline Queries..
    (select count(*) 'PrimaryISIN'
    from instrument ins,
              instrument_xref insx
    where ins.id_inst = insx.id_inst
    and     insx.flg_active = 'Y'
    and     ins.id_source   ='MF'
    and     ins.flg_primary_listing = 'Y'
    and     ins.flg_active = 'Y'
    and     insx.id_inst_xref = (  select  DISTINCT  a.id_inst_xref
                                    from    instrument_xref a,
                                            instrument_xref  b
                                      where b.id_inst_xref ='B0DV8Y9'
                                            and     b.id_inst = a.id_inst
                                         and     b.id_inst_xref_type in ('SE','IS')
                                      and    a.id_inst_xref =  'GB00B03MLX29'
    )PISIN2
    I am hardcoding the id_inst_xref is the SELF JOIN? Can these be passed dynamically from
    the INLINE Query which is invoking the SUB QUERY?

    Hi, The hardcoded values entered in the SELF JOIN run fine.
    Now,I need to pass values dynamically from the INLINE query.
    How do I do that?
    Thanks

  • Query regarding Inline Query and a Join.Please see

    I have a query regarding Inline Queries
    I have 2 tables
    TRADE and POSTING
    TRADE has Primary Key:id_trade_ref and POSTING has Foriegn.Key: id_trade_ref
    id_trade_ref 5V973781B has 5 records in POSTINGS
    If i need to join these 2 tables,I use
    1) Select t.id_entity,t.id_trade_ref,p.am_stock
       from trade t,posting p
       where t.id_trade_ref = '5V973781B'
       and   p.id_trade_ref = '5V973781B'
    2) Now I can use the same as an Inline Query:
       select
       t.id_entity,
       t.id_trade_ref,
       (  select p1.am_stock 
          from posting p1
          where p1.id_entity =  t.id_entity
          and   p1.id_trade_ref= t.id_trade_ref
          and   p1.id_posting_type in ( 'NEW', 'CAN')
    from
    trade t
    where t.id_entity = 'DBL'
    and   t.id_trade_ref = '5V973781B'
    My Query:
    Of the two,which is a better option to use.?
    Is Inline a better option or a normal join?

    I would rewrite the first one to:
    Select t.id_entity,t.id_trade_ref,p.am_stock
    from trade t,posting p
    where t.id_trade_ref = '5V973781B'
    and p.id_trade_ref = t.id_trade_ref
    This way, you really make the join between the table.
    I think the join is better because it uses less code. I do not think you will notice much performance win between one or the other, especially because it concerns only a few rows.

  • Inline query vs cross join

    Hi all,
    Two tables who have no relation with each other. For example an employees table and a systemparameter table with a startworktime.
    We need a query with data from both tables:
    Get all employees and the startworktime (which is the same for everybody)
    Which is cheaper: an inline query or a cartesian product or crossjoin?
    Inine :
    select name, function
           , (select startworktime from systemparameter)
    from employees;
    Cartesian product:
    select name, function, startwoime
    rktfrom employees
    cross join systemparameter;
    Your thoughts on this.

    Hi,
    To see which runs faster on your system, with your sub-query, there's no sibstitute for testing on your system, with your sub-query.
    I predict you won't notice any difference.  Scalar sub-queries (as in your first example) are cached (at least in recent versions of Oracle), so you can include a scalar sub-query in a result set of a million rows, and the sub-query can be executed once, not a million times.

  • Self Joining and Inline Query. A tricky report.

    I am stuck with a very tricky situation.Please help.This is PROD issue.
    I have written a SQL code which has 1 inline queries,and displays the right results
    with the right report output
    Dont get confused.Just go thru this.
    select   pie.id_inst_code,
             ISNULL(PN.Active, 0)                 'Active',
    from position_master_input_event pie,
    (select  insx.id_inst_xref_type,insx.id_inst_xref,count(*) 'PrimaryListing'
    from instrument ins, instrument_xref insx
    where ins.id_inst = insx.id_inst
    and   insx.flg_active = 'Y'
    and   ins.flg_active  = 'Y'
    group by insx.id_inst_xref_type,insx.id_inst_xref
    )PN
    where     id_entity = 'AGL'
    and       pie.id_inst_code *= PN.id_inst_xref
    and       pie.id_src_inst_code_type*= PN.id_inst_xref_type
    group by  pie.id_inst_code,PN.Active,
    Table :Instrument_xref
    id_inst      id_inst_xref_type    id_inst_xref  flg_active
    0372285      SE                   B0DV8Y9       Y
    0372285      IS                   GB00B03MLX29  Y
    Table :Instrument
    id_inst      id_inst_xref_type    id_inst_xref  flg_active  flg_primary_listing
    0372285      SE                   B0DV8Y9       Y           N 
    OUTPUT:
    id_inst_xref                      Active
    B0DV8Y9                           1
    PERFECT.Works fine
    2) Now comes the tricky part.:
        0372285 also has GB00B03MLX29 which has flg_active to Y and which also maps to 0372285.
        Am I right?
        New reportOutput
        id_inst_xref                      Active   PRIMARY ISIN
        B0DV8Y9                           1        1
        So,now I want a SELF JOIN this way built into the code:
        (hardcoded values work)
        (i)
        select  a.id_inst_xref
        from instrument_xref a,
             instrument_xref b
        where b.id_inst_xref ='B0DV8Y9'
        and   b.id_inst = a.id_inst
        and   b.id_inst_xref_type in ('SE','IS')
        and   a.id_inst_xref =  'GB00B03MLX29'
        (ii)
         select count(*) 'PrimaryISIN'
         from instrument ins,
             instrument_xref insx
         where ins.id_inst = insx.id_inst
         and   insx.flg_active = 'Y'
         and   ins.flg_primary_listing = 'Y'
         and   ins.flg_active = 'Y'
         And now LINKING ALL :
        select   pie.id_inst_code,
             ISNULL(PN.Active, 0)                 'Active',
        from position_master_input_event pie,
        (select  insx.id_inst_xref_type,insx.id_inst_xref,count(*) 'PrimaryListing'
         from instrument ins, instrument_xref insx
         where ins.id_inst = insx.id_inst
         and   insx.flg_active = 'Y'
         and   ins.flg_active  = 'Y'
         group by insx.id_inst_xref_type,insx.id_inst_xref
        )PN,
        (select count(*) 'PrimaryISIN'
         from instrument ins,
             instrument_xref insx
         where ins.id_inst = insx.id_inst
         and   insx.flg_active = 'Y'
         and   ins.flg_primary_listing = 'Y'
         and   ins.flg_active = 'Y'
          and     insx.id_inst_xref = ( 
                                           select  DISTINCT  a.id_inst_xref
                                            from    instrument_xref a,
                                            instrument_xref  b
                                      where b.id_inst_xref = 'B0DV8Y9'
                                          and     b.id_inst = a.id_inst
                                          and     b.id_inst_xref_type in ('SE','IS')
                                       and    a.id_inst_xref =  'GB00B03MLX29'
        where     id_entity = 'AGL'
        and       pie.id_inst_code *= PN.id_inst_xref
        and       pie.id_src_inst_code_type*= PN.id_inst_xref_type
        group by  pie.id_inst_code,PN.Active,
        THE Self join works fine as long as it is hardcoded.
        But assume there can br multiple such situations as the above,and I dont want to
        hardcode the values,how can I build the NEW REPORT by SELF JOINING.
        Please can someome help.This is a tricky one.
        Is there a better way to this

    Isn't this the same question as:
    Passing values dynamically Froman 'INLINE Query' to a 'SUB QUERY'
    and
    Another query regarding Inline Query and Self Join and pass Column Values

  • INLINE QUERY

    hi,
    i have to add the value in such a way from different tables, if you have any solution pls send me
    SELECT ENAME, (SAL+A.SAL) FROM EMP111 C, (SELECT (SAL + B.SAL) FROM EMP1, (SELECT SAL FROM EMP11) B) A WHERE A.EMPNO = C.EMPNO AND B.EMPNO=C.EMPNO
    regards
    vk

    Without checking or googling I think that inline query is an embedded cte, e.g.
    select columns ... from (another select goes here) T1 
    inner join (select statement goes here) T2 on ...
    You can express the above with 2 cte instead. Using CTE improves readability of the query.
    For every expert, there is an equal and opposite expert. - Becker's Law
    My blog
    My TechNet articles

  • Inline Query Sum

    hi
    i am using 2 inline queries, from both queries am getting values in outer query
    i need to find out the sum of amount total.
    here is the query
    select  customer_name,branch, round((revenue)/100000,2) revenue,round(((sum(revenue)- sum(ap_amount))/100000),2) margin, (sum(revenue)- sum(ap_amount))/sum(revenue) as "Margin %" from
    (select                 'AP' Mod,
                            fvv.description branch,
                            glc.segment4 ap_won,
                            sum(aida.amount) ap_amount                          
                           FROM
                            apps.ap_invoice_distributions_all aida,
                            apps.ap_invoices_all aia,
                            apps.fnd_flex_values_vl fvv,
                            apps.gl_code_combinations glc,
                            apps.po_vendors pv
                         WHERE aia.invoice_id = aida.invoice_id
                         AND aida.dist_code_combination_id = glc.code_combination_id
                         AND pv.vendor_id = aia.vendor_id
                         AND aida.set_of_books_id = pv.set_of_books_id
                         AND aia.set_of_books_id = aida.set_of_books_id
                         AND aia.set_of_books_id = 2
                         AND glc.segment4 != '0000000'
                         AND aia.org_id = aida.org_id
                         AND aia.org_id = 92
                         AND aida.posted_flag = 'Y'
                         AND glc.segment2 = fvv.flex_value
                         AND fvv.flex_value_set_id = 1008015
                         AND aida.accounting_date between '01-jun-2010' and '10-jun-2010'
                         AND aia.invoice_amount != 0
                        and glc.SEGMENT1=16
                        group by
                           fvv.description,
                           glc.segment4
                            order by 2)a,
    (SELECT                 
                            'AR' Mod,
                            rc.CUSTOMER_NAME customer_name,
                            rct.interface_header_attribute1 ar_won,
                            sum(rctl.extended_AMOUNT) revenue,
                            fvv.description branch1   
                        FROM apps.ra_customer_trx_lines_all rctl,
                            apps.RA_CUST_TRX_LINE_GL_DIST_ALL rctg,
                            apps.fnd_flex_values_vl fvv,
                            apps.ra_customer_trx_all rct,
                            apps.ra_customers rc,
                            apps.RA_CUST_TRX_TYPES_ALL rctt,
                            apps.gl_code_combinations gcc
                      WHERE rctl.customer_trx_id = rct.customer_trx_id
                        and rct.BILL_TO_CUSTOMER_ID=rc.CUSTOMER_ID
                        and rct.CUSTOMER_TRX_ID=rctg.CUSTOMER_TRX_ID
                        and rctl.CUSTOMER_TRX_LINE_ID=rctg.CUSTOMER_TRX_LINE_ID
                        and rctt.CUST_TRX_TYPE_ID = rct.CUST_TRX_TYPE_ID
                        and gcc.CODE_COMBINATION_ID=rctg.CODE_COMBINATION_ID
                        AND gcc.segment2 = fvv.flex_value
                        AND fvv.flex_value_set_id = 1008015
                        and gcc.SEGMENT3 like '5%'
                        and rctg.ACCOUNT_CLASS='REV' 
                        and rctl.line_type =  'LINE'
                        and rct.complete_flag='Y'
                        and rctg.gl_date between '01-jun-2010' and '10-jun-2010'
                        and rct.ORG_ID=92
                        group by fvv.description,rct.interface_header_attribute1,rc.CUSTOMER_NAME
                        order by 2)b
    where a.ap_won=b.ar_won
    and a.branch = b.branch1
    group by branch,revenue,customer_name
    order by 2  I need to revenue total,
    can you let me know, how to calculate that.
    Thanks,

    Like this:
    select  customer_name,branch, round((revenue)/100000,2) revenue,
            round(((sum(revenue)- sum(ap_amount))/100000),2) margin,
            (sum(revenue)- sum(ap_amount))/sum(revenue) as "Margin %",
            SUM(revenue) OVER() tot_Rev from
    (select                 'AP' Mod,
                            fvv.description branch,
                            glc.segment4 ap_won,
                            sum(aida.amount) ap_amount                          
                           FROM
                            apps.ap_invoice_distributions_all aida,
                            apps.ap_invoices_all aia,
                            apps.fnd_flex_values_vl fvv,
                            apps.gl_code_combinations glc,
                            apps.po_vendors pv
                         WHERE aia.invoice_id = aida.invoice_id
                         AND aida.dist_code_combination_id = glc.code_combination_id
                         AND pv.vendor_id = aia.vendor_id
                         AND aida.set_of_books_id = pv.set_of_books_id
                         AND aia.set_of_books_id = aida.set_of_books_id
                         AND aia.set_of_books_id = 2
                         AND glc.segment4 != '0000000'
                         AND aia.org_id = aida.org_id
                         AND aia.org_id = 92
                         AND aida.posted_flag = 'Y'
                         AND glc.segment2 = fvv.flex_value
                         AND fvv.flex_value_set_id = 1008015
                         AND aida.accounting_date between '01-jun-2010' and '10-jun-2010'
                         AND aia.invoice_amount != 0
                        and glc.SEGMENT1=16
                        group by
                           fvv.description,
                           glc.segment4
                            order by 2)a,
    (SELECT                 
                            'AR' Mod,
                            rc.CUSTOMER_NAME customer_name,
                            rct.interface_header_attribute1 ar_won,
                            sum(rctl.extended_AMOUNT) revenue,
                            fvv.description branch1   
                        FROM apps.ra_customer_trx_lines_all rctl,
                            apps.RA_CUST_TRX_LINE_GL_DIST_ALL rctg,
                            apps.fnd_flex_values_vl fvv,
                            apps.ra_customer_trx_all rct,
                            apps.ra_customers rc,
                            apps.RA_CUST_TRX_TYPES_ALL rctt,
                            apps.gl_code_combinations gcc
                      WHERE rctl.customer_trx_id = rct.customer_trx_id
                        and rct.BILL_TO_CUSTOMER_ID=rc.CUSTOMER_ID
                        and rct.CUSTOMER_TRX_ID=rctg.CUSTOMER_TRX_ID
                        and rctl.CUSTOMER_TRX_LINE_ID=rctg.CUSTOMER_TRX_LINE_ID
                        and rctt.CUST_TRX_TYPE_ID = rct.CUST_TRX_TYPE_ID
                        and gcc.CODE_COMBINATION_ID=rctg.CODE_COMBINATION_ID
                        AND gcc.segment2 = fvv.flex_value
                        AND fvv.flex_value_set_id = 1008015
                        and gcc.SEGMENT3 like '5%'
                        and rctg.ACCOUNT_CLASS='REV' 
                        and rctl.line_type =  'LINE'
                        and rct.complete_flag='Y'
                        and rctg.gl_date between '01-jun-2010' and '10-jun-2010'
                        and rct.ORG_ID=92
                        group by fvv.description,rct.interface_header_attribute1,rc.CUSTOMER_NAME
                        order by 2)b
    where a.ap_won=b.ar_won
    and a.branch = b.branch1
    group by branch,revenue,customer_name
    order by 2

  • Getting "ORA-00979: not a GROUP BY expression" error in Inline query

    Hello all,
    The following query when run in SCOTT user gives "ORA-00979: not a GROUP BY expression" error.
    If I remove the TRUNC function from the outer query's group by clause, then it fetches
    (My actual query is something similar to the following query. I have given emp, dept tables for convenience's sake)
    select e.empno,e.ename, AVG(e.SAL), trunc(e.hiredate),
    (select sum(sal) from emp
    where hiredate = e.hiredate) salary
    from emp e
    group by e.DEPTNO,e.EMPNO,e.ENAME, trunc(e.hiredate)
    Pls suggest how this error can be avoided.
    Regards,
    Sam

    Why not this?
    satyaki>
    satyaki>select * from v$version;
    BANNER
    Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
    PL/SQL Release 10.2.0.1.0 - Production
    CORE    10.2.0.1.0      Production
    TNS for Linux: Version 10.2.0.1.0 - Production
    NLSRTL Version 10.2.0.1.0 - Production
    Elapsed: 00:00:00.02
    satyaki>
    satyaki>
    satyaki>select e.empno,
      2         e.ename,
      3         AVG(e.SAL),
      4         trunc(e.hiredate),
      5        (
      6           select sum(sal)
      7           from emp
      8           where hiredate = e.hiredate
      9        ) salary
    10  from emp e
    11  group by e.DEPTNO,
    12           e.EMPNO,
    13           e.ENAME,
    14           e.hiredate;
         EMPNO ENAME      AVG(E.SAL) TRUNC(E.H     SALARY
          7934 MILLER         1887.6 23-JAN-82     5583.6
          7698 Glen             1848 23-JAN-82     5583.6
          7788 SCOTT          598.95 19-APR-87     598.95
          7900 JAMES          1379.4 03-DEC-81    6650.16
          7521 WARD           226.88 22-FEB-81     226.88
          7599 BILLY            4500 10-JUN-09       4500
          2222 sp               1200 14-SEP-09       1200
          7902 FORD          5270.76 03-DEC-81    6650.16
          7566 Smith            1848 23-JAN-82     5583.6
          7654 MARTIN           1815 28-SEP-81       1815
          7839 KING             7260 17-NOV-81       7260
         EMPNO ENAME      AVG(E.SAL) TRUNC(E.H     SALARY
          7844 TURNER           2178 08-SEP-81       2178
          7876 ADAMS          159.72 23-MAY-87     159.72
    13 rows selected.
    Elapsed: 00:00:00.03
    satyaki>Regards.
    Satyaki De.

  • Hint in Update or Select Statement in an inline query ??

    Hi ,
    I had an update statement that will get the data from the inline select statement,now where can i can keep the hint ,either in update statement or in Select statement...
    Please let me know if my sample script is wrong or any better way to approach...Please assume that Salary table had millions of employee's salary records.
    update emp
    set salary = salary + (select /*+ full(a) parallel(a,4)  */ salary from Salary
                             where   experience > 5  and empno = 55 )
    where empno = 85Thanks
    Rede

    You would put the hint in the select statement as you have it, but it won't work until you alias your table name to "a" since that is how you are referencing it in the hint:
    update emp
    set salary = salary + (select /*+ full(a) parallel(a,4)  */ salary from Salary a
                             where   experience > 5  and empno = 55 )
    where empno = 85
    NOTE I added the "a" right after the Salary table name AND you can only do parallel updates on partitioned table, but the select will run in parallel to at least get the data for you quickly.
    To answer your other question about syntax, it is incorrect and there are a lot of ways to write it but a straight forward way would be to use the merge statement
    merge into emp e
    using (select /*+ full(s) parallel(s,4)  */ s.salary from salary s where s.experience > 5) s
    on (e.empno = s.empno)
    when matched then update
    set salary = s.salary;

  • Using Inline Query in BPEL

    Hi All,
    We have requirement to exect the bleow query by not using Custom SQL
    select * from A a where a.x1 not in (select b.x1 from B b).
    Is this possible using simple DBadapter by selecting "*perform an Operation on table*" by modifingt the Toplink in the jdeveloper.
    any help in this highly appreciated.
    Thanks and Regards,
    Nagaraju .D

    Hi,
    You can use "perform an operation on table" and in the 8th Step replace the SQL statement by
    select * from A a where a.x1 not in (select b.x1 from B b)
    Regards,
    Deepak Suri

  • Stored Procedure vs Inline Query

    Hi all,
    The other day i came across a link
    http://www.blackwasp.co.uk/SpeedTestSqlSproc.aspx
    saying that in sql server stored procedure takes more time for execution
    rather than inline queries.
    Is this true in the case of Oracle.Kindly seeking valuable suggestions from gurus
    Regards
    josh

    Handle:      user588120
    Status Level:      Newbie (5)
    Registered:      Aug 2, 2007
    Total Posts:      228
    Total Questions:      29 (25 unresolved)
    so many questions & so fews answers.
    Is this true in the case of OracleWhat will you do differently if it is always true?
    What will you do differently if it is always false?
    What will you do differently if it depends upon externals factors?

  • Inline query issue

    Hi,
    I'm creating a report which will dislplay all purchase orders pending for approval :
    iI've a table po_headers_all will give all purchase orders
    I've another table called po approival history from that table I need to check whether particular purchase is approved or not if it is not approved I need to pull most recent user as approver
    po_orders table is like this :
    po number   creation date header id   
    1111            12/31/2012     234   .....History table is like this
    Seq  po number  creation date             action        approver
    1       1111          01/01/2013  12:00    submit        abc
    2       1111          01/01/2013  1:30      forward      abc
    3       1111           01/10/2013  12:50    in process   bcd
    4       1111           01/12/2013    1:30     approve     gcf
    5       1111           01/13/2013    2:30     approve     hcfwhile pulling the purchase order details from po_headers table I need to joiin these table and get
    the most recent approver i.e for action=approve
    I'm expecting result like this :
    po number header id  approver
    1111          234          hcfBut I'm getting more than one record with the following query :
    select a.po_number,
             a.header_id,
            b.action,
           b.approver
    from po_headers_all a
    (select max(seq_num),approver from po_history
    group by approver
    having action='approve') b
    where a.po_header_id = b.po_header_id

    user518071 wrote:
    po_orders table is like this :
    po number   creation date header id   
    1111            12/31/2012     234   .....History table is like this
    Seq  po number  creation date             action        approver
    1       1111          01/01/2013  12:00    submit        abc
    2       1111          01/01/2013  1:30      forward      abc
    3       1111           01/10/2013  12:50    in process   bcd
    4       1111           01/12/2013    1:30     approve     gcf
    5       1111           01/13/2013    2:30     approve     hcfI'm expecting result like this :
    po number header id  approver
    1111          234          hcfBut I'm getting more than one record with the following query :
    select a.po_number,
    a.header_id,
    b.action,
    b.approver
    from po_headers_all a
    (select max(seq_num),approver from po_history
    group by approver
    having action='approve') b
    where a.po_header_id = b.po_header_id
    couple of questions:
    1) Firstly your data does not seems to be correct, because you are joining using po_header_id in history table which does not exist in the data you provided.
    2) Secondly your query will not work as there is comma missing.
    Help us to help you :)
    Cheers,
    Manik.

  • Need help in writing inline query or subquery

    Hi All,
    I have 4 select statements like below ,
    1>Select customer_number, customer_name,customer_address1
    FROM ABC
    WHERE Site_use_code='BILL_TO'
    2>Select customer_number, customer_name,customer_address1
    FROM ABC
    WHERE Site_use_code='SHIP_TO'
    3>Select customer_number, customer_name,customer_address1
    FROM ABC
    WHERE Site_use_code='SOLD_TO'
    I need to write all these 3 in single select statement in such a way that if any select statement doesnot return any data it should execute the other select statements means it should not fail.
    for each column of the select statment i can write separate select statements in that way it should work but the query should be very big.
    So please help me on thsi.
    Thanks

    Example of my solution:
    SQL> ed
    Wrote file afiedt.buf
      1  with t as (
      2    select 1 as customer_number, 'Fred' as customer_name, 'address1' as customer_address1, 'BILL_TO' as site_use_code from dual union all
      3    select 1 as customer_number, 'Fred' as customer_name, 'address1' as customer_address1, 'SHIP_TO' as site_use_code from dual union all
      4    select 2 as customer_number, 'Bob' as customer_name, 'address2' as customer_address1, 'BILL_TO' as site_use_code from dual union all
      5    select 3 as customer_number, 'George' as customer_name, 'address3' as customer_address1, 'BILL_TO' as site_use_code from dual union all
      6    select 3 as customer_number, 'George' as customer_name, 'address3' as customer_address1, 'SHIP_TO' as site_use_code from dual union all
      7    select 3 as customer_number, 'George' as customer_name, 'address3' as customer_address1, 'SOLD_TO' as site_use_code from dual union all
      8    select 4 as customer_number, 'Tim' as customer_name, 'address4' as customer_address1, 'BILL_TO' as site_use_code from dual union all
      9    select 5 as customer_number, 'Sam' as customer_name, 'address5' as customer_address1, 'BILL_TO' as site_use_code from dual
    10  )
    11  --
    12  -- END OF TEST DATA
    13  --
    14  select customer_number
    15        ,customer_name
    16        ,customer_address1
    17        ,site_use_code
    18  from (
    19        select customer_number
    20              ,customer_name
    21              ,customer_address1
    22              ,site_use_code
    23              ,row_number() over (partition by customer_number order by decode(Site_use_code,'BILL_TO',1,'SHIP_TO',2,3) desc) as rn
    24        FROM t
    25        WHERE Site_use_code IN ('BILL_TO','SHIP_TO','SOLD_TO')
    26       )
    27* where rn = 1
    SQL> /
    CUSTOMER_NUMBER CUSTOM CUSTOMER SITE_US
                  1 Fred   address1 SHIP_TO
                  2 Bob    address2 BILL_TO
                  3 George address3 SOLD_TO
                  4 Tim    address4 BILL_TO
                  5 Sam    address5 BILL_TO
    SQL>Or alternative...
    SQL> ed
    Wrote file afiedt.buf
      1  with t as (
      2    select 1 as customer_number, 'Fred' as customer_name, 'address1' as customer_address1, 'BILL_TO' as site_use_code from dual union all
      3    select 1 as customer_number, 'Fred' as customer_name, 'address1' as customer_address1, 'SHIP_TO' as site_use_code from dual union all
      4    select 2 as customer_number, 'Bob' as customer_name, 'address2' as customer_address1, 'BILL_TO' as site_use_code from dual union all
      5    select 3 as customer_number, 'George' as customer_name, 'address3' as customer_address1, 'BILL_TO' as site_use_code from dual union all
      6    select 3 as customer_number, 'George' as customer_name, 'address3' as customer_address1, 'SHIP_TO' as site_use_code from dual union all
      7    select 3 as customer_number, 'George' as customer_name, 'address3' as customer_address1, 'SOLD_TO' as site_use_code from dual union all
      8    select 4 as customer_number, 'Tim' as customer_name, 'address4' as customer_address1, 'BILL_TO' as site_use_code from dual union all
      9    select 5 as customer_number, 'Sam' as customer_name, 'address5' as customer_address1, 'BILL_TO' as site_use_code from dual
    10  )
    11  --
    12  -- END OF TEST DATA
    13  --
    14  select distinct
    15         customer_number
    16        ,customer_name
    17        ,customer_address1
    18        ,first_value(site_use_code) over (partition by customer_number order by decode(Site_use_code,'BILL_TO',1,'SHIP_TO',2,3) desc) as site_use_code
    19  FROM t
    20  WHERE Site_use_code IN ('BILL_TO','SHIP_TO','SOLD_TO')
    21* order by customer_number
    SQL> /
    CUSTOMER_NUMBER CUSTOM CUSTOMER SITE_US
                  1 Fred   address1 SHIP_TO
                  2 Bob    address2 BILL_TO
                  3 George address3 SOLD_TO
                  4 Tim    address4 BILL_TO
                  5 Sam    address5 BILL_TO

  • Improve Performance on Inline view Query

    All,
    I have a unique situation where I have to limit the number of rows based on group. so I created an Inline view and applied a limit on it.
    ex:
    SELECT col_A, col_B FROM ( SELECT col_a, count(*) FROM tab_a WHERE col_a = 'XXX' GROUP BY col_a) ROWNUM <-10.
    but this design was rejected, because it seems to have a great impact on performance due to inline view.
    Also I cant set a rowlimit directly on the query because GROUP BY clause is used in the select.
    When the rownum is directly applied in the WHERE, first it limits the rows and then it makes a GROUP, so when user asks to retrieve 10 records, it may show less than 10 rows because of the grouping.
    please help to find a alternative solution which helps to get the expected result set and same time without loosing performance

    but this design was rejected, because it seems to have a great impact on performance due to inline view.What proof was offered that this query performed badly. There is nothing inherently non-performant about in-line views. I would demand to see some evidence that this query uses an unreasonable amount of system resource.
    However, you could try this:
    SELECT t.col1, avg(t.col2)
    FROM   t1 t
    WHERE  t.col1 IN ( SELECT DISTINCT x.col1
                       FROM   t1 x
                       WHERE  col3 = 'XXX'
                       AND    rownum <=10 )
    GROUP  BY t.col1
    /I assume your example is not derived from your real code (otherwise the WHERE clause would restrict the inline query to one returned row - problem solved!)
    Cheers, APC

Maybe you are looking for

  • Error during installation phase when upgrading  from oracle 10 to 11g

    hi, We have going to upgrade oracle 10.2.0.4 to 11.2.0.3 in SAP ECC 6.4(DB:oracle, OS:AIX6.1). By mistake I run RUNINSTALLER from wrong path oracle/stage/11203 <dump folder>/database instead of SAP folder.it gives some error message during installati

  • How to pass a LOV variable to javascript?

    Hi All, I have a drop down list (LOV) with the following entry in Element Attributes: onChange="javascript:show_value(this);" The javascript is like this: function show_value(taskid) document.getElementById('P28_TEST_ONLY').value = taskid; However wh

  • 3600 slide - battery getting very hot on charging

    Is this normal that the phone gets hot on charging. Or is it a flaw with the battery or the phone. Please advise

  • Accessing the network from extproc

    Hi! (dont know if this the correct forum category, but couldnt find any other suitable). We have a 11.2 database running on Red Hat 5.5. The database have a scheduled job to fetch some files from another server using ftp, and herein lies the problem.

  • Sub-total in a XML Crosstab report

    Hi, Can anyone help me to get the subtotal for the crosstab within another group report. Format is like this: Method Analysis Unit Test1 Test2 Test3 ....... Avg Quanity Moisture % 13.35 13.40 13.40 13.38 Ash (db) % 13.35 13.40 13.40 13.38 Protein % 1