How to get the subtotal and top 3 discs only within a group

Hello PL/SQL gurus and experts.


I use Oracle Database 11 g Enterprise Edition Release 11.2.0.1.0 - 64-bit Production version
I have table-

drop table t2;
create table t2(Pat_NM,Hospital,Test_Range,Total) as select
'Andy','Batra','> 10 Mph','20000' from dual union all select
'Andy','Fortis','1-3 Mph','24500' from dual union all select
'Andy','Max','5-10 Mph','10600' from dual union all select
'Andy','Max','5-10 Mph','22500' from dual union all select
'Andy','Aashiana','5-10 Mph','110600' from dual union all select
'Andy','Amar','5-10 Mph','34800' from dual union all select
'Andy','Max','5-10 Mph','600' from dual union all select
'Andy','Columbia','< 1 Mph','27700' from dual union all select
'Andy','Nimhans','< 1 Mph','50000' from dual union all select
'Andy','Meenam','< 1 Mph','11000' from dual union all select
'Andy','Meeran','5-10 Mph','24625' from dual union all select
'Andy','Mnagamani','> 10 Mph','12000' from dual union all select
'Andy','Murari','> 10 Mph','20600' from dual union all select
'Andy','Triveni','5-10 Mph','16500' from dual union all select
'Cindy','Batra','5-10 Mph','14700' from dual union all select
'Cindy','Max','< 1 Mph','170000' from dual union all select
'Cindy','Apollo Medical Centre','> 10 Mph','19000' from dual union all select
'Cindy','MLal','1-3 Mph','22600' from dual union all select    
'Cindy','Columbia','< 1 Mph','28900' from dual union all select
'Cindy','Asian','1-3 Mph','27900' from dual union all select
'Cindy','Mahagun','< 1 Mph','28700' from dual union all select
'Cindy','Manipal','< 1 Mph','29040' from dual union all select
'Cindy','Prestige','< 1 Mph','12700' from dual union all select
'Cindy','A.G.M.','< 1 Mph','97800' from dual union all select
'Cindy','Shobha','< 1 Mph','700' from dual union all select
'Cindy','Aashiana','5-10 Mph','23450' from dual union all select
'Cindy','Amar','1-3 Mph','21325' from dual union all select
'Cindy','Childs Trust','5-10 Mph','22775' from dual union all select
'Cindy','Crescent ','< 1 Mph','20025' from dual;

I need to extract the data based on the condition that-
In each Test_Range get the subtotal of all the records and display only the top 3 hospitals
and ultimately make the total general.

For this I use the following query-

select Pat_NM,
Hospital,
SUM (Total) "Total",
decode(grouping(Test_Range), 0, Test_Range, 'Total') "Test Range",
decode(grouping(Test_Range), 0, max(rank), null) Rank
from
(
SELECT Pat_NM,
         Hospital,
         Test_Range,
         SUM (Total) Total,
         DENSE_RANK ()
            OVER (PARTITION BY test_range || pat_nm ORDER BY SUM (Total) DESC)
            AS RANK,
         ROW_NUMBER ()
            OVER (PARTITION BY test_range || pat_nm ORDER BY SUM (Total) DESC)
            AS rk
    FROM t2
GROUP BY Pat_NM, Hospital, Test_Range
)
where rank <=3
group by grouping sets((Pat_NM, Hospital, Test_Range),())
order by Pat_NM,Test_Range, Rank;


The result is I'm getting is -.

PAT_N Test Ran HOSPITAL                   Total       RANK
----- -------- --------------------- ---------- ----------
Andy  1-3 Mph  Fortis                     24500          1
Andy  5-10 Mph Aashiana                  110600          1
Andy  5-10 Mph Amar                       34800          2
Andy  5-10 Mph Max                        33700          3
Andy  < 1 Mph  Nimhans                    50000          1
Andy  < 1 Mph  Columbia                   27700          2
Andy  < 1 Mph  Meenam                     11000          3
Andy  > 10 Mph Murari                     20600          1
Andy  > 10 Mph Batra                      20000          2
Andy  > 10 Mph Mnagamani                  12000          3
Cindy 1-3 Mph  Asian                      27900          1
PAT_N Test Ran HOSPITAL                   Total       RANK
----- -------- --------------------- ---------- ----------
Cindy 1-3 Mph  MLal                       22600          2
Cindy 1-3 Mph  Amar                       21325          3
Cindy 5-10 Mph Aashiana                   23450          1
Cindy 5-10 Mph Childs Trust               22775          2
Cindy 5-10 Mph Batra                      14700          3
Cindy < 1 Mph  Max                       170000          1
Cindy < 1 Mph  A.G.M.                     97800          2
Cindy < 1 Mph  Manipal                    29040          3
Cindy > 10 Mph Apollo Medical Centre      19000          1
      Total                              793490
21 rows selected.

While I'm looking forward to a result like -

PAT_N Test Ran HOSPITAL                   Total       RANK
----- -------- --------------------- ---------- ----------
Andy  1-3 Mph  Fortis                     24500          1
      Subtotal       24500
Andy  5-10 Mph Aashiana                  110600          1
Andy  5-10 Mph Amar                       34800          2
Andy  5-10 Mph Max                        33700          3
      Subtotal       220225
Andy  < 1 Mph  Nimhans                    50000          1
Andy  < 1 Mph  Columbia                   27700          2
Andy  < 1 Mph  Meenam                     11000          3
      Subtotal       88700
Andy  > 10 Mph Murari                     20600          1
Andy  > 10 Mph Batra                      20000          2
PAT_N Test Ran HOSPITAL                   Total       RANK
----- -------- --------------------- ---------- ----------
Andy  > 10 Mph Mnagamani                  12000          3
      Subtotal       52600
Cindy 1-3 Mph  Asian                      27900          1
Cindy 1-3 Mph  MLal                       22600          2
Cindy 1-3 Mph  Amar                       21325          3
      Subtotal       71825
Cindy 5-10 Mph Aashiana                   23450          1
Cindy 5-10 Mph Childs Trust               22775          2
Cindy 5-10 Mph Batra                      14700          3
      Subtotal       60925
Cindy < 1 Mph  Max                       170000          1
Cindy < 1 Mph  A.G.M.                     97800          2
Cindy < 1 Mph  Manipal                    29040          3
      Subtotal      387865
PAT_N Test Ran HOSPITAL                   Total       RANK
----- -------- --------------------- ---------- ----------
Cindy > 10 Mph Apollo Medical Centre      19000          1
      Subtotal       19000
      Total                              925640
29 rows selected.


I thank you in advance for your valuable time and effort.

Just move the position of the filter level upward.

SQL > select *.
2 starting at)
3. Select pat_nm
4, hospital
5, sum (total) 'total '.
6, decode (grouping (test_range) + grouping (hospital), test_range, 0, 1, 'total of void, 2,' 'total') 'test range. "
7, decode (grouping (test_range) + grouping (hospital), 0, max (rank), null) row
8 of)
9. Select pat_nm
10 in the hospital
11, test_range
12, sum (total)
13, dense_rank () over (partition test_range | pat_nm order of sum (total) / / desc) as rank
14, row_number () over (partition of test_range | pat_nm order of sum (total) / / desc) as rk
15 of t2
Group 16
17 by pat_nm
18, hospital
19, test_range
20                   )
Group 21
22 by grouping sets ((pat_nm, test_range, hôpital), (pat_nm, test_range), ())
Order 23
24 by pat_nm
25, test_range
26, (grouping (test_range) + grouping (hospital))
27, rank
28         )
29 where to classify<= 3="" or="" rank="" is="">

Total number of tests PAT_N HOSPITAL rank RANK
----- --------------------- ---------- --------- ----------
Andy Fortis 24500 1 - 3 mph 1
Andy 24500 subtotal
Andy Aashiana 110600 5-10 mph 1
Andy Amar 34800 5-10 mph 2
Andy, Max 33700 5-10 mph 3
Andy 220225 subtotal
Andy Nimhans 50000< 1="" mph           ="">
Andy Columbia 27700< 1="" mph           ="">
Andy Meenam 11000< 1="" mph           ="">
Andy 88700 subtotal
Andy Murari 20600 > 10 mph 1
Andy Batra 20000 > 10 mph 2
Andy Mnagamani 12000 > 10 mph 3
Andy 52600 subtotal
Cindy 27900 1 Asia - 3 mph 1
Cindy MLal 22600 1 - 3 mph 2
Cindy Amar 21325 1 - 3 mph 3
Total part-time cindy 71825
Cindy Aashiana 23450 5-10 mph 1
Cindy Childs Trust 22775 5-10 mph 2
Cindy Batra 14700 5-10 mph 3
Cindy 60925 subtotal
Cindy Max 170000< 1="" mph           ="">
Cindy A.G.M.                     97800< 1="" mph           ="">
Cindy Manipal 29040< 1="" mph           ="">
Cindy 387865 subtotal
Medical center of cindy Apollo 19000 > 10 mph 1
Cindy 19000 subtotal
925640 total

29 selected lines.

SQL >

Tags: Database

Similar Questions

  • How to get the second and third weekend of every month over a period of time?

    Hello

    No idea how to get the second and third weekend of every month on a given period without use of CLAUSE?

    Thanks in advance.

    Try it below,

    SELECT CASE WHEN TO_CHAR (ADD_MONTHS (TRUNC(startdate,'MM'),(LEVEL-1)), 'DY') = 'SAT '.

    THEN ADD_MONTHS (TRUNC(startdate,'MM'),(LEVEL-1)) + 1

    WHERE TO_CHAR (ADD_MONTHS (TRUNC(startdate,'MM'),(LEVEL-1)), 'DY') = 'Sun '.

    THEN ADD_MONTHS (TRUNC(startdate,'MM'),(LEVEL-1)) + 6

    Of OTHER NEXT_DAY (ADD_MONTHS (TRUNC(startdate,'MM'),(LEVEL-1)), "SAT") + 1

    END as second_weekendday,

    BOX WHEN TO_CHAR (ADD_MONTHS (TRUNC(startdate,'MM'),(LEVEL-1)), 'DY') = 'SAT '.

    THEN ADD_MONTHS (TRUNC(startdate,'MM'),(LEVEL-1)) + 7

    WHERE TO_CHAR (ADD_MONTHS (TRUNC(startdate,'MM'),(LEVEL-1)), 'DY') = 'Sun '.

    THEN ADD_MONTHS (TRUNC(startdate,'MM'),(LEVEL-1)) + 7

    Of OTHER NEXT_DAY (ADD_MONTHS (TRUNC(startdate,'MM'),(LEVEL-1)), "SAT") + 7

    END AS third_weekendday

    FROM (SELECT SYSDATE startdate,

    SYSDATE + 300 enddate

    THE DOUBLE)

    CONNECT BY LEVEL<=>

  • How to get the width and height of Flex Mobile view?

    Hello

    The following instructions can get the width and height of the current Flex app:

    var request: UIComponent = FlexGlobals.topLevelApplication as UIComponent;

    trace (application. Width);

    trace (application. Height);

    But I don't know how to get the width and height of mobile display Flex current (i.e. the content size excludes bar action and the Navigation bar). Why the "this.width" always returns 0?

    Thank you!

    Looks like you're running in this bug: https://bugs.adobe.com/jira/browse/SDK-30070

  • How to get the phone and messages back in my dock apps? And for some reason, I discover now all my app pages from the middle of my phone and not at the top?

    How can I get the phone and messages apps in my dock? And for some reason, I discover now all my app pages from the middle of my phone and not at the top?

    Try

    Settings > general > reset > reset home screen presentation.

    Note: All other applications will be organised by alphabetical order.

  • How to get the scanning and fax utilities works from 2840 printer. Print function works from pc

    I changed my google chrome browser and I finally got my confiquered pc and printer. He is an all-in-one 2840 color laser jet. How do I get the scan and fax utilities to perform?

    It is useful to include the manufacturer and model number.

    What version of Windows you have, including the service pack?

    How the device is connected to your computer (USB, Ethernet, other)?

    Have you installed the software that came with your HP Color LaserJet 2840 AiO printer?

    If not, why?  If you did, what error messages you are seeing?  Please report messages completely, without paraphrasing.

    Start here: HP Color LaserJet 2820 and 2840 AiO products Series - first installation and install

  • How to get the number and the name of the contacts selector

    Hi all

    I got the Contact Picker work but I have no idea how to get the selected telephone number and the name.

    There are attributes that I can put like contactId.value () to retrieve or there is another way?

    Thanks in advance.

    ImageButton{
                        defaultImageSource: "asset:///images/bluebutton.png"
                        onClicked: {
                            contactPicker.open();
                        }
                        attachedObjects: [
                            ContactPicker {
                                id: contactPicker
                                onContactSelected: {
                                    result.text = "You chose contact: " + contactId;
                                }
                            }
                        ]
    
                    }
    
                    Label {
                        id: result
                        text: "You chose contact: "
                    }
    

    Hello

    You can get the contact name and phone number as this,

    ImageButton{
                        defaultImageSource: "asset:///images/bluebutton.png"
                        onClicked: {
                            contactPicker.open();
                        }
                        attachedObjects: [
                            ContactPicker {
                                id: contactPicker
                                onContactSelected: {
                                    result.text = "You chose contact: " + contactId;
    // call a cpp method to get the details
    
    app.getDetails(contactId); } } ] } Label { id: result text: "You chose contact: " }
    

    the CPP code:

    void ContactDetails(ContactId id)
    {
    Contact contact_info = m_contactService->contactDetails(id);
    
        QString firstName = contact_info.firstName();
        QString lastname = contact_info.lastName();
            QList phoneno_list = contact_info.phoneNumbers();
    
        QStringList no_s;
    
        foreach(ContactAttribute attr, phoneno_list)
        {
            no_s << attr.value();
        }
    }
    

    You can get details like this.

    Kind regards

    Naresh Kodumuri.

  • How to get the name and number of the procedure parameter list or a function?

    I stated the procedure described in the package

    If I want to get the number and the name of the parameter list, so how do I it

    I don't have I her name with the package.

    create or replace package demoApp is

    PROCEDURE insert_data (p_fname IN VARCHAR2,

    p_lname IN VARCHAR2,

    p_address IN VARCHAR2,

    p_cellno IN VARCHAR2,

    p_email IN varchar2);

    end demoApp;

    create or replace package demoApp body is

    PROCEDURE insert_data (p_fname IN VARCHAR2,

    p_lname IN VARCHAR2,

    p_address IN VARCHAR2,

    p_cellno IN VARCHAR2,

    p_email IN varchar2) IS

    EmpID number;

    BEGIN

    Select nvl (max (emp1.empid), 0) + 1 in EMP1 empid;

    INSERT INTO VALUES EMP1 (empid, p_fname, p_lname, p_address, p_cellno, p_email);

    EXCEPTION

    WHILE OTHERS THEN

    raise_application_error (-20001,' insert the problem ' |) SQLERRM);

    END;

    end demoApp;

    -----------------

    I want to get the name of the parameter and the number of use of stroredrprocedure ""demoApp.insert_data " "

    Select *.

    of all_arguments

    where package_name = 'DEMOAPP.

    and object_name = 'INSERT_DATA.

  • How to get the LOGO and company name in the theme of the 4 apex

    Hello

    I use the theme 4 for apex 4.0.2.
    I must have the logo as well as the name of the company the two, but when I select den Image it shows only the image in the dashboard and when I select den text it shows the name of the company.
    How to get the two logo and the name of the company?

    Thanks for your help.

    Kind regards
    Sébastien Pallav.

    Pushpesh Pallav says:
    Hello

    I use the theme 4 for apex 4.0.2.
    I must have the logo as well as the name of the company the two, but when I select den Image it shows only the image in the dashboard and when I select den text it shows the name of the company.

    The word is spelled 'then', not 'den '.

    How to get the two logo and the name of the company?

    Select the text, and include HTML, making reference to the image in the text:

    
    

    The image src URI used depends on the location of the image, which you have failed to specify.

  • How to get the length and sub lymph nodes.

    Hello

    find the script below.

    < List1 >

    < list >

    < List1 >

    < List1 >

    < List1 >

    < / list >

    < list >

    < List2 >

    < List2 >

    < List2 >

    < / list >

    --

    --

    -

    -

    -

    -< / list1 >

    Here the tag list will be growing based on the I / p. Now what I want is to know how to get the length of the tag < list >.

    To implement the above I used the code below

    var number = xfa.record.List1.List [0].nodes.length; (Does not work)

    But if var account = xfa.record.List1.nodes.length; (work)

    and also I want to delete a tag inside the < List > tag. How?

    Very Urgent.

    Thanks in advance.

    Yes, you need to use the name instead of the value to get the tag name...

    xfa.datsets.data.Root.nodes.item (0) .name

    Thank you

    Srini

  • How to get the public and private keys to use recaptcha?

    I registered with google to get a recaptcha for my Web site. I've gotten has been the key to site and the secret key! If I need to get the public and private key for muse!

    Hello

    Please use the site as a 'public key'key key and Secret as "clΘ privΘe".

    Concerning

    Vivek

  • How to get the speed and the influence of the keyframe?

    Hello world

    I'm sorry if my question is due lack of understanding of script in AE but I can't seem to get the speed and influence the properties of selected or closest to the keyframe, I found that the ways to implement.

    Can someone tell me please in the right direction?

    Thank you!

    property.keyInTemporalEase (key) and property.keyOutTemporalEase (key) will give you the objects made easy with speed and influence the attributes for a particular key.

    Dan

  • How to get the Favorites and the top tool bar after the Mozilla refresh operation.

    I did the update Mozilla, the recommendation that popped up today on my browser. He promised that I would get my favorites back - but they AND the top toolbar are now gone. Tried Mozilla help but cannot see any detailed help to restore the bookmarks and the top of the browser window toolbar. (found notes on the restoration of the bookmarks, but without the toolbar, are not available). People need to come back.

    You can try the list of toolbars. This is available in a few different places:

    • Right click on an empty area of the tab bar, or the "+" button on the tab bar
    • Press the Alt key to display the menu classic temporarily bar
    • Click on the button '3-bar' menu > customize > ' show/hide toolbars.

    Can you get your Menu bar and toolbar bookmarks displayed again?

  • How to get the hang on top of layers that extend beyond the canvas?

    I have several image layers in a document which range from (inteninally) on the edge of the canvas.  Whenever I have add a stroke (black border) to the doc, images that overlap are on top of the border of the race.

    How to get around this?

    Thank you.

    Are you sure that set you the fill to 0% and not the opacity?

  • How to get the .rbf and .through files?

    Hello I am currently working on the project, which provides for automatic deployment and launch programs everchanging.

    Ev3 are scattered in different schools. I wrote a simple script that can download a file and then send it to ev3 via wifi.

    But I'm currently stuck on how to get .rbf and Labview .through files.
    I know that I can deploy .vi using the target to ev3 and then upload these files to the PC, but I can't add script.
    Is it possible to compile .vi to the file?

    Sorry, try this

  • How to get the height and width of the screen of the blackberry device?

    Can someone tellme how to get details of the screen of the blackberry device?

    Display.getWidth () and Display.getHeight ().  If this isn't what you want, let us know.

Maybe you are looking for