Conversion of SQL Query Help

Hello, I'm doing a migration of SQL Server T - SQL to oracle 11 g PL/SQL, and I don't want to use a global temporary table; Instead, consider using sliders and pl/sql table.

I got hit in a scenario where I need to convert the following in a single sql statement.

Stage of T - SQL:
============

Insert (#ActCde)
InvntrySbLdgrChngeID
DlHdrID
DlDtlID
ChemID
StrtgyID
Quantity
Value
Modified)
Select InvntrySbLdgrChngeID
DlHdrID
DlDtlID
ChildPrdctID
StrtgyID
VolumeChanged
ValueChanged
, 'N'
of InventorySubLedgerChange
where LastInChain = 'Y '.

-updated column "Changed" of particular documents to 'Y' based on conditions

Update #ActCde
Changed the value = 'Y '.
of #ActCde (nolock)
Left Outer Join InventorySubledgerDetail ISD (nolock)
on #ActCde.DlHdrID = DSI. DlDtlChmclDlDtlDlHdrID
and #ActCde.DlDtlID = ISD. DlDtlChmclDlDtlID
and #ActCde.ChemID = DSI. DlDtlChmclChmclChldPrdctID
and #ActCde.StrtgyID = DSI. StrtgyID
and the DSI. AccntngPrdID = @i_AccntngPrdID - variable parameter
Where abs (#ActCde.Qty - isnull (ISD.) BeginningVolume, 0)) >.01
or abs (#ActCde.Value - isnull (ISD.) BeginningValue, 0)) >.01
or (abs(#ActCde.Qty) = 0 and)
ABS(#ActCde.value) = 0 and
ABS (isnull (ISD. BeginningVolume, 0)) = 0 and
ABS (isnull (ISD. BeginningValue, 0)) = 0
)

Convertion of SQL:
===============
I would like to do something like

Select A.InvntrySbLdgrChngeID,
A.DlHdrID,
A.DlDtlID,
A.ChemID,
A.StrtgyID,
A.Qty,
A.Value,
A.Changed
Of
(select InvntrySbLdgrChngeID
DlHdrID
DlDtlID
ChildPrdctID AS ChemID
StrtgyID
VolumeChanged AS Qty,
ValueChanged value have
, 'N'
of InventorySubLedgerChange
where LastInChain = 'Y') A LEFT OUTER JOIN InventorySubledgerDetail B
ON A.DlHdrID = B.DlDtlChmclDlDtlDlHdrID
and A.DlDtlID = B.DlDtlChmclDlDtlID
and A.ChemID = B.DlDtlChmclChmclChldPrdctID
and A.StrtgyID = B.StrtgyID
and B.AccntngPrdID = i_AccntngPrdID - parameter varibale
Where abs (A.Qty - isnull(B.BeginningVolume,0)) >.01
or abs (A.Value - isnull(B.BeginningValue,0)) >.01
or (abs (A.Qty) = 0 and)
ABS (A.value) = 0 and
ABS (IsNull(B.BeginningVolume,0)) = 0 and
ABS (IsNull(B.BeginningValue,0)) = 0
)

And I need to change the 'A.CHANGED' column value 'Y', based on the outer join, as is above T - SQL.

IF this wont work so pls help me to another way to do this.

in this regard, your help is appreciated.

Thank you!

SS

Try this

select #ActCde.InvntrySbLdgrChngeID
       , #ActCde.DlHdrID
       , #ActCde.DlDtlID
       , #ActCde.ChildPrdctID
       , #ActCde.StrtgyID
       , #ActCde.VolumeChanged
       , #ActCde.ValueChanged
       , case when abs(#ActCde.Qty - isnull(ISD.BeginningVolume,0)) > .01  or
                   abs(#ActCde.Value - isnull(ISD.BeginningValue,0)) > .01 or
                   (
                         abs(#ActCde.Qty)                   = 0 and
                         abs(#ActCde.Value)                 = 0 and
                         abs(isnull(ISD.BeginningVolume,0)) = 0 and
                         abs(isnull(ISD.BeginningValue,0))  = 0
                   )
              then 'Y'
              else 'N'
         end changed
  from InventorySubLedgerChange #ActCde
  Left Outer Join InventorySubledgerDetail ISD (nolock)
    on #ActCde.DlHdrID = ISD.DlDtlChmclDlDtlDlHdrID
   and #ActCde.DlDtlID= ISD.DlDtlChmclDlDtlID
   and #ActCde.ChemID = ISD.DlDtlChmclChmclChldPrdctID
   and #ActCde.StrtgyID = ISD.StrtgyID
   and ISD.AccntngPrdID = @i_AccntngPrdID - paramenter variable
 where LastInChain = "Y"

Tags: Database

Similar Questions

  • SQL query - help with join

    Dear friends,

    Version of DB - 11.1.0.7... , I'm stuck with SQL basics today... need your help...

    The slot SQL tells me "cache them locks library" in the database that I will put up as a proactive measure.

    I'll be it works via shell script and include the table gv instance_name $ instance ... I'm a little confused as to how a 3rd table "gv$ instance ' can be introduced into the query in order to make the instance_name in the result set...

    SELECT * FROM)

    SELECT / * + LEADING (a) USE_HASH (u) * /.

    instance_name, INST_ID select, blocking_inst_id, blocking_session, username, session_id, sql_id, current_obj #,.

    DECODE (sql_opcode, 1, 'CREATE TABLE', 2, 'INSERT') as "order."

    Event, mod(P1,16) p1, p2, p3

    COUNT (*) totalseconds

    , SUM (CASE WHEN wait_class = 'Application' THEN 1 ELSE 0 END) 'Application '.

    Of

    (SELECT

    a.*

    , TO_CHAR (CASE WHEN session_state = 'WAITING' THEN ELSE null END p1, '0XXXXXXXXXXXXXXX') p1hex

    , TO_CHAR (CASE WHEN session_state = "PENDING" THEN p2 ELSE null END, '0XXXXXXXXXXXXXXX') p2hex

    , TO_CHAR (CASE WHEN session_state = "PENDING" THEN ELSE null END p3, '0XXXXXXXXXXXXXXX') p3hex

    SGS $ active_session_history one) a

    u dba_users

    WHERE

    a.user_id = u.user_id

    AND sample_time BETWEEN sysdate-90 /(24*60) AND sysdate

    - AND a test of ('library cache lock', 'library cache pin")

    AND event like '% library '.

    GROUP BY

    INST_ID select, blocking_inst_id, blocking_session, username, session_id, sql_id, current_obj #,.

    DECODE (sql_opcode, 1, 'CREATE TABLE', 'INSERT', 2),

    event, mod (p1, 16), p2, p3

    Having count (*) > 5

    ORDER BY

    TotalSeconds DESC

    , INST_ID select, blocking_session, username, session_id, sql_id, current_obj #, 'Order', event

    )

    WHERE

    ROWNUM < = 20

    /

    replace

    instance_name

    by

    (select instance_name gv$ instance where INST_ID select = a.inst_id) instance_name

    or select... in... a, u, gv$ instance where... and gv$ instance.inst_id (+) = a.inst_id...

  • The join SQL query help

    I'm just having a bit of troubel get a correct join query - I thought it was an Inner Join, but I don't get the results I expect.

    My table structure is:

    Table: lodges

    LodgeID (PK)

    Lodge

    etc.

    Table: implemented application

    NominationID (PK)

    Category

    LodgeID

    Year

    So I try to use this structure to replicate this page:

    http://www.safariawards.com/nominees12/

    That is to say a list of boxes for each category, they are appointed on.

    The query I've tried looks like this:

    SELECT appointments. LodgeID, lodges. Lodge, applications. NominationID, applications. Lodges INNER JOIN applications category IT lodges. LodgeID = nominated. NominationID WHERE category = "Best property of Safari in southern Africa" ORDER BY Lodge

    But this product:

    http://www.safariawards.com/nominees12/southernafrica.php

    Its the right number of results, but not the list on the right of the boxes - for example British Airwways is not LodgeID 786

    If anyone could help with the SQL right for what would be well appreciated.

    That you join on the wrong column. Try this:

    SELECT appointments. LodgeID, lodges. Lodge, applications. NominationID, applications. Lodges INNER JOIN applications category IT lodges. LodgeID = nominated. LodgeID WHERE category = "Best property of Safari in southern Africa" ORDER BY Lodge

  • SQL Query help find albums from sale

    Hi Experts,

    I have the following data and the need to find the book Top sold in each type.

    Book Type QTY

    20Help the3
    10Kitchen1
    5Navigation2
    30Help the4

    Please let me how can know we get this simple SQL help?

    Thank you

    Bharat

    Hello

    Bharat Hegde wrote:

    Hi all

    I tried to use Dense_rank as below. But it gives me the top selling books. I need high library in each type...

    This looks like a job for 'PARTITION BY type

    For example:

    WITH got_rnk AS

    (

    SELECT b.bid, b.type

    SUM (o.quantity) AS total_quantity

    DENSE_RANK () OVER ( PARTITION BY b.type

    ORDER OF SUM (o.quantity) / / DESC

    ) AS rnk

    B BOOK

    o order1

    WHERE b.bid = o.bid

    GROUP BY b.bid, b.type

    )

    SELECT total_quantity, type submission

    OF got_rnk

    WHERE rnk = 1

    ;

    . Aggregate functions (such as the SUM, above) are calculated before analytical functions, so an analytic function (such as DENSE_RANK above) may depend on an aggregate function; you don't need a separate subquery for that.

  • SQL Query Help (not working not properly)

    Hello everyone,

    I use JDeveloper 12.1.2.0.0. I do a two-way communication using 3 tables, with links between them (and using schema HR).

    In my example, I have something like:

    Departments, employees, and the SalaryByJobs (I created this table where it shows a departmentd id, employee id, salary).

    Whenever I click on one OR more departments, the employees up-to-date table by putting on the table, employees who belong to the selected department and the salaryByjob to put the jobs of the employees selected on the employees table.

    So it's something like this:

    The departments selected-> employees selected-> salarybyjbobs of these employees.

    My query used (in the view) has the following code:

    SELECT Employees.COMMISSION_PCT,

    Employees.DEPARTMENT_ID,

    Employees.EMAIL,

    Employees.EMPLOYEE_ID,

    Employees.FIRST_NAME,

    Employees.HIRE_DATE,

    Employees.JOB_ID,

    Employees.LAST_NAME,

    Employees.MANAGER_ID,

    Employees.PHONE_NUMBER,

    Employees.SALARY

    Employees EMPLOYEES

    WHERE (department_id IN (select * from THE (select cast (in_list(:variavel3) as mytableType) double) a))

    Since I use the links, the employees table does not show anything at the beginning, so I added this to my query used to go: OR nvl(:variavel3,0) = 0

    But now, whenever I try to select multiple lines, it gives me the invalid numbers and I don't understand why...

    It's only one line of code and it is not in the bean.

    Can someone help me?

    PS - The bean will Department by Department, adds the departments with ',' and for each Department, gets employees who belongs to them.

    My best regards,

    Frederico Barracha.

    The expression NVL (: variavel3, 0) = 0 is not correct. The data type of the return value of the NVL function is considered to be equal to the data type of the argument of 1 (that is, the data type of the variable binding: variavel3). You said that this variable contained a list separated by commas to ID, so the data type of the variable is VARCHAR2. As long as you compare the NVL expression of a number, you get 'Invalid number' exception, because Oracle expects a numeric data type on the left side of the comparison operator.

    To avoid the exception "Invalid number", you can modify the expression by using one of the following options:

    : variavel3 IS NULL

    NVL (: variavel3, ' *') = ' *'

    NVL (: variavel3, ' 0') = '0'

    Option 1, so the simplest and clearest.

    Dimitar

  • SQl query help please...

    Hello

    I have a requirement... can help you pleease

    I have a table with two columns called Doc R, Seq R

    Sampled data:
    Type          Doc R           Seq R
    --------        ---------        ------------
    R                R1               Null
    Q               Q1                R1
    R                R2               Null
    Q               Q2               R2
    R                R3               Null
    Q               Null              Null
    
    
    Now  if you see first two records R1 and R2 have corresponding Q2.....
    if you see R3 dont have Q values..
    
    Now i want the count of R3 like records where it does not have Corresponding Q values in ...
    
    any ideas please...                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    

    Hello

    Another way, which could be more efficient (but less clear):

    WITH   got_grp         AS
    (
         SELECT     CASE WHEN rtype = 'Q' THEN seq_r ELSE doc_r END     AS grp
         ,     CASE WHEN rtype = 'Q' THEN 0     ELSE 1     END     AS r_val
         FROM     yourtable
         WHERE     rtype  IN ('Q', 'R')
    )
    SELECT       COUNT (COUNT (*))     AS grp_cnt
    FROM       got_grp
    GROUP BY  grp
    HAVING       COUNT (*)     = SUM (r_val)
    ;
    

    You have not said if a line where
    rtype = 'R' and doc_r has the value NULL is considered to be a match for a line where
    rtype = 'Q' and seq_r is NULL.

    In the application of Cyn, they are not a match.
    In the above query, they are.
    A query can be changed for a different result.

  • Join SQL query help

    I have two DEVICE and CONTACT tables. The tables are joined for CONTACT_ID DEVICE_ID. I want to print device_ID and the corresponding coordinates on the lower contact. PRIORITY.
    SQL> describe DEVICE;
    DEVICE_ID              NOT NULL VARCHAR2(50)
    
    SQL> describe CONTACT;
    CONTACT_ID               NOT NULL VARCHAR2(50)
    CONTACT_TYPE             NOT NULL VARCHAR2(4)
    PRIORITY                 NOT NULL DOUBLE PRECISION
    LASTNAME                 NOT NULL VARCHAR2(30)
    FIRSTNAME                NOT NULL VARCHAR2(80)
    
    SQL> SELECT a.DEVICE_ID, b.LASTNAME,b.PRIORITY from DEVICE a, CONTACT b where a.DEVICE_ID = 'DEVICEA' and a.DEVICE_ID=b.CONTACT_ID(+);
    DEVICE_ID              LASTNAME    FIRSTNAME PRIORITY CONTACT_TYPE
    ---------------------- ----------- --------- -------- ------------
    DEVICEA                CONTACT1     GN               1 ROUT
    DEVICEA                CONTACT2     Hans             2 ROUT
    How to print a single record with the CONTACT the lowest. PRIORITY?

    Thank you
    Ravi

    rmalghan wrote:
    Thank you Frank, Sundar and Sean. I had less than 3 to work. Two questions. My table of DEVICES has 31936 records.
    1. the first one returned 30207, 31936 records 2nd and 3rd a 30348. Since the 2nd result resembles what I expected, the 1st and 3rd one seem to miss some records. For troubleshooting, I tried to limit the query to a few devices, but do not understand why the difference. Any suggestions?

    The 2nd query not check for type_contact = 'ROUT', it is not surprising that it returns more rows.
    The request of 3rd fact actually an inner join. The "WHERE c.rnum = 1" condition is applied after the join, so he rejects all rows that have been added due to the outer join (which is where all the columns of c are NULL). You probably meant to say:

    LEFT OUTER JOIN got_rnum c ON  d.DEVICE_ID     = c.CONTACT_ID
                               AND c.rnum          = 1; 
    

    without a WHERE clause.

    2. are there advantages in terms of efficiency. 2nd one does not seem to run faster, so I think it's the best. Any comments?

    SELECT a.DEVICE_ID, b.LASTNAME,b.FIRSTNAME,b.PRIORITY
    FROM DEVICE a, CONTACT b
    where
    a.DEVICE_ID=b.CONTACT_ID(+) and
    b.CONTACT_TYPE = 'ROUT' and
    b.PRIORITY = (select min(b1.PRIORITY ) from CONTACT b1 where b1.CONTACT_ID = b.CONTACT_ID);
    
    30207 rows selected (10.12 seconds)
    
    SELECT DEVICE_ID, LASTNAME, PRIORITY
    FROM (
    SELECT a.DEVICE_ID, b.LASTNAME, b.PRIORITY, ROW_NUMBER() OVER (PARTITION BY a.DEVICE_ID ORDER BY b.PRIORITY) row_num
    from DEVICE a, CONTACT b
    where a.DEVICE_ID=b.CONTACT_ID(+))
    WHERE row_num = 1;
    
    31936 rows selected (6.68 seconds)
    
    WITH got_rnum AS (
    SELECT LASTNAME, FIRSTNAME, PRIORITY, CONTACT_TYPE, CONTACT_ID,
    ROW_NUMBER () OVER ( PARTITION BY  CONTACT_ID ORDER BY priority) AS rnum
    FROM contact
    where CONTACT_TYPE = 'ROUT'
    )
    
    SELECT d.DEVICE_ID, c.LASTNAME, c.FIRSTNAME, c.PRIORITY
    FROM device d
    JOIN got_rnum c ON d.DEVICE_ID     = c.CONTACT_ID(+)
    WHERE c.rnum = 1; 
    
    30348 rows selected (9.07 seconds)
    

    This calendar is not very accurate. One that you run first is likely to be slower, because the data are less likely to be stored in the memory cache.

    The 2nd query has only 3 columns in the result set. Which probably doesn't change the speed a lot, but you should fix it anyway.

  • SQL query help

    Hello

    I have the table and professorid, studentid, classid.

    Please provide the sql code to find professor_id which hosts up to students.

    with t as)

    Select professor_id,

    ROW_NUMBER() over (order by count (distinct student_id) desc) rn

    from your_table

    Professor_id group

    )

    Select professor_id

    t

    where rn = 1

    /

    However, if there is a tie and a teacher number max of students has more meanings will select one of them. If you want that all teachers use:

    with t as)

    Select professor_id,

    DENSE_RANK() over (order by count (distinct student_id) desc) rn

    from your_table

    Professor_id group

    )

    Select professor_id

    t

    where rn = 1

    /

    SY.

  • SQL query help needed in the Clause type

    Hello

    I'm currently learning Clause type. How can I write in the clause type to get the result below:

    I have a table like this

    DEPTNO ENAME

    -------           ------

    20 SMITH

    CELINE 10

    BOND 20

    ALLEN 30

    WARD 30

    20 JONES

    30 MARTIN

    30 BLAKE

    10 CLARK

    SCOTT 20

    10 KING

    30 TURNER

    20 ADAMS

    30 JAMES

    20 FORD

    10 MILLER

    I want the output should look like this

    DEPTNO ENAME

    ------     -----

    CELINE 10, CLARK, KING, MILLER

    20 JONES, ADAMS, BOND, FORD, SCOTT, SMITH

    30 MARTIN, JAMES BLAKE, WARD, TURNER, ALLEN

    Hear is the script I am currently working on.

    with emp_group as

    (

    Select 20 deptno, ename 'SMITH' Union double all the

    choose 10, 'CÉLINE' from dual union all

    Select 20, "BOND" of all the double union

    Select 30, 'ALLEN' from dual union all

    Select 30, 'WARD' of all the double union

    Select 20, "JONES' from dual union all

    Select 30, 'MARTIN' from dual union all

    Select 30, "BLAKE" from dual union all

    choose 10, 'CLARK' from dual union all

    Select 20, 'SCOTT' from dual union all

    choose 10, 'KING' of the dual union all

    Select 30, 'TURNER' from dual union all

    Select 20, 'ADAMS' from dual union all

    Select 30, 'JAMES' of the dual union all

    Select 20, 'FORD' Union double all the

    choose 10, 'MILLER' from dual

    )

    Select

    DEPTNO

    A1

    of emp_group

    model

    ignore the nav

    partition (deptno)

    dimension (ROW_NUMBER() over (PARTITION BY deptno ORDER BY ename DESC) rn)

    measures (ename, LPAD(' ', 100) A1)

    rules)

    A1 [rn > 0] = ename [cv ()],

    A1 [0] = ename [cv ()]

    )

    order by deptno

    ;

    your help is very appreciated.

    Thank you in advance.

    Try this.

    Select

    *

    Of

    (

    with emp_group as

    (

    Select 20 deptno, ename 'SMITH' Union double all the

    choose 10, 'CÉLINE' from dual union all

    Select 20, "BOND" of all the double union

    Select 30, 'ALLEN' from dual union all

    Select 30, 'WARD' of all the double union

    Select 20, "JONES' from dual union all

    Select 30, 'MARTIN' from dual union all

    Select 30, "BLAKE" from dual union all

    choose 10, 'CLARK' from dual union all

    Select 20, 'SCOTT' from dual union all

    choose 10, 'KING' of the dual union all

    Select 30, 'TURNER' from dual union all

    Select 20, 'ADAMS' from dual union all

    Select 30, 'JAMES' of the dual union all

    Select 20, 'FORD' Union double all the

    choose 10, 'MILLER' from dual

    )

    Select

    DEPTNO

    names

    rn

    of emp_group

    model

    ignore the nav

    partition (deptno)

    dimension of)

    ROW_NUMBER() over (PARTITION BY deptno ORDER BY ename) rn

    )

    measures (ename, cast (null as names of varchar2 (40)))

    rules

    (

    names [any] order by desc = ename [cv ()] rn | «, » || names [cv () + 1]

    )

    order by deptno

    )

    where rn = 1

    Best regards, Andrei

  • SQL query help (we connect by clause level)

    Hi all
    I have this application developed with data with the clause.
     With dat As
    (
      select '@AAA @SSS @DDD' col1 from dual union all
      select '@ZZZ @XXX @TTT @RRR @ZZA' col1 from dual 
    )
    Select regexp_substr( col1 , '[^@][A-Z]+',1,level) Show from dat
    connect by level  <= regexp_count(col1, '@');
    Output current: -.
    SHOW
    -----------------------
    AAA
    SSS
    DDD
    RRR
    ZZA
    TTT
    RRR
    ZZA
    XXX
    DDD
    RRR
    
    SHOW
    -----------------------
    ZZA
    TTT
    RRR
    ZZA
    . . .
    . . .
    1st row comes very well, but the next line data copy. And the number of total records = 30. I tried with some, but not worked.
    Expected results: -.
    SHOW
    -----------------------
    AAA
    SSS
    DDD
    ZZZ 
    XXX 
    TTT 
    RRR 
    ZZA
    I need some changes on my request and I am not able to see that. So anyone can add to that or can also provide a different solution also.

    Thank you!
    Ashutosh

    Thanks for providing the loan to the use of query. :)

    Here's a solution :-(tested on 10 g, do not have 11 g at hand)

    For 11g, just use regexp_count instead of functions of the length.

    With dat As
    (
      select '@AAA @SSS @DDD' col from dual union all
      select '@ZZZ @XXX @TTT @RRR @ZZA' col1 from dual
    )
    Select regexp_substr( col, '[^@][A-Z]+',1,level) Show from dat
    connect by nocycle level  <= length(col) - length(translate(col, 'A@', 'A'))
           and col = prior col
           and prior sys_guid() is not null;
    
    SHOW
    ------------------------
    AAA
    SSS
    DDD
    ZZZ
    XXX
    TTT
    RRR
    ZZA                      
    
     8 rows selected
    
  • SQL query - help reqd

    Hello

    Here is the table, insert and script object...
    create table emp
     (eno number,
      ename varchar2(10));
      
      insert into emp values (1,'abc');
      commit;
      
      
      create or replace type emp_t as object (eno_t number,ename_t varchar2(10));
      
      
      select v.* from
          (select emp_t(eno,ename) t_emp from emp) v
     
     select v.t_emp.eno_t from  
         (select emp_t(eno,ename) t_emp from emp) v
         
    It works so far, but I want as below as well...
     
         
      with t as ( select emp_t(eno,ename) t_emp from emp) 
         select t.t_emp.eno_t from t;
    pls help.

    Published by: Khaled Aradhye, December 12, 2010 22:39
    with t as ( select emp_t(eno,ename) as t_emp from emp)
    select x.t_emp.eno_t from t x
    /
    

    You must explicitly present a table for this alias.

  • SQL Query Help: Average amounts!

    Hi people!
    I was in charge of is a program that queries the Oracle DB. And I'm not used to SQL much. Maybe my question is very basic. Please bear with me.
    I've simplified the structure of the table for that matter.
    The Control Panel has 4 fields:
    1 Order_id
    2 Trial_No.
    3 Response_Time.
    4 Order_Time

    Now the first two fields (Order_id, Trial_number) form the primary key (it's a composite key). My goal is to find the average response time for each command ID in the last 10 minutes. Order_Time is the time at which the order was placed. An order may take more than one path to complete the order. Order response time is the sum of all the response time of trail.

    A base like this question

    Select AVG (Response_Time) in the order where Order_time < sysdate 10/1440.

    gives me the average response time but here is the catch: it produces the average no matter how testing was in the order. Response time of the order is the SUM of all the response Trail time had it.

    Consider the following data in the table

    Order_id Trial_No Resopnse_Time (ms)
    1-1-1000
    2-1-1000
    2 2 1000
    2 3 1000

    If I just use "select AVG (RESPONSE_Time), it will result in 1000ms. (4000/4).
    But in reality, order 2 had taken 3000ms to complete (3 trials). And Avg response time should be (1000 + 3000) / 2 = 2000ms. can see you?

    In other words, how can I calcluater the average of the amounts on the lines, instead of the average of all the lines. I went through a few docs on article "group by" and "subqueries", but can't seem to find the answer.

    Thank you very much for reading this long post. Looking forward to some guideance.

    K

    Maybe it's

    with t
    as
    (select 1 order_id, 1000 resp_time from dual union all
     select 2, 1000 from dual union all
     select 2, 1000 from dual union all
     select 2, 1000 from dual
    )
    select avg(sum(resp_time)) from t
    group by order_id;
    

    Concerning

    REDA

  • Calculation of SQL query help

    Hello
    I have a query that calculates the values of and assigns the alias names. If I want to make calculations more in the same query by using already calculated values, how can I make it happen?

    For example,.
    If I have a table with columns A, B, C, D and there a 1,2,3,4 line

    TABLE

    A B C D

    1 2 3 4

    The query looks like this

    Select A, Sum_all (A + B + C + D), Sum_three (A + B) of the table;

    Now in my select statement so I need a higher value say (C + D), how can I achieve this with the already calculated values (Sum_all and Sum_three)?

    Thanks in advance.

    Hello

    If you want to say

    sum_all - sum_three
    

    Instead of

    c + d
    

    ? Personally, I find the second way easier and clearer, and they are not necessarily the same if columns can be NULL, but I think I understand your question.

    Assigned in the SELECT clause alias cannot be referenced in the same SELECT clause.
    You can get around that by assigning aliases in a subquery, then their ranking in a query great, like this:

    WITH  sub_q  AS
    (
        Select A, (A+B+C+D) Sum_all, (A+B) Sum_three from table_x
    )
    SELECT  sub_q.*
    ,       sum_all - sum_three  AS sum_cd
    FROM    sub_q;
    
  • SQL query help in oracle database 11g

    I have test table name these data and column as

    Select t.user_id, t.user_date, t.amount

    t-test

    ----------------------------------------------------------

    User_id date amount

    10 1/1 / 2011 10

    20/1 2/11/2011

    11 1/3 / 2011 30

    40/2 10/2/2011

    You want to display data in this format...

    User_id date amount here is the logic how to get the amount

    1                  10/1/2011      10                      10

    11 1/2 / 2011 30 10 + 20

    11 1/3 / 2011 60 (10 + 20) + 30

    2                   10/2/2011      40                         40

    SELECT t.user_id, t.user_date, t.amount
    , SUM (t.amount) OVER (PARTITION BY t.user_id
                               ORDER BY t.user_date
                             ) AS the amount
    Of t-test
    ORDER BY t.user_id
    , t.user_date
    ;
  • The SQL query help

    with t as

    (

    Select to_date (January 1, 2014 "," dd-mon-yyyy ') col1, 'a' col2, col3 1.04 by union double all the

    Select to_date (5 January 2014 ',' mon-dd-yyyy "") col1, 'a' col2, col3 1.02 Union double all the

    Select to_date (8 January 2014 ',' mon-dd-yyyy "") col1, col2, col3 1.01 Union ' b' double all the

    Select to_date (January 10, 2014 "," dd-mon-yyyy "") col1, col2, col3 1.05 Union ' b' double all the

    Select to_date (January 10, 2014 "," dd-mon-yyyy "") col1, col2, col3 2.00 Union ' b' double all the

    Select to_date (January 10, 2014 "," dd-mon-yyyy "") col1, col2 'c', col3 3.05 Union double all the

    Select to_date (January 14, 2014 "," dd-mon-yyyy "") col1, col2, col3 6.00 Union ' b' double all the

    Select to_date (15 January 2014 "," dd-mon-yyyy "") col1, col2, col3 6.10 Union ' b' double all the

    Select to_date (15 January 2014 "," dd-mon-yyyy ') col1, col2, col3 6.05 of the double ' c'

    )

    Select t.*

    t;

    OUTPUT:

    COL1 COL2 COL3

    01/01/2014 by 1.04

    05/01/2014 a 1.02

    08/01/2014 b 1.01

    10/01/2014 b 1.05

    10/01/2014 b 2

    10/01/2014 c 3.05

    14/01/2014 b 6

    15/01/2014 b 6.1

    15/01/2014 c 6.5

    Required power must be:

    COL1 COL2 COL3

    01/01/2014 by 1.04

    05/01/2014 a 1.02

    08/01/2014 b 1.01

    10/01/2014 b 1.01

    10/01/2014 b 1.01

    10/01/2014 c 1.01

    14/01/2014 b 6

    15/01/2014 b 6

    15/01/2014 c 6

    If we found the dates duplicate, we need update COL3 value with the previous value of max (date) value COL3.

    Please save my life!

    Hello

    Try this:

    WITH
    t AS
    (SELECT to_date (January 1, 2014 "," dd-mon-yyyy ') col1, 'a' col2, 1.04 col3 FROM dual)
    UNION ALL
    SELECT to_date (5 January 2014 ',' mon-dd-yyyy "") col1, 'a' col2, 1.02 col3 FROM dual
    UNION ALL
    SELECT to_date (8 January 2014 ',' mon-dd-yyyy "") col1, col2 'b', 1.01 col3 FROM dual
    UNION ALL
    SELECT to_date (January 10, 2014 ',' mon-dd-yyyy "") col1, col2 'b', 1.05 col3 FROM dual
    UNION ALL
    SELECT to_date (January 10, 2014 ',' mon-dd-yyyy "") col1, col2 'b', 2,00 col3 FROM dual
    UNION ALL
    SELECT to_date (January 10, 2014 ',' mon-dd-yyyy "") col1, col2 'c', 3.05 col3 FROM dual
    UNION ALL
    SELECT to_date (January 14, 2014 ',' mon-dd-yyyy "") col1, col2 'b', 6.00 col3 FROM dual
    UNION ALL
    SELECT to_date (15 January 2014 ',' mon-dd-yyyy "") col1, col2 'b', 6,10 col3 FROM dual
    UNION ALL
    SELECT to_date (15 January 2014 ',' mon-dd-yyyy "") col1, col2 'c', 6.05 col3 FROM dual
    )
    a, as
    (
    SELECT
    col1
    col2
    col3
    count (col1) on count_col1 (col1 partition)

    Of
    t
    )

    Select
    a.Col1
    a.col2
    a.col3
    case when a.count_col1 = 1
    then a.col3
    Max (t.col3) else keep (dense_rank of the first order by t.col1 desc)


    end                                                                         new_col3

    Of
    one
    left join (t.col1 t<>

    Group
    a.Col1
    a.col2
    a.col3
    a.count_col1
     
    order by
    a.Col1

    ;

    COL1 COL2 COL3 NEW_COL3
    --------- ---- ---- --------
    1 January 14 a 1.04 1.04
    5 January 14 a 1.02 1.02
    January 8, 14 b 1.01 1.01
    January 10, 14 b 1.05 1.01
    January 10, 14 2 b 1.01
    10 January 14 c 3.05 1.01
    January 14, 14 b 6 of 6
    15 January 14 b 6.1 6
    15 January 14 c 6.05 6

    9 selected lines

    Kind regards

    Peter

Maybe you are looking for