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

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

  • 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.

  • SQL query SUM and AVG

    Hi all,
    I think I'm being really stupid but I'm having SQL query issues.
    I have a table with lots of lines of transactions which all refer to different stores, for example:
    Store..............._Sales_............._Month_
    Reading............200k..............April
    Leeds...............50k................April
    Manchester........70k................May
    Reading............100k..............May
    I need to come up with the average Sales for the combined months as a total. I.e. the average Store revenue for a given period is 200k+ 50k + 70k +100k / *3* (As there are only 3 unique stores) - hopefully this makes sense!
    So essentially I am trying to do both a SUM query (and grouping by store) and then outputting the average of all stores together. Is this possible?
    Thank you,

    Hello,
    This query returns 140 what seems to be the correct value:
    with data as
    ( select 'Reading' Store
      , 200 sales
      from dual
      union
      select 'Leeds'
      , 50
      from dual
      union
      select 'Manchester'
      , 70
      from dual
      union
      select 'Reading'
      , 100
      from dual
    select sum(sales)
    , count( distinct store )
    , sum(sales)/count(distinct store)
    from dataThere are multiple options:
    1. You can get two IR's on one page (by using IFRAMEs - search for that word on the Forum)....
    2. You create another region with the query above and position that just below the report
    3. In the Region Footer call a PL/SQL process (using AJAX) that calculates the value using the query and prints it there (using htp.p)
    Greetings,
    Roel
    http://roelhartman.blogspot.com/
    http://www.bloggingaboutoracle.org/
    http://www.logica.com/
    You can award this reply to your question by marking it as either Helpful or Correct ;-)

  • [SQL] Query Sum

    Hi all, hope in your help.
    This is my table:
    +----+--------+--------+
    | id | field1 | field2 |
    +----+--------+--------+
    |  1 | A1     | 7      |
    |  2 | B1     | 9      |
    |  3 | C1     | 0      |
    |  4 | D1     | 3      |
    |  5 | A2     | 5      |
    |  6 | B2     | 6      |
    |  7 | C2     | 7      |
    |  8 | D2     | 8      |
    +----+--------+--------+I need this output:
    +--------+--------------------+
    | field1 | field2             |
    +--------+--------------------+
    | A2     | 19.230769230769200 |
    +--------+--------------------+
    | B2     | 23,076923076923100 |
    +--------+--------------------+
    | C2     | 26,923076923076900 |
    +--------+--------------------+
    | D2     | 30,769230769230800 |
    +--------+--------------------+and tried this query
    where calculate the value of single field1 (5,6,7,8) divided by the sum of field2 equal to A1, B2, C2 and D2 (26) :
    A = 5/26 * 100 = 19
    B = 6/26 * 100 = 23
    C = 7/26 * 100 = 26
    D = 8/26 * 100 = 30
    SELECT
         field1,
         field2/Sum(field2)*100 as field2
    FROM
         `tbl_t`
    WHERE
         1
    AND field1 IN ('A2', 'B2', 'C2', 'D2');
    +--------+--------------------+
    | field1 | field2             |
    +--------+--------------------+
    | A2     | 19.230769230769234 |
    +--------+--------------------+But the ouput is not what I want, can you help me?
    Thank you
    Any help would be greatly appreciated.
    Edited by: user13264840 on 25-mag-2013 10.33

    WITH data AS
      ( SELECT 1 id , 'A1' Field1 , 7 Field2 FROM dual
    UNION ALL
       SELECT 2 , 'B1' , 9 FROM dual
    UNION ALL
       SELECT 3 , 'C1' , 0 FROM dual
    UNION ALL
       SELECT 4 , 'D1' , 3 FROM dual
    UNION ALL
       SELECT 5 , 'A2' , 5 FROM dual
    UNION ALL
       SELECT 6 , 'B2' , 6 FROM dual
    UNION ALL
       SELECT 7 , 'C2' , 7 FROM dual
    UNION ALL
       SELECT 8 , 'D2' , 8 FROM dual
      Group_data AS
      (SELECT  id ,
        field1    ,
        field2    ,
        SUBSTR ( field1 , regexp_instr ( field1 , '[1234567890]' ) ) groupBy
          FROM data
      SELECT  Field1 ,
       ratio_to_report ( field2 ) over ( partition BY groupby ) * 100 "Field2"
         FROM group_data;

  • Query sum of daily maximum

    Hi!
    I need to query the sum of a daily maximum value but I get an ORA-00936: missing expression with the following query. Why?
    select to_char(a.mytimestamp, 'MM/YYYY'),
    sum(select max(b.wert) from values b group by trunc(b.mytimestamp))
    from values a
    group by to_char(a.mytimestamp, 'MM/YYYY')
    order by to_char(a.mytimestamp, 'MM/YYYY') desc;
    The subselct within sum (select max(b.wert) from values b group by trunc(b.mytimestamp)) works as standalone.
    Without the subselect the query works, too!
    select to_char(a.zeitstempel, 'MM/YYYY'),
    sum(a.wert)
    from values a
    group by to_char(a.mytimestamp, 'MM/YYYY')
    order by to_char(a.mytimestamp, 'MM/YYYY') desc;
    Can anybody help?

    HI,
    something like this
    SELECT   to_char(a.mytimestamp, 'MM/YYYY'), (SELECT   sum(max(b.wert))
                                                     FROM values b
                                                 GROUP BY trunc(b.mytimestamp))
        FROM values a
    GROUP BY to_char(a.mytimestamp, 'MM/YYYY')
    ORDER BY to_char(a.mytimestamp, 'MM/YYYY') DESC;edit:
    Just to get the syntax right - not sure what you actually want
    Regards
    Peter
    Message was edited by:
    Peter Gjelstrup

  • Sql query : sum of feild between two conditional date

    hi
    I have a table whit 3 field like below. I want to Find the
    total number of hours in mode "run" after the latest mode "decoke".
    field time is for every day and its unique.
    id -- time  --  mode -- hour
    1  --1/1/1 -- run  --  24
    2  -- 1/1/2  -- off  -- 24
    3  -- 1/1/3  decoke  -- 24
    4  -- 1/1/4  -- run  -- 24
    5  -- 1/1/5  -- run -- 24
     this query should add row 4 and 5 , the latest "run" after "decoke"

    Create table #temp
    [id] int identity(1,1)
    , [time] date
    , [mode] varchar(50)
    , [hour] int
    Insert into #temp
    Select '1/1/1' , 'run' ,24
    Union
    Select '1/1/2' , 'off' ,24
    Union
    Select '1/1/3' , 'decoke' ,24
    Union
    Select '1/1/4' , 'run' ,24
    Union
    Select '1/1/5' , 'run' ,24
    -----Here is the Query-------
    ;With Cte_TotalTime
    As
    Select Max(time)[Time] from #temp Where mode = 'decoke'
    Select SUM(a.hour) TotalRunHours from #temp a
    Inner join Cte_TotalTime b on a.[time] > b.[Time]
    And mode = 'run'
    Drop table #temp
    Please mark as answer if it is helpful to you.
    Thanks

  • 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

  • Sql Query : Sum , all the possible combination of rows in a table

    SQL Server 2008 R2
    Sample Table structure
    create table TempTable
    ID int identity,
    value int
    insert into TempTable values(6)
    insert into TempTable values(7)
    insert into TempTable values(8)
    insert into TempTable values(9)
    insert into TempTable values(10)
    I actually want something like below,
    that It returns rows such that, it has the sum of all the remaining rows , as shown below.
    I will ignore the repeated rows.
    6+7
    6+8
    6+9
    6+10
    6+7+8
    6+7+9
    6+7+10
    6+8+9
    6+8+10
    6+9+10 
    6+7+8+9
    6+7+8+9+10

    This makes no sense. What about NULLs? What about duplicate values? You have rows, so you have many combinations. We know that this set totals to 39, so there is no subset totals to half of any odd number.  19.5
    in this case. 
    None of your examples are
    even close! 
     6+7 = 13
    the remaining rows 8+9+10 = 27, so you failed! 
    Want to try again?
    --CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
    in Sets / Trees and Hierarchies in SQL

Maybe you are looking for