Three record with maximum values

I have followed query:
select a.deptno a, count(b.empno) b
from dept a, emp b
where b.deptno(+) = a.deptno
group by a.deptno
and I got a result:
a b
10 3
20 5
30 6
40 0
50 9
60 4
70 7
80 1
How can I get three departments with largest number of employees?
I would like to get result like this:
a b
50 9
70 7
30 6
Thanks.

If you have several departments with the same count of employees then a limitation by rownum leads to a undefined result,
e.g. when department 20 in your eample has also 6 employees, then the answer could be
a b
50 9
70 7
30 6
20 6
use the dense_rank function to get the rank of the departments and then limit to the top three,
e.g.
select *
from (
select a, b, dense_rank() over(order by b desc) b_rank
from (
select a.deptno a, count(b.empno) b
from dept a, emp b
where b.deptno(+) = a.deptno
group by a.deptno
where b_rank < 4
;

Similar Messages

  • Purchase info record with same values

    Hi,
    I want to create purchase info records with the same material vendor purchasing org. plant ,how can it be done.
    This is required because it is possible for the same vendor to deliver material for the same plant and purchasing organisation.Is there a workaround or any reason for this.
    Regards

    In SAP , this is not possible to have 2 different inforecords for the combinaiton of MaterialVendorPur orgPlantinfo record type
    could you please explain more regarding the requirement ....
    Regards
    Mani

  • Populate Parent/Child records with "Family" value

    Hi,
    I typically do the task I'm asking assistance with in Excel and it works great... the only problem is that it's VERY slow and takes a lot of work when over 1 million rows as you must split it up and yeah just painful! But what I'm trying to accomplish is
    pretty straight forward for Excel, however escapes me in T-SQL! I have an ordered number that is sorted asc. I have a flag that indicates a record is the "Parent". I want to update the family value for the "Parent" and all sequential records
    until the next "Parent" records is reached. So here is an example in Excel.
    Using the following formula in cell C2 and pasting all the way to the last record, I can easily(for smaller sets anyway) create my desired results.
    =IF(B2="Y",A2,C1)
    Number Flag Family
    1 Y 1
    2 1
    3 1
    4 1
    5 1
    6 1
    7 1
    8 1
    9 Y 9
    10 9
    11 Y 11
    12 11
    13 11
    14 Y 14
    15 14
    16 14
    17 14
    18 14
    19 14

    I have an article on this exact topic
    Fixing
    Missing Data Based on Prior Row Information
    It shows the concept of using OUTER APPLY to achieve desired functionality.
    For every expert, there is an equal and opposite expert. - Becker's Law
    My blog
    My TechNet articles
    I wish I knew how to apply this OUTER APPLY solution to an update statement so I could use. Would this be more efficient and faster than the solution provided by RyanAB? I have millions of records and NOT enough time... the fastest solution is definitely desired!!!
    I ended up using this as a solution as it was the fastest. I first did:
    SELECT a.Number, M.Number 'Family' into FamilyTable FROM Table a
    OUTER APPLY (
    SELECT TOP (1) *
    FROM Table b
    WHERE a.Number >= b.Number and b.[Flag] = 'Y'
    ORDER BY b.[Number] DESC) M
    ORDER BY 1, 2
    Then updated the original like so:
    update a set a.Family = b.Family
    FROM Table a inner join FamilyTable b
    on a.Number = b.Number
    Thanks to all!!!

  • Sorting records with property values

    Hi Friends,
    Can anybody please suggest a solution/approach for this requirement?
    Lets say,I have 100 records displayed in a non-search results page which are inter-related but they have 2 different inventory properties ex:online_inventory and store_inventory.We are sorting the products by online_inventory property and we are seeing the products displayed in the following order:
    online_instock items--->online_outofstock items--->store_instock items--->store_outofstock items.
    Now,our requirement is to show the products in following order:
    online_instock items--->store_instock items--->online_outofstock items--->store_outofstock items.
    The basic idea is to push the out of stock items at last irrespective of whether it is an online or store product?
    Please note that due to some limitations,we cannot maintain a single inventory property combining online and store inventories.
    Is it possible to apply a sort key here based on a property value or how can we achieve this requirement?
    Regards
    Shreyas

    Shreyas,
    You can do it another way :
    Create a Property say P_combain_sort and enabled for sort, add a manipulator for retrieving the value of online_instock and store_instock.
    check if online_instock  and store_instock value is 1 then add source property combain_sort and its value 1 otherwise 0 with record.
    In Property mapper map property combain_sort to P_combain_sort and apply sort in newly created property.
    Hope this will help you.
    Thanks,
    Sunil

  • How to suppress records with vzero value in a union report in OBIEE?

    Hi
    I have a Union report ,Rep1 union Rep 2,with a cloumn A in rep1 and a dummy column in place of column A in rep 2.
    I want to restrict the records where A= 0 in the union report.
    How to go abouth this.
    Thanks in advance !!

    hi,
    In report1 let the column be as it is ,why are you changing the f(x) to case statement.Did you check is data there in that column of report1.
    In the union report make tat dummy column as 0 or 1 accordingly as per your requirement.So it would retrieve all the values of column of report1.
    Check the join conditions established and play with the joins like give a full outer join and check if the data is coming including 0's if so then you can pply filter on that column saying column not equal to 0,so the remaining values gets populated.
    Cheers,
    KK
    Edited by: Kranthi.K on May 18, 2011 2:03 AM

  • How to create new record with old values

    Dear all
    i have creation page with 45 fields are there
    user will enter all the fields and save it will call to non editable mode
    if user will click on change button it will call to editable mode
    in that my requirement is user wont change among those ites(45 items)clcik on save again it will call to view mode
    in that user will change any one of the value among 45 items and click on save button i need to create one more record in database
    if user wont edit any one of the field out of 45 items i dont want to create new record in database
    how can we achieve thsi functionality
    Regards
    Sreekanth

    Hi Srikant,
    When user may edit the record, the same row may get updated in vo. Thus over writing existing row instead of creating new row.
    Approach1..
    On the page where user may edit records, instead of presenting records of existing row, present copy of existing row. So
    In AM... (Later on you can put vo logic to its proper place, i.e. vo java file)
    public void viewPageCallSetup()
    Row originalRow=vo.createRow();
    Row dummyRowForEdit=vo.createRow();
    //COPY THE ORIGINAL ROW TO DUMMY ROW. IF THERE IS DIRECT METHOD TO CREATE COPY OF EXISTING ROW, THAT MAY BE MUCH BETTER.
    for(int i=0;i<vo.getAttributeCount()-1; i++)
    dummyRowForEdit.setAttribute(i, originalRow.getAttribute(i));
    vo.insertRow(dummyRowForEdit);
    vo.setNewRowState(Row.STATUS_INITIALIZED);
    vo.setCurrentRow(dummyRowForEdit);
    call this method in contoller's processRequest..
    string oid=pagecontext.getparameter("oid");
    if(pageContext.getparameter("Purpose").equals("Edit")) //Means user clickED the edit button so we need to replicate the row
    am. viewPageCallSetup(oid); //calling this method in am
    else
    In PFR...
    if(pageContext.getparameter("Purpose").equals("Edit"))
    am.xxcostedit(oid); //calling this method in am
    in case user selects "Cancel" on edit form,we need to remove the duplicate row. So in AM write a method to remove the duplicate rowd on click of "Cancel" button on edit form.
    Rest of the logic is same for "Save" button. Just say commit.
    Abdul Wahid

  • How to develop this report/Query that selects records with MAX Value

    Hi,
    My requirment is to develop a report to show all emp whose policy is going to expire ith in the user selected date intreval...
    Emp_ID--App_Date-Policy_Strt_Date--Policy_End_Date--App_Status
    101.01.2007---BlankBlank--
    NEW   (New application for a Medical Insurance policy raised for employee)
    101.01.2007---01.02.200730.05.2007--
    COMPLETE (policy issued for emp with strt & end dates)
    115.05.2007---01.02.200730.05.2007--
    RENEWAL  (a application is raised to renew emp policy)
    115.05.2007---01.06.200731.12.2007--
    COMPLETE  (policy issued for emp with strt & end dates)
    115.12.2007---01.06.200731.12.2007--
    RENEWAL  (a application is raised to renew emp policy)
    115.12.2007---01.01.200831.05.2008--
    COMPLETE  (policy issued for emp with strt & end dates)
    115.05.2008---01.01.200831.05.2008--
    RENEWAL  (a application is raised to renew emp policy)
    115.05.2008---01.06.200831.05.2009--
    COMPLETE  (a application is raised to renew emp policy)
    So EMP 1 got valid policy untill 31.05.2009.
    When a user executes a query to show all employees whose policy expiry date intreval (01.05.2008 to 31.05.2008)
    it shouldn't show Emp 1 as he got  valid policy untill 31.05.2009  but currently it is showing Emp 1....
    How can i achieve this
    Thanks

    Hi
    Thanks for the update
    I created variable on Plc End end
    If user enter show all emps whose policy is expiring in MAY (01.05.2008---31.05.2008) it is displaying EMp 1 as well but emp 1 has a policy which expires in 2009
    Thanks

  • How to get internal table with maximum value of a field in other internal

    Let say I have an table itab consisting of:
    itab-matnr = marm-matnr.
    itab-umren = marm-umren.
    itab-ean11 = marm-ean11.
    there are many "ean11" for one "matnr"
    How could I create itab2 with only "biggest" "ean11". could you give me examlple

    Hi,
    data: mat_no type MATNR.
    Loop at ITAB1.
    IF mat_no = itab1-matnr.
    here check the  ean11 with previos ean11(Store it like matnr), and move the biggest one andf modify that row or move that in ITAB2 with a Modify statment, so that there will no second row with same material No
    ENDIF.
    MAT_NO = ITAB1-Matnr     (Moving Itab1 material no to a Local field)
    ENDLOOP.
    Endloop.

  • Bdc while uploading Itab one extra record is coming with 000000 values

    Hi
    am getting one extra record with 00000 values for begda ,endda and object id  dont know why ?
    code is like,
    TYPES : BEGIN OF tt_record,
    plvar  type   hrp1000-plvar,
    otype  type   hrp1000-otype,
    seark  type   hrp1000-objid,
    begda  type   hrp1000-begda,
    endda  type   hrp1000-endda,
    short  type   hrp1000-short,
    stext  type   hrp1000-stext,
    END of tt_record.
    DATA:gt_record TYPE TABLE OF tt_record,
         gs_record TYPE tt_record.
    include bdcrecx1.
    start-of-selection.
    CLEAR : gt_record,gs_record.
    CALL FUNCTION 'GUI_UPLOAD'
      EXPORTING
        FILENAME                      = 'C:\ABC.TXT'
      FILETYPE                      = 'ASC'
       HAS_FIELD_SEPARATOR           = 'X'
      TABLES
        DATA_TAB                      = gt_record
    EXCEPTIONS
       FILE_OPEN_ERROR               = 1
       FILE_READ_ERROR               = 2
       NO_BATCH                      = 3
       GUI_REFUSE_FILETRANSFER       = 4
       INVALID_TYPE                  = 5
       NO_AUTHORITY                  = 6
       UNKNOWN_ERROR                 = 7
       BAD_DATA_FORMAT               = 8
       HEADER_NOT_ALLOWED            = 9
       SEPARATOR_NOT_ALLOWED         = 10
       HEADER_TOO_LONG               = 11
       UNKNOWN_DP_ERROR              = 12
       ACCESS_DENIED                 = 13
       DP_OUT_OF_MEMORY              = 14
       DISK_FULL                     = 15
       DP_TIMEOUT                    = 16
       OTHERS                        = 17
    IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
             WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
    Regards
    Meeta

    Hi  Txs for your response  i have created new file and my that problem solved but i want to know when i run in forground then only it works fine and update records but when i try in background it immd turn into incorrect log and when i run in Display error mode  i got these errors in error log,
    Runtime error RAISE_EXCEPTION has occurred
    Transaction error
    Processing statistics
    3 transactions read
    0 transactions processed
    3 transactions with errors
    0 transactions deleted
    Batch input processing ended
    Any idea any one can guide me on this,
    Regards
    Meeta

  • Picking records with distinct col values

    Hi gurus,
    I have a table with
    id col1 col2 col3
    1 a b c
    2 e f g
    3 a b c
    4 a b c
    what would be the query to pick records with distinct col1,col2,col3 values.i.e records with col values 1,a,b,c and 2,e,f,g only in the above table
    Could you please help me on this?
    Best Regards
    Sridha

    SQL> with t as
      2  (
      3  select 1 COL1,'A' COL2,'B' COL3,'C' COL4 FROM DUAL UNION ALL
      4  select 2,'D' COL1,'E' COL2,'F' COL3 FROM DUAL UNION ALL
      5  select 3,'A' COL1,'B' COL2,'C' COL3 FROM DUAL UNION ALL
      6  select 4,'G' COL1,'H' COL2,'I' COL3 FROM DUAL UNION ALL
      7  select 5,'D' COL1,'E' COL2,'F' COL3 FROM DUAL
      8  )
      9  SELECT COL1, COL2, COL3, COL4
    10  FROM T
    11  /
          COL1 C C C
             1 A B C
             2 D E F
             3 A B C
             4 G H I
             5 D E F
    SQL> with t as
      2  (
      3  select 1 COL1,'A' COL2,'B' COL3,'C' COL4 FROM DUAL UNION ALL
      4  select 2,'D' COL1,'E' COL2,'F' COL3 FROM DUAL UNION ALL
      5  select 3,'A' COL1,'B' COL2,'C' COL3 FROM DUAL UNION ALL
      6  select 4,'G' COL1,'H' COL2,'I' COL3 FROM DUAL UNION ALL
      7  select 5,'D' COL1,'E' COL2,'F' COL3 FROM DUAL
      8  )
      9  SELECT COL1, COL2, COL3, COL4
    10  FROM
    11  (
    12  SELECT COL1, COL2, COL3, COL4, ROW_NUMBER () OVER (PARTITION BY COL2, COL3, COL3 ORDER BY COL1) AS RN
    13  FROM T
    14  )
    15  WHERE RN=1
    16  /
          COL1 C C C
             1 A B C
             2 D E F
             4 G H I
    SQL>

  • How to show records with zero on dashboard

    hi expert,
    if I have a dimension table product,
    a fact table
    if in the db, there are records with zero values of product A.
    if the logical column for the fact is SUM(...).
    how to show records with zero values on dashboard.
    Use the outer join?
    or other method?
    I dont wanna use outer join because it makes the PRD be so complicated

    hi,
    Pull all the columns to criteria ,it will show NULL for a product
    To get zero,on the fact column use IFNULL(fact_col,0)
    which will look like this
    http://a.imageshack.us/img180/6616/ifnull.jpg
    thanks,
    saichand.v

  • Sort multiple records with textfield

    Hi everyone!
    I want to sort from the backend all the products in a site.
    First, i use the repeat region server behavior, to list all the products. Added a textfield in each record, with the value of it's position (1, 2, 3, 4, etc).
    How can i update that?
    Just like Joomla! has in it's backend. A list with all the articles created, and the ability to change the order of the articles, changing it's number and pressing "save".
    Hope you guys understand me, my english is very very poor...
    Thanks in advance
    Cheers!
    A pic to better understanding:

    Since you refer to Joomla, I presume you want to do this with PHP/MySQL.
    Basically, you need to put your repeat region in a form, and submit the values of the product IDs and the new order numbers as arrays. To do this with PHP, you add a pair of square brackets after the form elements' names like this:
    <input type="text" name="product_id[]" value="....
    <input type="text" name="order[]" ...
    When the form is submitted, $_POST['product_id'] will contain an array of the primary keys, and $_POST['order'] will contain an array of the values inserted in the order text boxes.
    You then need to create a loop to update the existing records in the database. The following is not the full code, but should show you the basic principle:
    // connect to database
    for ($i = 0, $len = count($_POST['product_id']); $i < $len; $i++) {
        $sql = 'UPDATE products SET order = ' . mysql_real_escape_string($_POST['order'][$i]
               . ' WHERE product_id = ' . mysql_real_escape_string($_POST['product_id'][$i];
        mysql_query($sql);
    This loops through the $_POST['product_id'] array and updates each record with the appropriate value for order. The SQL query assumes that both product_id and order are numeric data types.

  • How to Control with Maximum Record per Page

    Hi Every one
    how can I control with maximum record per page with parameter sending from Form
    ex.
    if I set parameter with value=15 the report will run 15 rows per page ?
    Can anyone help me Please ASAP ?
    Thankx

    PLay around with SRW.SETMAXROW , from memory that should do the trick.
    Cheers.
    N.

  • Issue with fetching a record from IHPA table with PARNR value as condition

    Hi All,
    I am trying to fetch a particular record from IHPA table where i am giving the OBJNR number, PARVW and PARNR as customer no. , But I am not getting any record  but if i will give OBJNR and PARVW it will generate a record with having same PARNR value.
    Example :
    case 1st :-
    when i am passing following values in IHPA table,
    OBJNR  = IE000000000010070910
    PRAVW = BP                 
    PARNR  = 620000562   
    it is not generating any record  
    case 2nd:-
    But if i will pass  
    OBJNR  = IE000000000010070910
    PRAVW = BP
    it will generate a record?(one row) which will include PARNR as same 620000562 along with other value.
    Q1 -  Why it's not generating in case 1st,
    Q2 - How I will resolve this issue ? as i have to link some table as EQUI -
    IHPA -
    VCKUN
    VCNUM ?
          I have written a code for this to join :
         SELECT A~EQUNR
           B~PARVW
           C~CCNUM
           D~CCNUM
           FROM EQUI AS A JOIN IHPA AS B ON AOBJNR = BOBJNR
           INNER JOIN VCKUN AS C ON BPARNR = CKUNNR
           INNER JOIN VCNUM AS D ON CCCNUM = DCCNUM
           APPENDING CORRESPONDING FIELDS OF TABLE IT_OBSO
           WHERE A~EQUNR IN S_EQN.
                 AND PARVW  = 'BP'.
          But it is not generating any record but when i am commenting the PARVW i am getting the mixed record of PARVW as (BP,SP,SH)
    Let me know if any more clarification required.
    Highly appreciate your help
    Thanks & Regards
    Shaw

    Hi Santosh,
    Thanks for your quick response.
    But I am Sorry, I already checked with putting Zero before PARNR.
    The issue is still as it is.
    Thanks
    Shaw

  • Depriciation for asset with limit on maximum value

    Hello,
    We have a requirement in which we have a limit on maximum value of the asset on which depreciation is allowed. For example is asset value is more than 100000 then depreciation should be allowed only for value 100000 and the depreciation should be spread across the useful life of the asset.
    I have an asset having value 300000 and I am able to post depriciation only for 2 years (50000 each) whereas the useful life of asset is 6 years.
    What I want is that the depreciation should be posted for 6 years with value (100000/6) 16666.67 each year.
    Has anyone come across the similar business scenario earlier?
    Any pointer to this will be of great help.
    Thanks
    Jyoti

    Hi,
    I found the solution.
    IT can be done by defining the maximum value of the asset in the SPRO path
    AA-> valuation mentods> further settings> define maximum base value.
    Jyoti

Maybe you are looking for

  • Query taking much time to execute

    The following query is taking more than 4hrs to execute. select  l_extendedprice , count(l_extendedprice) from dbo.lineitem group by  l_extendedprice Cardinality of table : 6001215 ( > 6 million) Index on l_extendedprice is there ReadAheadLobThreshol

  • ADE 4.0.1 and Windows 8.1

    Is Adobe 4.0.1 compatible with Windows 8.1? The highest Windows-version on the Adobe-website, that I have found for ADE 4.0.1 is: 7. Can't find anything about ADE 4 and W8.1. thx Willem

  • Dynamic action on File browse change event

    Hi Experts, apex version 4.1 This is what i'm trying to do.. i have a file browse control and a text field. when user selects a file, selected fiel's name (without file type extension) should be set to text field. I tried adding a dynamic action to f

  • How do i get my ipod touch 1st generation to work like normal?

    hey i got an ipod touch 1st generation and i recently upgraded my software but my ipod is always in recovery mode and when  i restore it with new software it work just fine for about 20mins then  i goes back to recovery mode and dont wanna charge or

  • After updating to ios5 my previously purchased ring tones don't work.

    After updating to ios 5 my ring tones show only the default ones on my phone.  In Itunes it shows up, I have synced a number of times but the ring tone never shows up on the phone.