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. group by, max and another column

group by, max and another column

Scheduled Pinned Locked Moved Database
questiontutorial
2 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.
  • S Offline
    S Offline
    swjam
    wrote on last edited by
    #1

    Hi, Say I have the table below: Tabl1 PKey ColA ColB ColC 1 a1 b1 c1 2 a1 b2 c2 3 a1 b3 c3 ... a2 ... ... ... a3 ... ... If I group by ColA with the select statement as follows: SELECT ColA, MAX(ColB), FROM Tabl1 GROUP BY ColA If for example for a1 the MAX(ColB) is b2, THEN I would like to include ColC in the select statement such that for a1 the resulting row would be: ColA ColB ColC a1 b2 c2 What is the best way to do this? TIA!

    I am a SysAdmin, I battle my own daemons.

    A 1 Reply Last reply
    0
    • S swjam

      Hi, Say I have the table below: Tabl1 PKey ColA ColB ColC 1 a1 b1 c1 2 a1 b2 c2 3 a1 b3 c3 ... a2 ... ... ... a3 ... ... If I group by ColA with the select statement as follows: SELECT ColA, MAX(ColB), FROM Tabl1 GROUP BY ColA If for example for a1 the MAX(ColB) is b2, THEN I would like to include ColC in the select statement such that for a1 the resulting row would be: ColA ColB ColC a1 b2 c2 What is the best way to do this? TIA!

      I am a SysAdmin, I battle my own daemons.

      A Offline
      A Offline
      andyharman
      wrote on last edited by
      #2

      select b.* from ( select ColA, Max(ColB) as MaxColB from Tabl1 group by ColA ) as a Tabl1 as b on b.ColA = a.ColA and b.ColB = a.ColB order by b.ColA

      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