How to nest the top-N capability?

Okay, guys.  Here's a mind-bender for you.  Let's use an altered version of Oracle's EMP table.  Assume we add a "company number" column named CONO.  Thus, in the revised EMP table, each company has many departments, and each department has many employee salaries.  The objective is to print the total salaries by company.  The trick is to only sum the top-N salaries of each department.  Can any of you wizards crack this nut?
Thanks,
Kim
PS: If possible, please try to use the "with t as" syntax.

Hi,
I don't remember if I have modified by EMP table for some other tests anyway what I get running the following result in this 3 top salaries:
with got_rnk as
   select e.*,  rank() over (partition by cono order by sal desc) rnk 
     from emp e
select *
  from got_rnk
where rnk <=3;
     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO       CONO        RNK
      7839 KING       PRESIDENT            17-NOV-81       5000                    10          1          1
      7788 SCOTT      ANALYST         7566 19-APR-87       3000                    20          1          2
      7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30          1          3
      7902 FORD       ANALYST         7566 03-DEC-81       3000                    20          2          1
      7566 JONES      MANAGER         7839 02-APR-81       2975                    20          2          2
      7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10          2          3
6 rows selected.
Grouping by CONO result in the following:
with got_rnk as
   select e.*,  rank() over (partition by cono order by sal desc) rnk 
     from emp e
select cono, sum(sal) selected_sal
  from got_rnk
where rnk <=3
group by cono;
      CONO SELECTED_SAL
         1        10850
         2         8425
In case you have similar values for salary you may decide to use DENSE_RANK instead of RANK as already explained by Solomon in the other thread.
Also in the initial question you were asking:
The objective is to print the total salaries by company.  The trick is to only sum the top-N salaries of each department.
And it was not clear if you wanted to have the top N salary per department or per company.
Anyway after you have posted the output it was a bit more clear.
By the way, what's the difference with the question you posted yesterday??
It seems you are asking the same question but grouping by CONO instead of DEPTNO.
Regards.
Alberto
Message was edited by: AlbertoFaenza
Clarifications added

Similar Messages

  • How to print the top of page part along with the ALV list and generate PDF

    HI all,
             I have created one ALV by using oops concept .
             and also am able to get the top of page where I have One standard logo on the right hand side
             and some details on the left side .
               Now my requirement is to while printing the list the logo and other top of page details should appear
               In the PDF output but currently while am pressing the print preview button only the alv data is coming
              am already using the method
        handle_top_of_page
          FOR EVENT print_top_of_page
                 OF cl_gui_alv_grid,
    may be am missing something ... How to get the top of page along with the logo printed ?

    Hi  Surya,
    After generating the grid display  click on print button,
    a spool number is generated. capture the spool number and convert it to pdf using the fm:
    CONVERT_ABAPSPOOLJOB_2_PDF  and save the file
    Hope this will solve your problem.
    Regards,
    R K.

  • How to edit the top sites on safari 7.0.1

    The edit button that usually sits at the bottom of the top sites page in safari no longer exists and i can't figure out how to change my top sites or the amount of top sites displayed.

    Safari 7
    Top Sites Edit button is moved to top left corner the tile from the bottom left corner.
    Hover the mouse pointer near the top left corner of the tile,
    Edit button and the pin should appear.
    Best.

  • How to list the top 10 largest files in the current working directory ?

    How can I list the top 10 largest files in the current working directory in Solaris ?

    execute below....to get the large files in order.. change the <FS>
    find /<FS> -type f -size +1024 -ls | awk '{print $11, $7}' | sort -rn +1 | awk '{print $1, $2/1024/1024 "MB"}' | /bin/more

  • How to get the top most community given a sub community ID

    Hi,
    I have developed a custom navigation scheme with top level communities as top nav and the sub communities of the top level communities in the left nav.
    Now when the user selects a particular sub community in the left nav the top level community in the top nav should be highlighted.
    I am able to get the first level parent community of the current community but not able to recursively reach the top most community.
    I have tried the following piece of code
    IPTCommunityManager ptOM = (IPTCommunityManager) ptMgr.GetInterfaces("IPTCommunityManager");
    IPTCommunityInfo ptCommunityInfo = ptOM.CachedOpenCommunityInfo(communityID ,true);
    IPTQueryResult result = ptCommunityInfo.GetParentCommunity() ;
    I dont know how to get the communityID from result
    Any pointers in this regards will be very helpful
    Thanks

    To get community ID from the result just use this code:
    int nCommID = result.ItemAsInt(0, PT_PROPIDS.PT_PROPID_OBJECTID);
    The NavigationModel class in portalpages project has a wealth of code dealing with these types of tasks. Look around there if you don't know how to code something, it is a good source for examples.
    You would then get the CommunityInfo object for the parent community ID and call GetParentCommunity again until you reach the top. The IPTCommunityInfo objects are cached so the performance will not be too bad.

  • How to make the top bar opaque as it's hard to read transparent background.

    The transparent bar at the top of the browser is terrible to read and is distracting. I want to get rid of it. How?

    Hi,
    please have a look at skinning
    http://download.oracle.com/docs/cd/E15523_01/apirefs.1111/e15862/toc.htm
    search for af:calendar
    Skinning is explained in teh Web Developer Guide on
    http://download.oracle.com/docs/cd/E15523_01/web.1111/b31973/toc.htm
    Frank

  • How to get the TOP MAT from a component material of BOM list?

    I only kown a sub material of a BOM Llist & not sure its level in the TOP MAT's BOM.
    How can i get the TOP MAT of this sub material?
    Any BAPI or Function?
    TKS a million~~~

    HI .. in my view you should ask your ABAP to help you with a small program to solve your purpose, i am not aware of any system available t-code/ program ..... hence this is what we also did at client place.

  • How to enable the "top activities" from the performance in OEM12C?

    We installed the 12C OEM. But the "top activities" and other options are not enabled from performance pane. I tried to set the parameter control_management_pack_access = "DIAGNOSTIC+TUNING" . Does not work. As was told that should be due to the databases we are running is 11.2.0.2 standard edition. We have the license for the tuning package. Does anybody knows how to enable the option on standard edition database?
    Thanks!

    Unfortunately, we are using SE and need to use OEM to monitor on the database performance activities.

  • How to replace the top of my laptop due to it being broken on

    MY SCREEN BROKEN AND THE TOUCH SCREEN SO THE TOP NEEDS REPLACEING WHICH I HAVE GOT ONE JUST NEED TO KNOW HOW TO REPLACE IT

    Here is your Service Manual:
    Manual
    It is all in there. See page 55 in particular.
    If this is "the Answer" please click "Accept as Solution" to help others find it. 

  • How to find the top ten reports consuming the high response times in st03n?

    HI Basis Gurus,
    Please could anybody tell me "How can one find the top ten high response time consuming reports in st03n transaction or in is there any other way to find out this.Please help me its urgent....
    Advance thanks....

    hi Meghadoot,
    below is an example for your request.
    ST03N
    Expert mode
    Server name - Day- double clicdk Today
    In analysis view - Ranking lists-double click Top Response time
    Regards

  • !!!How to remove the top 'Window' menu in Applet!!! PLEASE

    How do I remove the top 'Window' menu? SDI (single document interface)? or something? How to I enable SDI in 9i? Can't find this answer anywhere.
    I managed to remove the rest of the default menu items at the top, but I DON'T WANT THAT 'WINDOW' ITEM STAYING THERE.
    Pheeew. Please. Getting annoyed... :)

    Edit your base html files and add the USESDI=yes parameter to the serverargs parameters.
    See the Forms demos (downloadable from OTN) for an example of this.

  • How to change the Top sites in Safari 6.1 ?

    the thing is, when I decided to update my Safari, it changed. I can't edit the Top sites because I can't find the oh-so-called 'edit' button.
    so how can I change it??

    This is how I did it using Safari 6.1.3:
    Add the webpage you want to add to your Top Sites to your Bookmarks Bar by dragging the URL to the Bookmarks Bar.
    Open your Top Site page and make sure your Bookmarks are open in your sidebar (View > Show Sidebar > Bookmarks)
    Drag the webpage you added to your Bookmarks Bar in [1] onto the Top Sites page. The existing pages will move around and you can drop your webpage when you're happy with its position.
    Hope it works!

  • How to populate the top left corner of a pivot table

    Can anybody tell me in a pivot table of ADF DVT, whether it is possible to fill any content into the top left corner of the title row, which is the leftmost cell to the left of the column edge?
    I did not see any demo samples which can populate any content into this cell, but leave it blank instead.
    Thank you.
    XF

    Can anybody tell me in a pivot table of ADF DVT, whether it is possible to fill any content into the top left corner of the title row, which is the leftmost cell to the left of the column edge?
    I did not see any demo samples which can populate any content into this cell, but leave it blank instead.
    Thank you.
    XF

  • Help: How to make the top level navigation into vertical at the left panel

    We have a need to change the top-level and 2nd-level navigation bars (horizontal)
    into the nodes on the left navigation panel (vertical).
    We find some way but it is a very extensive job.
    Is there any way to do it quickly?
    Thanks a lot! Points guaranteed.

    Hi,
    Try to develop your own menu by using Navigation Tag Libraries. Check this link :
    <a href="http://help.sap.com/saphelp_nw04/helpdata/en/42/f35146a7203255e10000000a1553f7/frameset.htm">Navigation Tag Library</a>
    There is also sample codes. You can start your project from this codes.
    Regards
    Abdul.

  • How to change the top left corner icon of a Frame?

    I have created jpg and would like to replace the default java top left corner icon of a JFrame and JInternalFrame, how to change it??
    thx very much

    ImageIcon customImageIcon = new ImageIcon("your-jpg-file.jpg");
    Image customImage = customImageIcon.getImage();
    yourFrame.setIconImage(customImage);
    yourInterfanFrame.setFrameIcon(customImageIcon);That's it

Maybe you are looking for

  • The Group Policy Client service failed the sign-in The universal unique identifier (UUID) type is not supported

    Hi guys, we created a custom WIM Image (Windows 8 Enterprise) with MDT 2012. Sysprept the Image, Deployed via SCCM 2012 SP1. Computers are Domainjoined. Error with standard Domain User. On some computers (not every computer) and not with every user o

  • Iphoto not reading iphone

    All of a sudden iPhoto is not reading my iPhone. As a result I cannot download new images. Any ideas?

  • Brush size, CS4

    Hello, Cannot use brackets [ and ] to alter the brush size because of swedish keyboard. Thought of remapping the function to other tangents but cannot find an entry for that in Edit>Keyboard shortcuts, is there one and where in that case? Thank You a

  • Accidentally moved a folder-help

    I am running Windows XP and use Adobe Photoshop Album Starter Edition 3.2 to download & view my pictures from my camera's memory card. I was trying to put some pictures on a cd (using Windows Explorer) and accidentally moved a picture folder (I'll ca

  • Organisational charts

    Hi, I am using Crystal Reports 2008 and would like to know if any chart is available to implement organisational hierarchy. Such as manager, team members the hierarchy should be represented in the chart. Kindly let me know if any work around or alter