Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PLSQL help

Options
  • 08-09-2006 10:04am
    #1
    Closed Accounts Posts: 170 ✭✭


    Hi,

    I need some PLSQL code that will find the top ten employee numbers that appear most often in an incident table. The incident table has a number of fields including dates, times, description... It also holds the ECode of the person who logged it. I need to find the top ten ECodes.. i.e. a top ten list of Employees, where no. 1 is the Employee that has logged the most incidents.

    Can anyone help?

    Thanks in advance.


Comments

  • Registered Users Posts: 123 ✭✭chatterbox


    Havent Tested the below code, but its pretty close what you want.

    select distinct(ecode) ECODE, count(1) NUMBER_OR_ENTRIES
    from TABLE_NAME
    group by ecode
    order by count(ecode) desc

    if you want to enter more conditions, stick in a where clause

    select distinct(ecode) ECODE, count(1) NUMBER_OR_ENTRIES
    from TABLE_NAME
    WHERE condition
    group by ecode
    order by count(ecode) desc


  • Registered Users Posts: 4,188 ✭✭✭pH


    As for the 'top 10' - if you're using PLSQL you can obviously open a cursor and stop after 10 rows, but it is possible to do it in SQL using ROWNUM.

    ROWNUM works on the base unsorted rows, so you need to add an outer select:
    [B]SELECT * FROM ([/B]
    select distinct(ecode) ECODE, count(1) NUMBER_OR_ENTRIES
    from TABLE_NAME
    WHERE condition
    group by ecode
    order by count(ecode) desc
    [B]) WHERE ROWNUM <= 10[/B]
    
    (Based on chatterbox's original SQL statement)


  • Closed Accounts Posts: 170 ✭✭SteamTrean


    Great help thanks.


Advertisement