+ Reply to Thread
Results 1 to 4 of 4

How to select data based on (highest) value in a paticular column (SQL)

  1. #1
    Registered User
    Join Date
    11-03-2014
    Location
    Copenhagen, Denmark
    MS-Off Ver
    2013
    Posts
    18

    How to select data based on (highest) value in a paticular column (SQL)

    Hi all

    A question from a SQL newbie

    I have the following dataset in access

    KOITNO KOCSU5 KOPCDT
    2 20 20160428
    2 22 20160429
    2 24 20160519
    3 12 20160514
    3 13 20160515

    Here I would like to write an sql so that I only get the line with the highest value in the KOPCDT (date) column (text format) grouped by KOITNO (Itemnumber) in my new dataset. In this case:

    2 24 20160519
    3 13 20160515

    I have tried several Things but can't seem to get just right

    Anyone with a quick solution?

    Thanks in advance!

    /JJ

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2404 Win 11 Home 64 Bit
    Posts
    23,871

    Re: How to select data based on (highest) value in a paticular column (SQL)

    This should do it:

    SELECT Sheet1.KOITNO, Max(Sheet1.KOPCDT) AS MaxOfKOPCDT
    FROM Sheet1
    GROUP BY Sheet1.KOITNO;
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    11-03-2014
    Location
    Copenhagen, Denmark
    MS-Off Ver
    2013
    Posts
    18

    Re: How to select data based on (highest) value in a paticular column (SQL)

    Hi alansidman

    Thanks for your reply

    Unfortunately, this doesn't quite Work. Sure enough only the highest value of KOPCDT is shown... but the issue is then KOCSU5.

    If I select 'Group by' on the KOCSU5 then I still get multiple lines... if I choose 'sum' it shows me the sumtotal of all lines (in the above example 20+22+24 = 66)
    It looks right if I choose 'last' as criteria but I'm not entirely sure if this is right? Maybe you're able to advise me?

    My SQL is now:
    SELECT M3PRDDTA_MCHEAD.KOITNO, Max(M3PRDDTA_MCHEAD.KOPCDT) AS MaxOfKOPCDT, Last(M3PRDDTA_MCHEAD.KOCSU5) AS LastOfKOCSU5
    FROM M3PRDDTA_MCHEAD
    GROUP BY M3PRDDTA_MCHEAD.KOITNO;

    BR

    JJ

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2404 Win 11 Home 64 Bit
    Posts
    23,871

    Re: How to select data based on (highest) value in a paticular column (SQL)

    Last will not give you the max value, it will give you the last entry made? You may need to run a second query using the first query as your record source, but if you are getting the proper result, then maybe you are right.
    Alternative solution?:
    Join the first query to the original table adding only the KOCSU5 from the table to the other two fields.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 06-09-2016, 12:51 PM
  2. [SOLVED] Need to select earliest five rows matching highest value, and next highest if not enough
    By Ochimus in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 05-05-2016, 11:26 AM
  3. [SOLVED] Identifying data in one column based on highest figure data in another column
    By Doofus1 in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 06-21-2014, 02:31 PM
  4. Macro to select rows with respect to highest value in column
    By gokzee in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-05-2012, 09:41 PM
  5. Replies: 2
    Last Post: 10-26-2009, 06:43 AM
  6. [SOLVED] Select paticular cells
    By vijaya in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-18-2005, 12:30 PM
  7. [SOLVED] Select paticular cells
    By vijaya in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-02-2005, 06:17 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1