Max from a group

Hi Gurus,
Want to find the max out of a group clause that has a sum. In the below eg want to get only the first row returned.
MANAGER 8275
select job,sum(sal) from scott.emp
group by job
order by sum(sal) desc
MANAGER 8275
ANALYST 6000
SALESMAN 5600
PRESIDENT 5000
CLERK 4150

You could also use RANK:
select job, sum(sal), rank() over (order by sum(sal) desc) sal_rank
from scott.emp
group by job
order by sum(sal) desc
JOB SUM(SAL) SAL_RANK
MANAGER 8275 1
ANALYST 6000 2
SALESMAN 5600 3
PRESIDENT 5000 4
CLERK 4150 5
Welcome to analytic SQL!
Or analytic MAX:
select *
from
select job, sum(sal) sum_sal, max(sum(sal)) over () max_sum_sal
from scott.emp
group by job
where sum_sal = max_sum_sal
JOB SUM_SAL MAX_SUM_SAL
MANAGER 8275 8275

Similar Messages

  • Selecting Max from group

    Hi,
    I have table say t1 like
    Manuf. model sr.no Date_out date_in Calib
    XYZ a100 125 6/28/2002 7/16/2002 7/8/2002
    XYZ a100 125 7/15/2003 --- ---
    ABC z100 120 4/20/2002 7/12/2002 7/8/2002
    ABC z100 120 7/22/2003 --- ---
    acs a000 100 1/28/2001 6/5/2002 7/8/2002
    acs a000 100 7/15/2002 --- ---
    pqr d14 12 1/1/2003 4/5/2002 4/5/2001
    Now I want out put like
    select manuf, model, sr_no, max(date_out), date_in, calib
    so can someone help me to write query

    select   manuf, model, sr_no, max(date_out), date_in, calib
    from     t1
    group by manuf, model, sr_no, date_in, calib

  • Get max min per group in sequence

    I have a list like this below with desired result, please help.
    The X are GPS cordinates, the time is the GPS times
    X    time
    A    5    
    B    6
    B    7
    C    8
    C    9
    A    10
    B    11
    The result must be:
    X    min    max
    A    5    5
    B    6    7
    C    8    9
    A    10    10
    B    11    11

    Here is a slightly diff solution, but you need SS 2012 or greater.
    The idea is expressed in each CTE.
    - Identify each new gps coordinate in time sequence per each car (new_gps).
    - Count how many new_gps you have for each row in specific partition and windows frame (grp_helper).
    - Aggregate to find min / max for each group (car, x, grp_helper).
    - Bring next minTime
    - APPLY to split the rows waiting and then moving
    SET NOCOUNT ON;
    USE tempdb;
    GO
    Create table #test (Car int, X Char(1), [Time] int)
    INSERT INTO #test
    VALUES
    ( 11, 'A', 5 ),
    ( 11, 'B', 6 ),
    ( 11, 'B', 7 ),
    ( 11, 'B', 8 ),
    ( 11, 'C', 9 ),
    ( 11, 'C', 10 ),
    ( 11, 'C', 11 ),
    ( 11, 'A', 12 ),
    ( 11, 'B', 13 );
    -- ( 12, 'A', 15 ),
    -- ( 12, 'B', 16 ),
    -- ( 12, 'B', 17 ),
    -- ( 12, 'B', 18 );
    WITH C1 AS (
    SELECT
    Car,
    X,
    [Time],
    CASE
    WHEN X = LAG(X, 1, NULL) OVER(PARTITION BY Car ORDER BY [Time]) THEN 0 ELSE 1
    END AS new_gps
    FROM
    #test
    , C2 AS (
    SELECT
    Car,
    X,
    [Time],
    SUM(new_gps) OVER(
    PARTITION BY Car
    ORDER BY [Time]
    ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
    ) AS grp_helper
    FROM
    C1
    Agg AS (
    SELECT
    Car,
    X,
    MIN([Time]) AS minTime,
    MAX([Time]) AS maxTime,
    COUNT(*) AS cnt
    FROM
    C2
    GROUP BY
    Car,
    X,
    grp_helper
    , C3 AS (
    SELECT
    Car,
    X,
    minTime,
    maxTime,
    cnt,
    LEAD(minTime, 1, maxTime) OVER(PARTITION BY Car ORDER BY minTime) AS nxt_minTime
    FROM
    Agg
    SELECT
    T.Car,
    T.X,
    T.minTime,
    T.maxTime,
    T.moving
    FROM
    C3
    CROSS APPLY
    SELECT C3.Car, C3.X, C3.minTime, C3.maxTime, 1 AS moving
    WHERE cnt = 1
    UNION ALL
    SELECT C3.Car, C3.X, C3.minTime, C3.maxTime, 0 AS moving
    WHERE cnt > 1
    UNION ALL
    SELECT C3.Car, C3.X, C3.maxTime, C3.nxt_minTime, 1 AS moving
    WHERE cnt > 1
    ) AS T
    ORDER BY
    T.Car,
    T.minTime;
    GO
    DROP TABLE #test;
    GO
    AMB
    Some guidelines for posting questions...
    AYÚDANOS A AYUDARTE, guía básica de consejos para formular preguntas

  • How can I move contact from one group to another?

    HOW CAN I MOVE A CONTACT FROM ONE GROUP TO ANOTHER GROUP?

    You usually make ​​contact groups via a computer (Mac or PC). If you use a mac and sync. via iCloud, you can go into Contacs.app and organize your contacts as you wish. Then (if) you sync with iCloud the groups you've changed or created will be displayed on your iPhone and (or) iPad.
    Hope this helped you.

  • How do I move contacts from one group to another?

    I entered several contacts in address book.  How do I control which group they are assigned to at the time of entry?  The ones I added showed up in my office contacts in Outlook instead of in Hotmail/iCloud.
    Is it possible to move them from one group to another once entered?

    Do the contacts that are not in iCloud belong to an external account such as Gmail, Yahoo or an Exchange account?  Or, do they belong to On My iPhone?

  • OneDrive for Business/SharePoint API - GET folders 'shared with me' if shared from a group/s.

    Does anybody have any ideas of how to get the list of folders shared with a user from a group?
    I can already retrieve files shared with a user from another user with:
    https://{tenant}-my.sharepoint.com/_api/search/query?querytext='(SharedWithUsersOWSUSER: {account_name} AND contentclass:STS_ListItem_MySiteDocumentLibrary)'
    but this end point does not show any of the files if they were shared from a group, is this possible with the REST API?
    Thanks for any help in advance.

    Hi SpringComp,
    You can change the root path for libraries you sync to your computer, though you can do this only if you’re not currently syncing any libraries. If you’re already syncing at least one library and you want to change the path, you must first
    stop syncing all libraries. Then, the first time you run the OneDrive for Business wizard to sync a library to your computer, you’ll see an option to change the location.
    More information, please refer to the link:
    http://office.microsoft.com/en-001/support/change-the-location-where-you-sync-sharepoint-libraries-on-your-computer-HA102893480.aspx
    I hope this helps.
    Thanks,
    Wendy
    Wendy Li
    TechNet Community Support

  • How do I move a contact from one group to another?

    On my device, or in iCloud on the web, how do I move a contact from one group to another.  This should be as easy as it is with calender items, but the only solution I've found is to change your default contact group, and email the contact to yourself.  Then tap the contact, and add it to your contact list.  You can then delete the original.  Is there an easier way???

    Hi rjwrjw11,
    Thanks for visiting Apple Support Communities.
    If you want a song to be listed under a different album in iTunes, you can edit the song information:
    Fixing Incorrect Song or Album Listings in iTunes
    http://support.apple.com/kb/TA24677
    Editing Track Information in iTunes
    To view and edit a song or video's info in iTunes:
    Click once on the track title to select it
    From the File menu, choose Get Info
    Click the Info tab
    Best,
    Jeremy

  • How can I transfer my contacts from one group to another?

    I have recently discovered that most of my personal contacts have been synced with my work's MS Outlook. This brings two issues to me if I ever had to change jobs and hand over my laptop - 1) My personal contacts will be shared with an unauthorised audience, 2) If they delete my account I will never have access to my own personal contacts as well.
    I tried to see how I can transfer contacts from one group to another but I can't seem to get the hang of it. Can anyone help, please? All I need to do is associate the contacts that have been associated with my work's email account to one of my personal accounts.
    Thank you in advance!
    Rgds
    Dan

    If you use an online syncing service for managing your contacts then you can simply clean up the contancts on one installation, and the changes should update on the second installation immediately. This will only work if you use iCloud, Gmail, or another service for storing your contacts.

  • Moving Contact From A Group.

    I have set up several Groups in my iPhone but some of my contacts are in "inappropriate" groups.
    How can I move a contact from one group to another without deleting and retyping?

    How did you set up the Groups on your iPhone?
    Are you syncing contacts with the Address Book application on your Mac?
    If so, place the contact in the appropriate Group in the Address Book on your Mac and then sync.

  • In contacts app how can I move a contact from one group to another group?

    I find I cannot move a contact from one group to another group. If I delete a contact from one group I delete that contact from my total contact population. I did not have this problem in earlier IOS versions. Can anyone help me?

    Thanks SO much, Barney.  How embarrassing, I could have sworn that I tried that first.
    I will definitely get them all moved over before I delete that account and I've already gone in & changed my preferences.  Several years ago, my husband I decided to share one calendar and one contacts list, using his mobile me account and it's been great for us.  I'm particularly thrilled to no longer serve as a human rolodex and I guess that I paniced a bit when I thought there might be a problem with our system.  I'm still confused where that "On my mac" account came from on the first place.  Do you suppose that was an automatic thing that came with the Mountain Lion upgrade?
    thanks again!

  • How do I move a contact or contacts from one group to another group?

    How do I move contacts from one group to another group on my iPhone?

    Hi ,
    In exchange you cannot have the same email address for multiple recipients .
    First and Best way is  to remove the email address from the old user account and add the same smtp address to the new user who is going to take his place.
    Second option would be forwarding all the emails from old user account to new user account when an email was triggered to the email address ([email protected]) .on this case there is no need to remove or delete the email address from the old user account
    and also email address ([email protected]) will still reside on the old user account .
    If i am at your place i will choose the first option and i agree with DJ
    Grijalva
    Please reply me if you have any queries .
    Regards
    S.Nithyanandham
    Thanks S.Nithyanandham

  • On my I phone how do I transfer contacts from one group to another then delete that group

    On the Iphone how do you transfer a contact from one group to another group the delete that group from your list of groups? I am on outlook, i know thats the first problem.
    John

    iPhone: Transferring information from your current iPhone to a new iPhone

  • How to tell if someone removed you from a group co...

    I'm new to skype so I apologize if this is a dumb question! When I go to "participants" it doesn't show I'm there, and the group hasn't been active in 3 days, which is odd since they are usually very active. Does it say you have been removed from the group or anything like that when the admin removes you? How can you tell if someone removes you? Thanks

    If your plugin is doing the exporting, just record which collections are in active sources upon export.
    PS - I'm not aware of any problems with catalog:getActiveSources(), other than differences in what's returned for the special collections (e.g. all-photos and recent-imports collections) vs. non-special collections.
    Of course you can't add photos to smart collections, so if collection is smart, consider an option for tagging in such a way that it will be auto-added (food for thought anyway, maybe doesn't apply...).
    Rob

  • Getting error while removinf user from AD group

    Hi,
    In AD User process definition, there is a default taks called :Remove user from Group. This task runs after another task called Organization Name Update . Whenever, an user is moved from one org to another org, his organization gets updated in AD user form and this task"Remove user from Group" runs. The work of this task is to remove the user from old groups. BUt the task is getting rejected and i see the below error in log files.
    11/07/04 00:24:17 Data AccessException:
    11/07/04 00:24:17 com.thortech.xl.orb.dataaccess.tcDataAccessException: DB_READ_FAILEDDetail: SQL: select UD_ADUSRC_GROUPNAME from UD_ADUSRC where UD_ADUSRC_KEY = Description: ORA-00936: missing expression
    SQL State: 42000Vendor Code: 936Additional Debug Info:com.thortech.xl.orb.dataaccess.tcDataAccessException
    at com.thortech.xl.dataaccess.tcDataAccessExceptionUtil.createException(Unknown Source)
    at com.thortech.xl.dataaccess.tcDataBase.createException(Unknown Source)
    at com.thortech.xl.dataaccess.tcDataBase.readPartialStatement(Unknown Source)
    at com.thortech.xl.dataobj.tcDataBase.readPartialStatement(Unknown Source)
    at com.thortech.xl.dataaccess.tcDataSet.executeQuery(Unknown Source)
    at com.thortech.xl.dataobj.tcDataSet.executeQuery(Unknown Source)
    at com.thortech.xl.dataaccess.tcDataSet.executeQuery(Unknown Source)
    at com.thortech.xl.dataobj.tcDataSet.executeQuery(Unknown Source)
    at com.thortech.xl.adapterfactory.events.tcAdpEvent.getChildTableFieldValue(Unknown Source)
    at com.thortech.xl.adapterfactory.events.tcAdpEvent.getRunTimeValue(Unknown Source)
    at com.thortech.xl.adapterfactory.events.tcAdpEvent.getRunTimeValue(Unknown Source)
    at com.thortech.xl.adapterGlue.ScheduleItemEvents.adpADREMOVEUSERFROMGROUP.implementation(adpADREMOVEUSERFROMGROUP.java:48)
    If anybody knows the solution for this then plz let me know.
    Thanks,
    Kalpana.

    I think the mappings and all would be correct. Here is what Kevin meant:
    - Let's assume the AD user account is a part of GroupA, GroupB and GroupC
    - Now on Change Organization completion if you invoke Remove user from Group then the adapter/process task has no way to know that which 3 of those groups has to be removed (or all 3 for your case)
    - Alternatively if you use API's to remove the group then this task would be invoked by the original OIM process/triggers and so the actual value would be known to adapter/process task.

  • Custom report for TOP 10 CPU Utilization machines from any group in aggregation last seven days

    I want to create a custom report that contain list of TOP CPU Utilization of machines form any group.This report is create on last 7 days CPU utilization of all machine from a group.
    What  should be query for this report.

    Hi,
    Please refer to the links below:
    SQL Query for TOP 10 Average CPU
    https://social.technet.microsoft.com/Forums/systemcenter/en-US/8d9a2d0d-8761-4d1f-b194-b24aa65172e1/sql-query-for-top-10-average-cpu?forum=operationsmanagerreporting
    How to use Report Builder to create custom reports in SCOM 2007
    http://www.systemcentercentral.com/how-to-use-report-builder-to-create-custom-reports-in-scom-2007/
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact [email protected]

Maybe you are looking for