Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL Group By Question

SQL Group By Question

Scheduled Pinned Locked Moved Database
databasequestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    loyal ginger
    wrote on last edited by
    #1

    I am trying to do this in Microsoft Access. I seem to be unable to construct a 'simple' query. To illustrate, here is a sample table:

    Field1 Field2 Amount
    A New 10
    D Old 5
    G Old 1
    H Old 15
    H New 55
    X Old 20
    Z Old 100
    Z New 20

    I need to group by Field1 and only return the Field2 data for the record that has the maximum number in the Amount field. I tried to use the 'First' function but Microsoft Access sort the records internally so it does not return the right value even if I pre-sort the records with a sub-query. Do you have any advice? The result should look like the table below. Desired Result:

    Field1 Field2 Max(Amount)
    A New 10
    D Old 5
    G Old 1
    H New 55
    X Old 20
    Z Old 100

    I have trouble getting the value of Field2 in the record containing to the maximum value of 'Amount.' Any suggestions? Thanks in advance for your time!

    B 1 Reply Last reply
    0
    • L loyal ginger

      I am trying to do this in Microsoft Access. I seem to be unable to construct a 'simple' query. To illustrate, here is a sample table:

      Field1 Field2 Amount
      A New 10
      D Old 5
      G Old 1
      H Old 15
      H New 55
      X Old 20
      Z Old 100
      Z New 20

      I need to group by Field1 and only return the Field2 data for the record that has the maximum number in the Amount field. I tried to use the 'First' function but Microsoft Access sort the records internally so it does not return the right value even if I pre-sort the records with a sub-query. Do you have any advice? The result should look like the table below. Desired Result:

      Field1 Field2 Max(Amount)
      A New 10
      D Old 5
      G Old 1
      H New 55
      X Old 20
      Z Old 100

      I have trouble getting the value of Field2 in the record containing to the maximum value of 'Amount.' Any suggestions? Thanks in advance for your time!

      B Offline
      B Offline
      Blue_Boy
      wrote on last edited by
      #2

      You can try this query

      SELECT mt.Field1,
      (
      SELECT TOP 1 mt2.Field2
      FROM myTable mt2
      WHERE mt2.Field1 = mt.Field1
      ORDER BY
      mt2.Amount DESC
      ) AS Field2,
      MAX(mt.Amount)
      FROM myTable mt
      GROUP BY
      mt.Field1


      I Love T-SQL "VB.NET is developed with C#.NET" If my post helps you kindly save my time by voting my post.

      L 1 Reply Last reply
      0
      • B Blue_Boy

        You can try this query

        SELECT mt.Field1,
        (
        SELECT TOP 1 mt2.Field2
        FROM myTable mt2
        WHERE mt2.Field1 = mt.Field1
        ORDER BY
        mt2.Amount DESC
        ) AS Field2,
        MAX(mt.Amount)
        FROM myTable mt
        GROUP BY
        mt.Field1


        I Love T-SQL "VB.NET is developed with C#.NET" If my post helps you kindly save my time by voting my post.

        L Offline
        L Offline
        loyal ginger
        wrote on last edited by
        #3

        Thanks for your time!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups