Question about GROUP BY and double aggregation for example MAX (AVG (val))

Good evening/morning,

I am struggling with what is probably a simple problem.

The objective of the exercise is to display the highest average earnings and his Department (with the EMP table).

Easy to get the maximum average wage:
select max(avg(sal)) as max_avg_sal
  from emp
 group by deptno;
I could not figure out how to change this query to get the deptno associated AVG. max. Gave up on changing this query and came to this:
select deptno,
       avg_sal as max_avg_sal
  from (
        select deptno,
               avg(sal) as avg_sal
          from emp
         group by deptno
         --
         -- order causes 1st row to have the max(avg(sal))
         -- this will be exploited in the where of the outer query
         --
         order by avg_sal desc                                             
       )
 --
 -- get only the first row since that one has the values we want
 --
 where rownum <= 1;
This works, but feels pretty disappointing compared to the simplicity of the first query (which I couldn't the deptno on.) That is the request more simple with that I could come.

The QUESTION is:

The query above is really the way simpler and easier to get the maximum average and its associated department number? If it isn't, I'm more interested in your simplest solution. :)

Could someone to laugh at one of the alternatives that I came up with a "simpler": solution
with x as
  (
   select deptno,
          avg(sal) as avg_sal
     from emp
    group by deptno
  ),
  max_avg_sal as
  (
   select max(avg_sal)                  as asmax
     from x
  ),
  deptno_max as
  (
   select deptno                        as dnmax
     from x where avg_sal = (select asmax from max_avg_sal)
  )
select (select asmax from max_avg_sal) as max_avg_sal,
       (select dnmax from deptno_max)  as deptno_max
  from dual;
The road to hell is simple, it is paved from selects < chuckle >,.

Thank you for helping,

John.

Hello
Try this

SELECT MAX(AVG(sal)) AS max_avg_sal,
       MAX(DEPTNO) KEEP (DENSE_RANK FIRST ORDER BY AVG(SAL) DESC )  DEPTNO
FROM scott.emp
GROUP BY deptno

Kind regards
Anthony Alix

Tags: Database

Similar Questions

  • Question about activating windows and media used for installation

    Intel Visual BIOS 2 my computer on board DZ87KLT - 75 K (Firmware updated) can not detect the optical drive as boot EFI device. So I made a bootable USB flash drive to my system OEM Windows 7 Pro x 64 DVD manufacturer who (USB flash drive) my system detect successfully as efi boot device. My question is if I do a windows 7 clean install with EFI enabled using the flash drive on the same computer that runs this window for the last 9 months, there will be an activation problem to not use the DVD?

    The problem is that your key is already used. He'll have to re - install Windows by using the same key (as long as it's the same motherboard and not a replacement) phone activation.

    J W Stuart: http://www.pagestart.com

  • Question about ADE downloads and Firefox extention for epub books

    Hello

    I am new to the use of my nook color and new on the forum. I hope I can explain my problem, so everyone can understand it.

    Downloads for ADE the library fine - they zip in full and I can load into the corner without any problem. I have read the info on downloading from other sites ePub files and downloads go to download or document files. I see them on the screen of the ADE but they disappear. I then transfer them on ADE by using the function "Add point" which will then appear in the lists/download the document. I double click to get them to ADE (if I have the chance and do right-lol)

    Now, I also can sideload these books directly in my corner and pass ADE with my extention for Firefox for EPUB. I may be hopelessly confused, but why use ADE with these sorts of downloads. It is an extra step. IS this necessary? I do something wrong in a first time?

    Any notice are received with gratitude!

    Thank you

    Nancy

    Nancy, ADE is useful for two reasons.  You must be the judge of

    to know if these reasons no sense to you.

    ADE is a library and a "transfer agent" management system for

    ePublications.  Among other things, it interacts with digital rights

    epublication and designed inside so material management it

    complies with the Digital Millennium Copyright Act of 2000.  This role is

    important industry, and is one of the reasons why the ADE is "mandatory".

    libraries and some distributors when they transfer ebooks to

    users.  The libraries provide data of single loan as interpreters ADE so that

    Library ebooks are controlled without delay of loan at the library.

    Booksellers are using the features of ADE for manage and control the distribution of

    material. you pay.

    ADE also serves as a "translation agent" to epublications

    transferred to different types of devices.  It supports several "unusual."

    types of devices and their various quirks and peculiarities.

    I also transferred ebooks directly to my ereader.  The problem is that the

    I transferred are free, so most of the points in my first paragraph

    do not - apply except that I would like to have a FREE software that allows me to manage

    My eLibrary on my computer, which is much more reliable than my

    eReaders...  Backup is paid.

    I hope this helps!

    =============

  • Question about GROUP BY and HAVING

    Good afternoon

    I have the following query, which returns the desired result (a set of students who follow the CS112 or CS114 but not both). I wanted to 'condense' it in a single SELECT statement (if that's possible - DDL of execution of the instruction is provided at the end of this post):
    --
    -- is this select distinct * and its associated where clause absolutely
    -- necessary to obtain the result ?
    --
    
    select distinct *
      from (
            select s.sno,
                   s.sname,
                   s.age,
                   sum(case when t.cno in ('CS112', 'CS114')
                            then 1
                            else 0
                       end)
                     over (partition by s.sno) as takes_either_or_both
              from student s join take t
                               on (s.sno = t.sno)
           )
      where takes_either_or_both = 1
    ;
    The following text seemed reasonable, but unfortunately without success:
    /*
      Window functions not allowed here (in Having Clause)
    
    select max(s.sno),
           max(s.sname),
           max(s.age),
           sum(case when t.cno in ('CS112', 'CS114')
                    then 1
                    else 0
               end)
             over (partition by s.sno) as takes_either_or_both
      from student s join take t
                       on (s.sno = t.sno)
     group by s.sno
    having sum(case when t.cno in ('CS112', 'CS114')
                    then 1
                    else 0
               end)
             over (partition by s.sno) = 1
    ;
    
    */
    
    /*
    
    Invalid identifier in Having clause
    
    select s.sno,
           s.sname,
           s.age,
           sum(case when t.cno in ('CS112', 'CS114')
                    then 1
                    else 0
               end)
             over (partition by s.sno) as takes_either_or_both
      from student s join take t
                       on (s.sno = t.sno)
     group by s.sno, s.sname, s.age
    having takes_either_or_both = 1
    ;
    I tried a document that completely defines the order in which the clauses are executed. I found bits and pieces here and there but not complete something. I realize that my race with problems like this is due to my lack of understanding of the sequence and scope of the clauses that make a statement. For this reason, I can't even say if it is possible to write the query above using a single select statement. Forgive my little frustration...

    Thank you for your help,

    John.

    DDL follows.
            /* drop any preexisting tables */
    
            drop table student;
            drop table courses;
            drop table take;
    
            /* table of students */
    
            create table student
            ( sno integer,
              sname varchar(10),
              age integer
            );
    
            /* table of courses */
    
            create table courses
            ( cno varchar(5),
              title varchar(10),
              credits integer
            );
    
            /* table of students and the courses they take */
    
            create table take
            ( sno integer,
              cno varchar(5)
            );
    
            insert into student values (1,'AARON',20);
            insert into student values (2,'CHUCK',21);
            insert into student values (3,'DOUG',20);
            insert into student values (4,'MAGGIE',19);
            insert into student values (5,'STEVE',22);
            insert into student values (6,'JING',18);
            insert into student values (7,'BRIAN',21);
            insert into student values (8,'KAY',20);
            insert into student values (9,'GILLIAN',20);
            insert into student values (10,'CHAD',21);
    
            insert into courses values ('CS112','PHYSICS',4);
            insert into courses values ('CS113','CALCULUS',4);
            insert into courses values ('CS114','HISTORY',4);
    
            insert into take values (1,'CS112');
            insert into take values (1,'CS113');
            insert into take values (1,'CS114');
            insert into take values (2,'CS112');
            insert into take values (3,'CS112');
            insert into take values (3,'CS114');
            insert into take values (4,'CS112');
            insert into take values (4,'CS113');
            insert into take values (5,'CS113');
            insert into take values (6,'CS113');
            insert into take values (6,'CS114');

    Hi, John,.

    Just use the SUM aggregate function.

    --
            select s.sno,
                   s.sname,
                   s.age,
                   sum(case when t.cno in ('CS112', 'CS114')
                            then 1
                            else 0
                       end) as takes_either_or_both
              from student s join take t
                               on (s.sno = t.sno)
           GROUP BY  s.sno,
                    s.sname,
                  s.age
           HAVING  sum(case when t.cno in ('CS112', 'CS114')
                            then 1
                            else 0
                       end)  = 1;
    

    Analytical functions are calculated after the WHERE - HAVING clause are applied. Use the results of an analytic function in a WHERE or HAVING clause, you need to calculate it in a subquery, and then you can use it in a WHERE - or the HAVING of a query clause Super.

  • Questions about the terms and conditions

    Dear team of Adobe Stock,

    I am considering a subscription for an e-commerce site that I am developing. I have a few questions about the terms and conditions:

    3.5 social media use. You can view or download an unmodified version of the book on the Social media Site if (A) you include a notice of copyright in the work itself (© author name - stock.adobe.com) and (B) the terms of use governing the Social media Site do not include any provision that would grant exclusive rights or the ownership of those works or alterations to anyone. "Social Media Site" means a website or application that puts the main emphasis on facilitating social interaction between its users and allowing users to share content in such social interaction

    What I have to insert in each post on social media on behalf of the author? Generally, this information is displayed only for free images. What I have to insert this information even if I signed up for a plan?

    Thank you

    Hello

    Please see the link below for help:

    http://wwwimages.Adobe.com/content/dam/ACOM/en/legal/servicetou/Adobe-stock-additional-ter ms_20160119.pdf

  • A question about the methods and parameters.

    Hey guys, this is my first post here. I am very new to Java and done a bit of C++ before Java. I had a question about the methods and parameters. I do not understand the methods; I know they can be repeated when it is called, but it's almost everything. I also know that a program should have a class that contains the main method. What I really, really understand on methods is what the parameters are. I know they are in parentheses and that is it. Could you explain what they are? I really appreciate it. Thanks to all in advance. Best regards, Michael

    Taking an example:
    Suppose you calculate area of the rectangle you need two inputs one is the length and the width. Area = l X b, where l = length, b = width

    If your method, say, calculateAreaOfRectangle (length int, int width) will be two parameters as arguments.

    System.out.println ("field of rectangle:" + calculateAreaOfRectangle (40,30);)

    public int calculateAreaOfRectangle (int length, int width) {}
    int area;
    Area = length * width;
    return of area;
    }

    So if you call this method then the output will be returned in 120.

    Parameters of a method are simply the input variables for the method of treatment for all calculations or something useful.

    And we cannot have methods inside the main method in Java. It is in the java syntax and if you do, it will throw a syntax error.

  • Question about the interruptions and priming

    Hello

    I had a few questions about the startup and interruptions in a virtualized environment

    Assuming that, plenty of virtualization (no material assistance to virtualization of memory ).

    1 initialization: in an ordinary PC, the boot process starts with the BIOS, to expansion ROM, back to the BIOS and MBR secondary boot record, then grub (is it) and finally the operating system. In a virtualized environment with VMM running directly on top of hardware (Native VMM), how is the initialization of the different process, as I understand it takes BIOS-> Expansion ROM - > BIOS-MBR-> other record secondary-> VMM-> OS--> applications >. Am I right on that?

    2 breaks: VMM examines the source interruptions prior to the interruption, so in a multicore environment, assuming a 2 processor core, how the VMM decides on the kernel for which the interruption in intended, prior to shipment of the interruption. Interruptions are tag ID, said core ID?

    Thanks in advance

    -SC

    sidc7 wrote:

    1 initialization: in an ordinary PC, the boot process starts with the BIOS, to expansion ROM, back to the BIOS and MBR secondary boot record, then grub (is it) and finally the operating system. In a virtualized environment with VMM running directly on top of hardware (Native VMM), how is the different boot process, if I understand correctly to -> BIOS-> Expansion ROM BIOS-> MBR-> other documents-->--> OS--> requests VMM. Am I right on that?

    It is simplistic but essentially correct.  A metaphor would be a little better thinking that the hypervisor/vmm is an operating system, except that the process on this operating system are requests or comments OSes.  If the BIOS-> OPROM-> BIOS-> MBR-> hypervisor-> init process of the hypervisor-> reviews of fork () s operating systems.  This can become blurred by the design choices (for example Xen/Hyper-V init start a coded parent hard partition that makes hypercalls privileged to fork() guest OSes; ESXi init runs a very limited subset of applications directly on the hypervisor and ESX Classic is somewhere between these approaches).

    There is a subtle distinction between "hypervisor" and "www."  The "hypervisor" is the operating system of single root (e.g. ESX) who interacts directly with the hardware and has access to everything; a hypervisor is not to be used to run virtual machines.  When we talk about a "hypervisor", the tendency is to describe something which is a BONE (although usually a minimal OS designed only to run virtual machines).  The "www" is the layer that allows a virtual machine run: it provides the interposition, virtualization and emulation services and can be plural (for example a vmm by guest operating system).  Architecture of VMware deal with these two separate components, while most other virtualization platforms merge the two.

    sidc7 wrote:

    2 breaks: VMM examines the source interruptions prior to the interruption, so in a multicore environment, assuming a 2 processor core, how the VMM decides on the kernel for which the interruption in intended, prior to shipment of the interruption. Interruptions are tag ID, said core ID?

    Strictly speaking, the interruption is not transmitted to a guest operating system - receives the hypervisor (or rather the VMM it transmits to the hypervisor), the hypervisor drivers interpret the interruption (for example reading package of NIC, I/O process of HBA, the timer tick completion), then after a new appropriate break in the guest OS.

  • Just bought a Nikon d750 and confused about adobe LR4 and PS6 support for RAW files. I have 8.7 DNG, but I was wondering if LR and PS will import direct soon thanks for all the tips

    Just bought a Nikon d750 and confused about adobe LR4 and PS6 support for RAW files. I have 8.7 DNG, but I was wondering if LR and PS will import direct soon thanks for all the tips

    Support for the Nikon D750 was introduced in the last version of LR 5.7 and ACR 8.7 Novemder 18 2014.

    New updates for LR 4 were arrested when LR 5 was released on June 9, 2013. No other updates of bug fixes and new camera support.

    Nada, LR 4 will never support Nikon D750. The Nikon D750 was introduced on the market in September 2014 some 15 months after further development of LR 4 was arrested.

    You can use the Adobe DNG (free download for packing) program to convert your Nikon D750 Nef (raw) files in Adobe's DNG format which will allow you to import those into LR 4. This is the crutch provided by Adobe to allow the processing of raw files with versions out-of-date LR and ACR.

    You can also update the ACR for PS CS6 version 8.7 plugin that can also work with the raw files of the D750. For direct support in Lightroom, you will need to upgrade (paying) to version 5.7.

  • When I go to "Selection tool" and "Type tool" for example. I can't go back in the selection tool again. It is locked.

    When I go to "Selection tool" and "Type tool" for example. I can't go back in the selection tool again. It is locked.

    I did a fresh install, and the programme of work of the normally.

  • Explain implicit and explicit curesor for example?

    Explain implicit and explicit curesor for example?

    Is less than the example of implicit cursor belogs?

    Set serverout
    Start
    I'm in (select * from emp)
    loop
    dbms_output.put_line (i.empno);
    end loop;
    end;

    All THE SQLs are analyzed and stored and runs under the cursor. These are stored in shared SQL database Pool.

    You can view the contents of the shared pool (and sliders inside it) using the V$ SQLAREA and other views of virtual performance.

    The concept of a cursor implicit and explicit cursor is a concept of LANGUAGE - not a SQL concept. It depends on how this language (client) (PL/SQL or Java or C/C++ or Delphi or c# and Perl, etc.) implements its interface language for SQL cursors created in Shared Pool data.

  • Questions about payment, cancellation, and planning for your review of VCA - VTC

    Fellow VCA (midshipman).

    I'm ready to go for my review of VCA - VTC, but I was interested in how you can pay for the exam and how much it costs. The last question, I think I found the answer to and $95, which is about €70? So, I registered for the exam with the assumption that I had to abandon in payment details before I confirmed. Unfortunately, I was already permanently registered after the first screen, I had to fill in and got an email sending me on the Pearson VIEW site. So I'm rather perplexed how me or maybe my employer can I pay for this review, if I can actually cancel it and if it is mandatory for me now to pass this review, now that I am registered of course for her.

    Kind regards

    Chris

    What you have said, I guess that the first site you visited was vmware.com and you completed the licensing process for the review.

    This operation generates an ID for you on VMware and PearsonVUE systems then this means that you can program review and pay via PearsonVUE.

    I don't think at this stage there is no obligation to actually go ahead and take the exam.

  • Question about AD commissioning and adding users to groups dynamically

    Hey all,.

    So I finally get the hang of the IOM and I know there are 100 ways to do something, but this is what I want to do, but do not know if it will work.

    Basically, when a new user is created in the IOM of a reconciliation of the trusted source, I rule of auto groups group memberships and access to the automobile policies in addition to the user AD resource. Based on the organization with which the user is, I have an adapter to prepopulate create them in a specific OU. A/d converters create user work flow starts and everything is good.

    Here is an example:

    Trusted source > IOM > belonging to a group of Auto > user AD Group > access policy > user AD.



    I am now all this a step further and also add the user to certain groups of ads, according to what OU, they are placed in. I can create another group, we'll call it user group AD Sales, create a strategy around this group and then prepopulate the group membership after a user is automatically created? Then

    SO:

    Trusted source > IOM > membership Auto Group (sales) > AD Sales User Group > access policy > AD refresh sales > add membership form.

    So I want the automatic provision a user, as well as dumping it in one ad group.

    Thanks, hope this fact to feel.

    Tony

    Remember to update the rules that supplies the objects of AD Group to take into account the value of the field 'Account AD' service.

    Your approach should work if each user can have zero or one role of AD groups and there is no field in the form of process AD is defined by a form of the object.

    Good luck
    / Martin

  • Protégé M200: Question about Vista TPM and the Bitlocker

    You want to know if I have a TPM chip to launch bitblocker on the M200 using Vista. Just call Toshiba Technical Support - the guy was totally distraught!

    If I don't have a TPM chip, then I can install it?

    Hello

    It is a good question. To my knowledge the Portege M200 does not support TPM but the Portege M400, R400 module and the R200

    But the Bitlocker, which is only available on the Vista Enterprise and ultimate can also be used without the TPM chip but then some functions are not available; for example, the SecureBoot.
    Without TPM only the OS volume encryption is available.
    In this case you can encrypts the entire Windows volume including both data and file system user, the file of hibernation, the page file and the temporary files.

    Using volume of BONE (HDD) encryption an encryption key will be generated. This key will be written to the USB and then the USB is required!

    AFAIK the Bitlocker requires 2 partitions. A partition must be created as needs at least 50 MB primary partition. I recommend a? about 1.5 GB
    The second big partition Vista can be installed.

    After the installation of Vista the Bitlocker must be enabled in Group Policy (units without secure TPM). This can be done using the command 'gpedit '.
    And then under models of administration-> windows components-> Bitlocker bitlocker function can be activated!

    Anyway, there is much information Bitlocker and I think you should also consult the Microsoft Web site for more information:
    BitLocker Windows Drive Encryption step-by-step guide

    I hope I could help a little

  • Question about importing RAW and JPEG

    Greetings,

    Support of Adobe at the following page talks about the "import and preferences file management Definition:

    http://help.Adobe.com/en_US/Lightroom/3.0/using/WSA66356E1-47C3-405f-8E7F-0FD7AAEB0575.htm l

    I have a few questions about this, and I would really appreciate your input gtting. (I also posted this question on this page, but I don't know if it was the right place...)

    1. that means 'otherwise, Lightroom treats the JPEG in doubles as a file? The JPEG format is not available at all in these circumstances? What can I do with this file "jpeg"? Is located in the same folder as the RAW images?

    2. my understanding is that the JPEG image contains the treatment unit (e.g., Vivid, sharpning, etc.); When I import pictures from my Nikon (NEF) raw, LR does not apply to these parameters, and the image I see in LR is * really * gross (i.e., bland and "free upgrades").  If I shoot and bring in JPEG and RAW, LR is it possible to have LR somehow automatically "make RAW image look like the JPEG?

    Thank you in advance!

    Zevi

    (1) No, it simply as an initial overview.  LR replaces this overview as soon as it renderes its own.

    (2) the profiles are already built for Nikon and Canon.  Look in the calibration Panel in the develop module.

  • questions about 2100 wlc and lwap connection

    Hello

    I have a few questions, I hope you can answer me.

    In fact, I have a controller connected to 3 Lwap 2100, and they are connected to the WCS. they have a static IP address.

    The distance between each point of access to more than 30 meters.

    1. the distance between the access points affect the accuracy or the stream. How can I calculate this

    2. because the APs use a static IP address, each customer should I connect to the network to add a static ip address? can I do the APs detect clients dynamically?

    I have lwapp reset guide, guide WCS configuration and configuration guide for WLC. If you have more documents or more pls share with me.

    BR

    Yamani

    Hello Ahmed,.

    Yes the distance between AP affect the wireless signal.

    to determine the best location of AP and the distance between them, as the site survey is necessary with the expert of the spectrum wireless or airmagnat...

    You can check the following links for design and RF management

    1) http://tools.cisco.com/squish/1Ea09

    2) http://tools.cisco.com/squish/51a58

    for other questions about DHCP, it is not a must that customer use static IP, you can always configure them with DHCP.

    as an extenal DHCP (like windows server), or on the WLC himself (to the title of controller-> > internal DHCP tab).

    Kind regards

    Talal

    =========
    Please note the answers that you find useful and mark as answer - when is it :-) - so that others can easily find

Maybe you are looking for