query to retrieve the second highest salary managers

Select * from employee where salary = (select max (salary) in e employees where employee_id = e.manager_id and salary < (select max (salary) in employees))
/

It does not run... can someone suggest a new

987184 wrote:
our teacher was asked to write without y analytical function.dats.

1 list of Manager IDs:

select  manager_id
  from  hr.employees
  where manager_id is not null
/

2 manager info:

select  employee_id,
        first_name,
        last_name,
        salary
  from  hr.employees
  where employee_id in (
                        select  manager_id
                          from  hr.employees
                          where manager_id is not null
                       )
/

3. Manager second highest salary

with managers as (
                  select  employee_id,
                          first_name,
                          last_name,
                          salary
                    from  hr.employees
                    where employee_id in (
                                          select  manager_id
                                            from  hr.employees
                                            where manager_id is not null
                                         )
                 )
select  m1.employee_id,
        m1.first_name,
        m1.last_name,
        max(m1.salary) second_highest_salary
  from  managers m1,
        managers m2
  where m1.salary < m2.salary
  group by m1.employee_id,
           m1.first_name,
           m1.last_name
  having count(distinct m2.salary) = 1
/

And execution:

SQL> select  employee_id,
  2          first_name,
  3          last_name,
  4          salary
  5    from  hr.employees
  6    where employee_id in (
  7                          select  manager_id
  8                            from  hr.employees
  9                            where manager_id is not null
 10                         )
 11    order by salary desc
 12  /

EMPLOYEE_ID FIRST_NAME           LAST_NAME                     SALARY
----------- -------------------- ------------------------- ----------
        100 Steven               King                           24000
        102 Lex                  De Haan                        17000
        101 Neena                Kochhar                        17000
        145 John                 Russell                        14000
        146 Karen                Partners                       13500
        201 Michael              Hartstein                      13000
        108 Nancy                Greenberg                      12008
        205 Shelley              Higgins                        12008
        147 Alberto              Errazuriz                      12000
        114 Den                  Raphaely                       11000
        148 Gerald               Cambrault                      11000

EMPLOYEE_ID FIRST_NAME           LAST_NAME                     SALARY
----------- -------------------- ------------------------- ----------
        149 Eleni                Zlotkey                        10500
        103 Alexander            Hunold                          9000
        121 Adam                 Fripp                           8200
        120 Matthew              Weiss                           8000
        122 Payam                Kaufling                        7900
        123 Shanta               Vollman                         6500
        124 Kevin                Mourgos                         5800

18 rows selected.

SQL> with managers as (
  2                    select  employee_id,
  3                            first_name,
  4                            last_name,
  5                            salary
  6                      from  hr.employees
  7                      where employee_id in (
  8                                            select  manager_id
  9                                              from  hr.employees
 10                                              where manager_id is not null
 11                                           )
 12                   )
 13  select  m1.employee_id,
 14          m1.first_name,
 15          m1.last_name,
 16          max(m1.salary) second_highest_salary
 17    from  managers m1,
 18          managers m2
 19    where m1.salary < m2.salary
 20    group by m1.employee_id,
 21             m1.first_name,
 22             m1.last_name
 23    having count(distinct m2.salary) = 1
 24  /

EMPLOYEE_ID FIRST_NAME           LAST_NAME                 SECOND_HIGHEST_SALARY
----------- -------------------- ------------------------- ---------------------
        101 Neena                Kochhar                                   17000
        102 Lex                  De Haan                                   17000

SQL> 

As you can see, most high salary 24000 is won by the King. Second highest salary is 17000 and is won by two managers: Kochhar and De Haan.

SY.

Tags: Database

Similar Questions

  • Query to find the nth highest salary...

    Hi guys,.

    I can't understand this query that we got a Web site. It is used to find the nth highest salary of the employee... Can someone explain to me please each and every part of it...

    The Charly is:

    Select distinct (a.salary)
    EMP a
    where 1 = (select count (distinct (salary))
    b emp
    where a.salary < = b.salary)

    The "BOLD" part which is the return value... is my main question about this query...

    Help, please

    It is called a correlated sub query. The inner query is executed for each iteration of the outer query. For example, if the table emp has 14 rows and then for each salary 14 the inner query will run.
    The part

    select count(distinct(sal))
    from emp b
    where a.sal<=b.sal
    

    actually counts the number of treatments that are less than or equal to the treatment of the outer query.
    Clearly for the King, who gets the highest salary is greater than or equal to 1 (himself). It returns the highest salary.

    SQL> select *
      2  from emp a
      3  where 1 >= (select count(distinct(sal))
      4  from emp b
      5  where a.sal<=b.sal)
      6  order by sal desc;
    
         EMPNO ENAME                               JOB              MGR HIREDATE         SAL       COMM     DEPTNO
    ---------- ----------------------------------- --------- ---------- --------- ---------- ---------- ----------
          7839 KING                                PRESIDENT            17-NOV-81       5000                    10
    

    Salary of the first two.

    SQL> ed
    Wrote file afiedt.buf
    
      1  select *
      2  from emp a
      3  where 2 >= (select count(distinct(sal))
      4  from emp b
      5  where a.sal<=b.sal)
      6* order by sal desc
    SQL> /
    
         EMPNO ENAME                               JOB              MGR HIREDATE         SAL       COMM     DEPTNO
    ---------- ----------------------------------- --------- ---------- --------- ---------- ---------- ----------
          7839 KING                                PRESIDENT            17-NOV-81       5000                    10
          7902 FORD                                ANALYST         7566 03-DEC-81       3000                    20
          7788 SCOTT                               ANALYST         7566 09-DEC-82       3000                    20
    
  • Need to retrieve the records whose salary is higher than the max of avg salar

    I wrote a query to retrieve the data whose salary exceeds the maximum salary of the avg of all departments
    SELECT * FROM EMP WHERE SAL>
    (SELECT MAX(AVG (SAL)) FROM EMP
    GROUP BY DEPTNO)
    I would like to write that this query using the associates.

    I tried
    SELECT EMPNO,ENAME,SAL FROM EMP E1
    WHERE SAL > ANY( SELECT MAX(AVG(SAL)) FROM EMP E2
    WHERE E1.DEPTNO  = E2.DEPTNO)
    It gives me error that we cannot use a function nested in the collateral.

    What is the solution of the present?

    Thnks

    This?

    select * from emp a
    where exists
    (select 1 from emp b
    where a.deptno=b.deptno
    and a.sal>(select round(max(avg(sal))) from emp group by deptno));
    
  • find the nth highest salary

    Hi guys, I'll try to find the nth highest salary

    using the sub query, is there a case in which the following query may fail

    Select

    * de (

    Select

    distinct rownum rn,salary from emp_mgr order by rownum) t where t. rn = 3

    ;

    I do something like that, but he got 3 selects, so I'm a worried buit, trying to optimize it

    Select * from)

    Select rownum rn, t t.salary (salary select distinct from emp_mgr by salary desc)

    ) rs

    where rs.rn = &n;

  • Query to retrieve the level 2 supervisors

    Hi all

    I use the following query to retrieve the people which is under the supervision of a given person_id... Is it possible to modify this query (see below) in order to get two levels of employees? something like:

    Supervisor 1
    Employee 1
    Employee 2
    Employee 3
    Supervisor 2
    Employee 4
    Employee 5
    Employee 6

    The query works if I pass the person_id 1 for example supervisor that I would get used 1 2 and 3. But if I pass an id of the person who, of higher hierarchy, I will just get supervisor 1 supervisor 2... And I need all the people (just two levels down)

    Select distinct *)
    Select distinct ppf.first_name |' '|| PPF.last_name
    ppf.last_name
    fu.user_name
    fu.user_id
    ppf.person_id
    papf.person_id Manager
    pi.image_id
    initcap (hla.description) LOCATION
    pb.NAME pay_basis_name
    pax.grade_id
    haou.name
    employment_category
    ppos.date_start
    of per_assignments_x pax
    per_grade_definitions pgd
    per_people_x WP
    fnd_user fu
    per_all_people_f women's wear
    per_images pi
    hr_locations_all hla
    per_pay_bases pb
    hr_all_organization_units haou
    per_periods_of_service OPP
    where ppf.person_id = pax.person_id
    and trunc (sysdate) between papf.effective_start_date and papf.effective_end_date
    and fu.person_party_id = papf.party_id
    and pax.supervisor_id = papf.person_id
    and pi.parent_id (+) = ppf.person_id
    and pi.table_name (+) = "PER_PEOPLE_F".
    and pax.person_id = ppf.person_id
    and hla.location_id (+) = pax.location_id
    AND ppf.current_employee_flag = 'Y '.
    AND pb.pay_basis_id = pax.pay_basis_id
    AND haou.organization_id = pax.organization_id
    AND ppos.person_id = ppf.person_id
    AND pax.grade_id = pgd.grade_definition_id
    AND papf.person_id =: inPersonId
    UNION ALL
    Select ppf.first_name |' '|| PPF.last_name
    ppf.last_name
    fu.user_name
    fu.user_id
    ppf.person_id
    papf.person_id Manager
    pi.image_id
    initcap (hla.description) LOCATION
    pb.NAME pay_basis_name
    pax.grade_id
    haou.name
    employment_category
    ppos.date_start
    of per_people_x WP
    per_grade_definitions pgd
    fnd_user fu
    per_all_people_f women's wear
    HR_WORKING_PERSON_LISTS HWPL
    per_images pi
    per_assignments_x pax
    hr_locations_all hla
    per_pay_bases pb
    hr_all_organization_units haou
    per_periods_of_service OPP
    where
    trunc (sysdate) between papf.effective_start_date and papf.effective_end_date
    and fu.person_party_id = papf.party_id
    AND HWPL.owning_person_id = papf.person_id
    and hwpl.selected_person_id = ppf.person_id
    AND pi.parent_id (+) = ppf.person_id
    and ppf.current_employee_flag = 'Y '.
    and pax.person_id = ppf.person_id
    AND hla.location_id (+) = pax.location_id
    AND pb.pay_basis_id = pax.pay_basis_id
    AND haou.organization_id = pax.organization_id
    AND ppos.person_id = ppf.person_id
    AND pax.grade_id = pgd.grade_definition_id
    AND papf.person_id =: inPersonId
    ) order of last_name

    Hello Alejandro,

    You can play a little bit with one below with what you find most comfortable.

    / * multi level * /.
    Select the level
    assignment_number
    assignment_id
    Manager
    , (select full_name from per_people_x where person_id = pax.person_id)
    , sys_connect_by_path ((sélectionnez employee_number dans per_people_x où person_id = pax.person_id), '-->')
    of per_assignments_x pax
    where primary_flag = 'Y '.
    connect by prior person_id = Manager
    Start by person_id = 1523

    / * level 2 only * /.
    Select (select full_name from per_people_x where person_id = pax1.person_id)
    , (select full_name from per_people_x where person_id = pax2.person_id)
    of per_assignments_x pax1
    per_assignments_x pax2
    where pax1.supervisor_id = 1523
    and pax1.primary_flag = 'Y '.
    and pax1.person_id = pax2.supervisor_id
    and pax2.primary_flag = 'Y '.

  • Query to retrieve the latest version

    Hello
    I am trying to create a query that retrieves the latetst of a product version. There are 2 tables I have mark from, we have the product number and we got the version - a varchar2 data type (60). Are versions A, B, c... etc. So, I want to give me a line for each product with its latest version of the application. It's the query I have so far


    SELECT A0. Prod_num, A0.version
    FROM (SELECT A0B. Prod_num, A0.version
    Of tb_1 A0, tb_2 A0B
    WHERE (A0.tb_1pk = A0B.tb_2pk)) A0
    ranking by A0. Prod_num

    Returns duplicate versions and product lines.

    Thank you.

    Without seeing sample data...

    SELECT A0B. Prod_num,
    Max (a0.version)
    Of tb_1 A0, tb_2 A0B
    WHERE (A0.tb_1pk = A0B.tb_2pk)
    AO8.prod_num GROUP;

  • Query to retrieve the number of transactions in every 1 hour for the end

    Hello

    Could someone help to write a query to retrieve the number of transactions in every hour for the last month.

    Case:
    I / P

    If Timestamp1
    1 01/01/2008 00:00:01
    CAS2 01/01/2008 00:01:01
    case3 01/01/2008 01:00:01
    1 01/01/2008 01:02:01
    case4 01/01/2008 01:10:01
    Service5 01/02/2008 02:00:01
    Case6 01/02/2008 02:10:01
    case7 2008-02-01 23:00:01
    .............................

    .............................
    case... 2008-01-31 00:24:00


    O/P
    cases of to_time of time
    2008-01-01 00:00:00 01/01/2008 01:00 2
    2008-01-01 01:00:01 01/01/2008 02:00 3

    .........................
    .........................

    etc.

    Any help really appreciated

    We can do this by using analytic functions

    Here's what I did:

    create table timestamp1 (date of ts)

    Select * from timestamp1
    2008-10-30 15:41:13
    2008-10-30 15:41:05
    2008-10-30 15:40:03
    2008-10-30 14:58:26
    2008-10-30 14:29:45
    2008-10-30 13:17:48
    2008-10-30 08:29:50
    2008-10-30 06:05:51
    2008-10-30 03:41:52
    2008-10-30 02:29:54

    Select distinct to_char (ts, 'hh24') frmhrs,
    TO_CHAR (ts, 'hh24') + 1 tohrs, count (ts) OVER (order by to_number (to_char (ts, 'hh24')) RANK BEFORE (1/24))
    of timestamp1
    When trunc (ts) = trunc (sysdate) - I added this just to make sure that I get for data of today
    order of frmhrs

    FRMHRS TOHRS CNT
    1-3-02
    03-4-1
    06 7 1
    1-9-08
    13 14 1
    14 15 2
    15 16 3

    You can customizeas by your need.

  • SQL query to retrieve the same record because no. boxes

    Hi all

    I have a table of Bill XXINV.

    the columns are

    INVH_NO INVH_ITEM_CODE INVH_QTY INVH_BOXES

    1122                XXITEM1                    1                    5

    1123                XXITEM2                     2                   6

    I can do a sql query so that I can retrieve the same records repeated as number INVH_BOXES.

    Select * from XXINV

    where invh_no = 1122

    output:

    INVH_NO INVH_ITEM_CODE INVH_QTY INVH_BOXES

    1122                XXITEM1                    1                    5

    Desired output:

    INVH_NO INVH_ITEM_CODE INVH_QTY INVH_BOXES

    1122                XXITEM1                    1                    5

    1122                XXITEM1                    1                    5

    1122                XXITEM1                    1                    5

    1122                XXITEM1                    1                    5

    1122                XXITEM1                    1                    5

    or

    INVH_NO INVH_ITEM_CODE INVH_QTY INVH_BOXES

    1122 XXITEM1 1 1 of 5

    1122 XXITEM1 1 2 of 5

    1122 1 3 5 XXITEM1

    1122 XXITEM1 1 4 of 5

    1122 XXITEM1 1 5 of 5

    Please suggest. Like I need to print each line in a single page to stick them on boxes so that packaging.

    Kind regards

    Afzal.

    Just add INVH_ITEM_CODE to the connection by the clause.

    Select invh_no

    invh_item_code

    invh_qty

    to_char (, 'fm99') | 'from ' | TO_CHAR (invh_boxes, 'fm99') invh_boxes

    of xxinv

    where invh_no = 1122

    connect

    by level<=>

    and prior invh_item_code = invh_item_code

    and prior sys_guid() is not null

  • Query to retrieve the data in a sort of pivot format.

    Hello gurus of PL/SQL,.
    I need to extract the data in a format some tables using the join condition. When I extracted using simple join then it turn around individual records based on condition, but want it in a particular format only.

    When run the query-

    Select sf. JOIN_DT_KEY, sd. CLS, sd. CLS_STATUS, sf. CLS_QTY, sf. CLS_FEES,
    SF. TOTAL_AMT
    of stud_det_fact sf, sd, cd class_dim stud_tag_dim
    where sf.stud_dim_key = sd.stud_dim_key
    AND sf.alloc_cls_dim_key = d.alloc_cls_dim_key AND sd. CLS_STATUS IN
    ("BA", "BSC", "MA", "MBA", "SMC")
    AND af. JOIN_DT_KEY between 20120404 and 20120410 ORDER BY sd. CLS

    That the data is returned in the following format:

    JOIN_DT_KEY CC CLS_STATUS CLS_QTY CLS_FEES TOTAL_AMT
    1 20120404 BA 1050000 12 875 WORK
    2 20120404 BA 125 12 150000 CAPITAL
    3 20120404 BA 1050000 12 875 WORK
    4 20120404 BSC 4 10 4000 CAPITAL
    5 20120404 BSC 3 10 3000 CAPITAL
    6 20120404 MY 1050000 12 875 WORK
    7 20120404 MY 1000 12 1200000 WORK
    8 20120404 MBA 1200000 12 1000 ALGO
    9 20120404 MBA 1200000 12 1000 ALGO

    But I want to in the format after using query itself.

    JOIN_DT_KEY WORK of CLS (Qty) WORK (Amt) CAPITAL (Qty) CAPITAL (Amt) (Qty) ALGO ALGO (Amt) NATURAL (Qty) NATURAL (Amt) GrandTotal (Qty) GrandTotal (Amt)
    20120404 1750 2100000 125 150000 0 0 0 0 1875 2250000 BA
    0 0 7 7000 BSC 0 0 0
    0 7 7000
    MY 1875 2250000 0 0 0 0 0
    0 1875 2250000
    0 0 0 0 2000 2400000 MBA 0
    2400000 2000 0
    3625 total 4350000 132 157000 2000 2400000 0
    0 6907000 of 5757

    How to change the query to get the hinge of the sort of result, kindly help me. I appreciate all your help in advance.

    Thank you and I really apperciate your help.

    Note:-attached, this is the example of excel sheet.

    Published by: user555994 on April 26, 2012 03:43

    I suggest that to take advantage of the Oracle version you have.

    To start, I took your first message and created a few sample data. This is not your original tables, but it allows us to compare the results.

    drop table T;
    create table T(JOIN_DT_KEY, CLS, STATUS, QTY, FEES, AMT) as select
    TO_DATE('20120404', 'YYYYMMDD'), 'BA', 'WORKING', 875, 12, 1050000 from dual union all select
    TO_DATE('20120404', 'YYYYMMDD'), 'BA', 'CAPITAL', 125, 12, 150000 from DUAL union all select
    TO_DATE('20120404', 'YYYYMMDD'), 'BA', 'WORKING', 875, 12, 1050000 from dual union all select
    TO_DATE('20120404', 'YYYYMMDD'), 'BSC', 'CAPITAL', 4, 10, 4000 from DUAL union all select
    TO_DATE('20120404', 'YYYYMMDD'), 'BSC', 'CAPITAL', 3, 10, 3000 from dual union all select
    TO_DATE('20120404', 'YYYYMMDD'), 'MA', 'WORKING', 875, 12, 1050000 from DUAL union all select
    TO_DATE('20120404', 'YYYYMMDD'), 'MA', 'WORKING', 1000, 12, 1200000 from DUAL union all select
    TO_DATE('20120404', 'YYYYMMDD'), 'MBA', 'ALGO', 1000, 12, 1200000 from DUAL union all select
    TO_DATE('20120404', 'YYYYMMDD'), 'MBA', 'ALGO', 1000, 12, 1200000 from DUAL;
    

    To test the following against your data, is taken your SELECT in the right place or create a view and plug the display where I have the table "T".

    Now, here's how to get everything except the last line "Total":

    select *
    from (
      select JOIN_DT_KEY, CLS, STATUS, QTY, AMT from t
    )
    PIVOT(SUM(QTY) QTY, SUM(AMT) AMT
    for STATUS in('ALGO' as ALGO,'CAPITAL' as CAPITAL,'DMA' as DMA,'NATURAL' as natural,'WORKING' as WORKING));
    
    JOIN_DT_KEY        CLS ALGO_QTY ALGO_AMT CAPITAL_QTY CAPITAL_AMT DMA_QTY DMA_AMT NATURAL_QTY NATURAL_AMT WORKING_QTY WORKING_AMT
    ------------------ --- -------- -------- ----------- ----------- ------- ------- ----------- ----------- ----------- -----------
    2012/04/04 00:00   BSC                             7        7000
    2012/04/04 00:00   BA                            125      150000                                                1750     2100000
    2012/04/04 00:00   MBA     2000  2400000
    2012/04/04 00:00   MA                                                                                           1875     2250000
    

    To get the latest "Total" line, you need to do another GROUP after the pivot.

    select JOIN_DT_KEY, nvl(CLS, 'Total') cls,
    SUM(ALGO_QTY) ALGO_QTY,
    SUM(ALGO_AMT) ALGO_AMT,
    SUM(CAPITAL_QTY) CAPITAL_QTY,
    SUM(CAPITAL_AMT) CAPITAL_AMT,
    SUM(DMA_QTY) DMA_QTY,
    SUM(DMA_AMT) DMA_AMT,
    SUM(NATURAL_QTY) NATURAL_QTY,
    SUM(NATURAL_AMT) NATURAL_AMT,
    SUM(WORKING_QTY) WORKING_QTY,
    SUM(working_AMT) working_AMT
    from (
      select JOIN_DT_KEY, CLS, STATUS, QTY, AMT from t
    )
    PIVOT(SUM(QTY) QTY, SUM(AMT) AMT
    for STATUS in('ALGO' as ALGO,'CAPITAL' as CAPITAL,'DMA' as DMA,'NATURAL' as natural,'WORKING' as WORKING))
    group by grouping sets((join_dt_key, cls), join_dt_key);
    
    JOIN_DT_KEY        CLS   ALGO_QTY ALGO_AMT CAPITAL_QTY CAPITAL_AMT DMA_QTY DMA_AMT NATURAL_QTY NATURAL_AMT WORKING_QTY WORKING_AMT
    ------------------ ----- -------- -------- ----------- ----------- ------- ------- ----------- ----------- ----------- -----------
    2012/04/04 00:00   BA                              125      150000                                                1750     2100000
    2012/04/04 00:00   MA                                                                                             1875     2250000
    2012/04/04 00:00   BSC                               7        7000
    2012/04/04 00:00   MBA       2000  2400000
    2012/04/04 00:00   Total     2000  2400000         132      157000                                                3625     4350000
    

    Using "grouping sets", you group by join_dt_key and cls (which the PIVOT is already done, but you have to do it again) and then by join_dt_key to get the total line.

    Note: I suppose you want a total by date. If you want just a total general for all dates, then Group Rollup (join_dt_key, cls)).

    I hope it's accurate and useful.

  • Query to retrieve the data according to requirement

    Hi team,

    I have the table with the details below and my requirement is when I entered the correct empid then only need to display recording (it's easy) and when I entered the empid that is not listed in the table, then I need to display all the records available in this table. Please give me the query.

    EmpID ename

    7839KING
    7698BLAKE
    7782CLARK
    7566JONES
    7788SCOTT
    7902FORD
    7369SMITH

    Kind regards

    Anil

    can you please mark it as answered@user575819

  • Need of a query to retrieve the data

    Hello

    I have a table named person. It has two fields A_ID and B_ID. The table does not have a primary key
    The data in the table are the following

    A_ID B_ID_ _

    1 5
    1 10
    2 5
    2 10
    4 15
    5 20

    I need to select A_ID and B_ID data so that when you select a given allocation A_ID or B_ID is not for more choices

    For Eg: I select the first pair of 1 and 5. Then 1 and 5 is not in the selection of data from A_ID or B_ID

    The output of the query for the data in the table above should be

    Output

    A_ID B_ID_ _

    1 5
    2 10
    4 15

    Hope you understand the situation.
    Can someone help me to create a query for me

    Thanks in advance

    Published by: user599079 on August 12, 2009 21:40

    This done:

    Nope:

    SQL>  with t as (
     select 1 a_id, 5 b_id from dual union all
     select 1, 10 from dual union all
     select 2, 5 from dual union all
     select 2, 10 from dual union all
     select 4, 15 from dual union all
     select 5, 6 from dual union all
     select 6, 9 from dual
    ),
    union_data  as
    (
        select    a_id    as id
        ,    'A'    as col
        ,    row_number () over (order by a_id, b_id)    as r_num
        from   t
    union all
        select    b_id    as id
        ,    'B'    as col
        ,    row_number () over (order by a_id, b_id)    as r_num
        from    t
    )
    ,    grouped_data    as
    (
        select      id
        ,      min (col) keep (dense_rank first order by r_num)    as first_col
        ,      min (r_num)                                     as first_r_num
        ,      row_number () over ( partition by min (col) keep (dense_rank first order by r_num)
                                  order by        min (r_num)
                         )                    as out_num
        from      union_data
        group by  id
    )
    select      a.id    as a_id
    ,      b.id    as b_id
    from      grouped_data    a
    join      grouped_data    b    on    a.out_num    = b.out_num
    where      a.first_col    = 'A'
    and      b.first_col    = 'B'
    order by  a.out_num
    /
          A_ID       B_ID
    ---------- ----------
             1          5
             2         10
             4         15
    
    3 rows selected.
    

    I expect him back at the last row too much (like my request).
    In addition to change line 5 - 6 5-25 mess up completely:

          A_ID       B_ID
    ---------- ----------
             1          5
             2         10
             4         15
             6         25
    
  • Query to retrieve the average growth rate

    Hi all

    10.2.0.5 on solaris

    Table script:

    DB_SPACE_HIST
    (
    DATE TIME STAMP,
    TOTAL_SPACE NUMBER (8).
    USED_SPACE NUMBER (8).
    FREE_SPACE NUMBER (8).
    PCT_INUSE NUMBER (5.2).
    NUM_DB_FILES NUMBER (5)
    );

    Sample data:


    Timestamp: / 16/2010 12:57:52 AM
    Totak space: 678017
    Used space: 235612
    Free space: 442405
    PCT in use: 34.75
    number of files: 60


    We have the timestamp stored in the table for more than 3 years. The requirement is that we get the average growth in the database per month.

    No idea how I can do this?

    Kai

    Start by getting your data used_space by month:

    select
    trunc(timestamp,'MM') mth,
    max(used_space) used_space_end_mth
    from db_space_hist
    group by
    trunc(timestamp,'MM');
    

    Then you can use the analytic function LAG to get used_space_end_mth compared to the month previous for calculating growth:

    select
    mth,
    used_space_end_mth - lag(used_space_end_mth) over (order by mth) growth
    from (
       select
       trunc(timestamp,'MM') mth,
       max(used_space) used_space_end_mth
       from db_space_hist
       group by
       trunc(timestamp,'MM')
    )
    order by
    mth;
    

    And finally, you can simply select the average growth:

    select
    avg(growth) avg_growth
    from (
       select
       mth,
       used_space_end_mth - lag(used_space_end_mth) over (order by mth) growth
       from (
          select
          trunc(timestamp,'MM') mth,
          max(used_space) used_space_end_mth
          from db_space_hist
          group by
          trunc(timestamp,'MM')
       )
    );
    
  • 2nd and 5th highest salary

    Hi, I'm kind of new to sql. I have a table with the name of the employee and salary.
    I would like to find the name of the 2nd higher salary? also I want to get the name of the highest salary 5th
    Here's my data

    INSERT INTO er4 VALUES ('John', 1000, 123);
    INSERT INTO er4 VALUES ('john2', 3000, 234);
    INSERT INTO er4 VALUES ('Jean3', 1000, 223);
    INSERT INTO er4 VALUES ('john4', 4000, 123);
    INSERT INTO er4 VALUES ('john5', 8000, 023);
    INSERT INTO er4 VALUES ('john6', 9000, 023);
    INSERT INTO er4 VALUES ('john7', 8000, 723);

    You can see in the data, there are two employees with the second highest salary. I would like to get one or the other, but I want my query to return a single line

    can someone help me write a query to get the name and salary of the 2nd higher and the name and salary of 5th higher

    Hello

    Part of learning about the functions is to learn what they are good for, and what they are not good for.
    As Justin said, MAX is not good for this task.
    If you were interested in just the 1st highest salary, you may be able to use MAX like this:

    SELECT  *
    FROM    er4
    WHERE   salary  =      (
                   SELECT     MAX (salary)
                   FROM     er4
                   )
    AND     ROWNUM     = 1     -- in case of a tie, pick one row arbitrarily
    ;
    

    But even if the goal was to find the highest salary, I would use ROW_NUMBER, as Justin did.
    Why? The following scenario happens all the time in real life. The client for which you wrote this comes back in 3 months and said: 'this query you wrote for the highest salary works very well! We found it so useful, we would like to do something similar to find the 2nd highest salary, either the 5th highest, or the top 5 or the next 5. "ROW_NUMBER is flexible enough to do all these things. When you have to solve a problem, you'll save time (long-term) If you can predict, or just guessing, what you need to solve later and write something for the problem today that can easily be adapted for potential future similar problems problems.

  • SQL query to represent the data in the graph bar

    Hello

    JDev 11.1.1.5.0

    We must create a dashboard to track the translation. We have created the ADF table with buttons 'Create', 'Update' and 'Delete' to manipulate the table.

    Our DB table structure is

    File_id (PK), File_Name, ToSpanish - ARE (YES/NO), beginning of the ToSpanish, ToChina(YES/No), ToChina-Date... etc.

    Once the translated file required language then user must update the DB table using the button "Update".

    So far, we have implemented the requirement above.

    We need represent the status of the translation in the graph bar with the language as X access and file count get access Ex: Spanish-100 China - 200 files files

    Please suggest the sql query to retrieve the necessary info from the DB table that can be represented in the graph bar. Also, it would be great, if you can provide a pointers to create a bar chart.

    Thanks in advance,

    MSR.

    If you set your major increment and minor than 1, then you won't not show decimal points.  You can try setting these 10 or 100 to reach your goal.

    Subtype = "BAR_VERT_CLUST" >

                 

  • Error when retrieving the value of the pair name / value

    Hi gentlemen,

    I am facing a problem while retrieving the value of the pair name / value in the incoming XML file. I am using an Xpath query to retrieve the value but Jdeveloper gives me
    an invalid expression error.

    Please find below the new xml and xquery.

    < ebo:QueryItemListEBM xmlns:ebo = "http://xmlns.oracle.com/EnterpriseObjects/Core/EBO/Item/V2" >
    < ebo:DataArea xmlns:aia = "http://www.oracle.com/XSL/Transform/java/oracle.apps.aia.core.xpath.AIAFunctions" xmlns:coreitem = "http://xmlns.oracle.com/EnterpriseObjects/Core/EBO/Item/V2" >
    < ebo:Query >
    < ebo:QueryCode > QUERY_ITEMS < / ebo:QueryCode >
    < corecom:QueryCriteria xmlns:corecom = "http://xmlns.oracle.com/EnterpriseObjects/Core/Common/V2" >
    < corecom:QueryExpression >
    < corecom:ValueExpression >
    < corecom:ElementPath > ItemEBO/Item/InventoryLocation/BusinessComponentID < / corecom:ElementPath >
    < corecom: Value > 82 < / corecom: Value >
    < / corecom:ValueExpression >
    < corecom:ValueExpression >
    < corecom:ElementPath > ItemEBO/Item/ItemIdentification/ContextID@OPERATING_UNIT_ID < / corecom:ElementPath >
    < corecom: Value / >
    < / corecom:ValueExpression >
    < corecom:ValueExpression >
    < corecom:ElementPath > ItemEBO/Identification/BusinessComponentID < / corecom:ElementPath >
    < corecom: Value > ba55de86-d8b4-4db1-b599-5aef8424ac3b < / corecom: Value >
    < / corecom:ValueExpression >
    < corecom:ValueExpression >
    < corecom:ElementPath > ItemEBO/Item/ItemIdentification/ContextID@STRUCTURE_NAME < / corecom:ElementPath >
    < corecom: Value > PIM_PBOM_S < / corecom: Value >
    < / corecom:ValueExpression >
    < corecom:ValueExpression >
    < corecom:ElementPath > ItemEBO/Identification/review < / corecom:ElementPath >
    < corecom: Value > 208281 < / corecom: Value >
    < / corecom:ValueExpression >
    < / corecom:QueryExpression >
    < / corecom:QueryCriteria >
    < / ebo:Query >
    < / ebo:DataArea >
    < / ebo:QueryItemListEBM >

    I need to retrieve the value 'ba55de86-d8b4-4db1-b599-5aef8424ac3b' on top of xml and xpath expression that I used is:

    bpws:.

    Please help with your valuable contributions.

    Thank you and best regards,
    Vikas marzouk

    829347 wrote:
    Hi Vlad,

    It works quite well. A big thank you to you.

    see you soon,
    Vikas

    Hi Vikas, could you marking my answer as correct then?

    Thank you
    Vlad

Maybe you are looking for