Calculation of the nth value highest in a group

Hi all

I was following the problem to be solved. I have a table with the following structure:
Table emp
EmpID number
NAme varchar2
DeptID number
Salary number
Now, to find the maximum salary in each Department, I can use:
select DeptId,max(salary) from emp group by DeptId;
Now to find the second salary max, I can do below:
select DeptId,max(salary) from emp
where (DeptId,salary) not in (select DeptId,max(salary) salary from emp group by DeptId) group by DeptId;
But suppose I need to find the 5th or 6th salary max then this method will be heavy.
I need a SQL query in which I can dynamically pass the parameter to calculate.

Kindly help.

Calculation of the nth value highest in a group

In recent db versions nth_value seems to be the game.

Tags: Database

Similar Questions

  • calculation of the average value of the sorted data and polar route drawing

    Hello

    I did a VI that calculates the average value of the wind rotor/speed-ratio in the sections of 30 degrees (wind direction). He also called the polar plot of calculated data. Everything works, but I would like to make more detailed calculations and drawings, by increasing the resolution to 1 degree, or...

    Problem is VI, I did, is not easy on a large scale. At the moment, I have 12 parallel structures of switch-box to calculate the average value and build the array function to collect data calculated for Polar plot to draw the image in real time. I know it's probably the worst way to do it, but since I have done a few things with LV, it was the only way I managed to do what I wanted.

    Now, if I continue in same way to reach my goal, I have to create 360 Parallels switch-case structures... that are crazy.

    Something like the calculation of the average of the table or matrix (zero/empty values should not be calculated on average) inside the loop or similar way would probably be the best solution.

    So, polar plot drawing is not a problem, but creating a reasonalbe average metering system is. Any ideas?

    I would also like to rotare northward to the top (0 deg), and degrees of increase in a clockwise direction on polar ground dial plate (as on the compass).

    VI on the attachment. (simplified version of the complete system)

    I have signals:

    -Wind speed

    -wind direction

    -Rotor speed

    I want to:

    -calculate the average value of the speed of the wind / rotor - ratio in sections (5 degrees, 1 degree)

    -Draw a polar path of the wind rotor/speed-ratio of averages in propotion of wind direction

    I'm using LabView 2009

    Thank you very much.

    It is closer to what you're looking for?

  • exclusion of a calculation of the average value

    Hello people from Adobe,.

    I am trying to exclude the "NC" 10 value are used to fill an average value (average rating of text box) on a form, I created.  I used the script from another topic on the exact topic thread, but I modified to fit my needs.  I think that it is a quick fix very.  For some reason, I don't get the average of all 10 drop boxes in my text box.  Can someone check this script and see what I might be missing.  The text box displays '0' because of this line event.value = 0;

    What Miss me?

    Here is the code:

    Average values of non - NA;

    var aFieldNames = new Array ("ListeDéroulante1", "Dropdown2", "Dropdown3", "Dropdown4", "Dropdown5", "Dropdown6", "Drop-down list 7", "Dropdown8", "Dropdown9", "Dropdown10");

    counter to the values non - NA;

    var nCount = 0;

    variable for values of sum of non - NA;

    nSum var = 0;

    default value of result if no average not calculated;

    Event.Value = 0;

    Table of process of domain names;

    for (i = 0; i < aFieldNames.length; i ++) {}

    If (this.getField(aFieldNames[i]).valueAsString! = "NA") {}

    field doesn't have a value of "NA";

    nCount ++; incrementing counter

    nSum += Number (this.getField(aFieldNames[i]).value). Add the value of the sum

    } / / end of value not of NA;

    } / / end of treatment a loop field;

    calculate the average;

    If (nCount! = 0) {}

    divisor of zero, so we can calculate the average;

    Event.Value = nSum / nCount;

    }

    I noticed that there 'Dropdown 7' as opposed to 'Dropdown7', which is what the field other names are similar. Could this be it?

  • return the same value defined in a group.

    All,
    I have the below requirement,

    I had a table (deptid, deptname, address, city, zip, State, other columns) I want to write a query to determine any errors (records with different values) because I expect all records grouped by(deptid, deptname, address,city,zip, state) to have the same deptid (Please note that deptid is not unique).

    or a specific deptid should have only one record based on the groupping (deptid, deptname, address, city, State, zip).

    any suggestion? Thank you.

    One way to check is to check for a Unique Deptid deptname combination, address, city, zip, State;

    In this way, you will get all the combination deptname, address, city, zip, Associate State with more than 1 dept_id. However, if one of these have different value, then you are bound to get several lines and thus false positives.

    select deptname, address,city,zip, state, count(distinct dept_id)
      from dept_table
    group by deptname, address,city,zip, state
    having count(distinct dept_id) > 1
    

    How about you, you give us the structure of the Table and a few examples of data as well as the expected release of him. It will help us giving you the good request that fits your needs.

  • I'm trying to access the selected value of a group of radiobutton Error #1006

    I'm trying to access the selected value of a radiobutton group, but when I do it like this:

    Debug.Text = radiogroup1.selectedValue (m:System.NET.SocketAddress.ToString ());

    I get an error like actionscript:

    TypeError: Error #1006: value is not a function.
    hand / debug1 () [Z:\mysite.co.uk\Staff\src\main.mxml:47]
    hand / ___main_Button2_click ([Z:\mysite.co.uk\Staff\src\main.mxml:372])

    No help from friendly people? ;-)

    Debug.Text = radiogroup1.selectedValue.toString ();

  • To find the nth higher/lower value in a table

    Hi all

    I have a question

    in the table a1(empid,sal) are the 2 columns

    EmpID sal

    100-5000

    105 7500

    110 3500

    087 11136

    14336 95

    I didn't want to write the query that will give me the 3rd highest salary. or generalized to find the nth more eleve/nieme the lowest value of the table.

    Note:-Rownum and Rowid must not be used

    Thank you

    Hello

    You can use a generic result of queries to find nth highest salary...

    Just put the value of N in the query, you will get the answer...

    Query:

    SELECT E1. Salary OF EMP E1 WHERE (N) = (SELECT COUNT (DISTINCT E2. Salary) OF EMP E2 WHERE E2. Salary > = E1. Salary);

    hope you got the answer...

    Check if it really helps...

    thanxx...

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

  • calculation between two columns that depends on the minimum value

    I have the table with the following values, how can auto calculation based to a minimum value to assign each line then?

    Seq 1 reference ID price price of the difference % of difference
    131500
    233500
    334000


    calculation logic

    SCREEN.png


    the result should be...

    Seq 1 reference ID price price of the difference % of difference
    13150000
    23350020001.333
    33400025001,666

    Dev environment

    service of Cloud Computing by oracle

    PRODUCT VERSION STATUS
    NLSRTL11.2.0.3.0Production
    Oracle Database 11g Enterprise Edition11.2.0.3.064 bit Production
    PL/SQL11.2.0.3.0Production
    AMT for Linux:11.2.0.3.0Production

    with

    a_table as

    (select 1 seq_1, 3 ref_id, 1500 Union double all the)

    Select 2,3,3500 from all the double union

    Select double 3,3,4000

    )

    Select seq_1, ref_id, price,

    price - min (price) on differece_price (partition by ref_id by seq_1 order),

    price / min (price) over (partition by seq_1 order ref_id) - 1 difference_percent

    of a_table

    SEQ_1 REF_ID PRICE DIFFERECE_PRICE DIFFERENCE_PERCENT
    1 3 1500 0 0
    2 3 3500 2000 1.33333333333333333333333333333333333333
    3 3 4000 2500 1.66666666666666666666666666666666666667

    Concerning

    Etbin

  • Table expanding: calculation of the difference in the value of the previous row

    Hello

    I'm a complete newbie to LiveCycle Designer (ES2) and created under the document with a table in full expansion.

    Shared files - Acrobat.com

    My goal is to make the friendly form by adding a calculation in the field "Twist" that calculates the difference of the current line to the previous row.

    I have no idea about Java Script, I'm afraid and also do not know if this is possible at all, so it's probably a bad idea after all.

    The reason for setting up as a table expansion is so that the form can be used for all 32 instances form would be used for (they all need different number of rows in total).

    The calculation will start in row 2 (Checklist.Table1), take the value of the entry in row 2 torsion and calculate the difference in the value of the twist entry in row 1.

    Then, in row 3, it calculates the difference between the value of the twist in the row 3 tier 2, etc.

    So subtract the value of the field in then with the value line onlinen-1 or something in that sense.

    Is there anyone at all who could help me with that at all?

    I would be very grateful for any advice you could give me.

    Thank you very much!

    Mathilda

    Hi Matilda,.

    Try this code;

    if (Row1.index > 0 && !Row1.resolveNode("NumericField4[0]").isNull)
    
     var prevTwist = Table1.resolveNode("Row1[" + (Row1.index - 1) + "].NumericField4[0]").rawValue
    
     var result = Math.abs(3000 / (Row1.resolveNode("NumericField4[0]").rawValue - prevTwist));
    
     this.rawValue = result;
    
     this.rawValue = "";
    

    Concerning

    Bruce

  • Recommended to refer to the calculation of the values for parameters of Linux (kernels, etc.) before installing Oracle

    Hi all

    Hope you are fine...

    Please, I need to install Oracle... WEU it will be (DB Oracle 10 g - 11 g or E-Business suite 12.1 - 12: 2), how can I specify the parameters of Linux values especially grain before proceed with the installation?

    Is there a reference that I can follow which help me to specify the correct values for parameters/kernels Linux?


    Thanks in advance.


    Kind regards

    Mohammed

    Mohammed... wrote:

    Thank you, Hussein...

    If I need to install 12.2 but "RAC ASM", is there another note for the purposes of operating systems?

    Kind regards

    Mohammed

    Mohammed,

    You must follow the same doc, just make sure that you install first - CRS refer to the Documentation online database Oracle 11 g Release 2 (11.2) for more details.

    Thank you

    Hussein

  • Calculation of the value of ready for a custom time period

    Hello

    article (*) and some other sites describe how to calculate the percentage value of the ready time for the periods of time given (in real time, (day, week, month and year).

    States KB:

    There is a shortcut, you can use the following formulas for the intervals to update chart by default to get the loan % CPU:

    • Real-time: value of summons processor / 200
    • Last day: value of summons UC / 3000
    • Last week: value of summons UC / 18000
    • Last month: value of summons UC / 72000
    • Last year: value of UC summons / 864000

    But it is also possible to obtain the divisor for a custom time period?

    for example if I choose the period of 14.10.2013 - 14.10.2013 08:00 - 10.00. The period is 2 hours. But what is the divisor and how to calculate it for other periods?

    Any help would be greatly appreciated,

    Chris

    (*) VMware KB: conversion between the summation of values CPU and CPU ready %

    It depends on what adjustments to custom value data.

    for example. If the custom vale was spent time maintaining real so in your example it is 2 hours and then he gets back in the daily data, it would be in 5 min data points.

    same goes if my custom value went for 2 days, it will be so in the past set of data of the week etc. It does not display some of them in every week and then some in every day.

    BTW, the numbers you cited have an extra 0 on the end of each

    See you soon

  • How to create a validation script that sets my value to a minimum of 1 (even if the calculation of the value is less than 1)?

    I use a notation simplified field to calculate the value

    (kg1 * 1.5) - diaporama1

    I want my value to only a minimum of 1 at any time, even if the actual value itself is less than 1

    Thank you!

    Add this code in the custom validation script:

    If (event.value<1) event.value="">

  • calculated formula database field trying to insert the null value

    Hello

    I have 3 elements of database text amount, rate and result.
    I enter both values (quantity and frequency), and the property using formula 'result' Gets the computed value.
    On the screen all the values are displayed correctly but when trying to save my information text field 'result' tent to insert the null value.

    Any suggestions?

    Forms 11g

    Thanks in advance

    794018 wrote:
    Hello

    I have 3 elements of database text amount, rate and result.
    I enter both values (quantity and frequency), and the property using formula 'result' Gets the computed value.
    On the screen all the values are displayed correctly but when trying to save my information text field 'result' tent to insert the null value.

    Point formula should not be issue of database. And insert the value in the result column in the database is not good idea.
    Then, if you want to save it. create the database result item and write trigger (When-validate-point) on amount and filed rates and the trigger code, write

    :result:=:amount*:rate;
    

    To display the result on the form, create the database no item and change the formula property and write the formula.

    Hope this will help you

    If someone useful or appropriate, please mark accordingly.

  • Fill a field value based on the calculation of the other fields

    I have two fields of n serial and serial no two are of lov after the user selects the two values that the following amount of field has to be filled automatically

    If serialno is 100 and the 200 serial No.

    Then in quantity is 200-100 = 100

    This should come in the quantity field

    If the quantity is an attribute in your view object, you can use the following expression groovy to calculate the difference:

     - 
    

    and remember not to update the quantity field series and on the basis of no field

    Jean Lou

Maybe you are looking for

  • Portege Z930 Windows 8.1 Overview - 3G no longer works

    Hello After you have installed the Preview of Windos 8.1, the 3G Modem is no longer in effect.Anyone have the same problem and know a solution? I did update the Firmeware (long before 8.1)I tried to download the latest version of the driver, but does

  • A satellite stops and restarts

    Each step and then my computer turns off and then restarts, then will turn off back again and so onThe only option is to use the product recovery disc which solves the problem. Then a month or two later, it will never happen. It s extremely frustrati

  • DEClass TB-26xx

    Any statement of declassification exists for the following: to-2635 & TB-2627?

  • I'm unable to show all of the icons on my desktop.

    I'm unable to show all of the icons on my desktop.  The taskbar appears, and I can access my applications from the start menu, but no icon on the desktop application. I use Windows XP, SP3.

  • HP Officejet Pro 8600: Vertical black line scanned single pages and copied

    The machine is about a year and treats a low volume (less than 100pgs/mth on average between pages sent from the computer, copying and scanning). Pages sent from the computer continue to print well, but for the last scan of the month or copy function