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. SELECT MIN

SELECT MIN

Scheduled Pinned Locked Moved Database
questiondatabase
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.
  • J Offline
    J Offline
    Jaydeanster
    wrote on last edited by
    #1

    Hello, Is there a way to select a row from a table based on a minimum? Or what is the correct syntax? This query returns two rows when I only need a row with the min column... :( select min(col1ID), col2ID, col3 from tbl where col2ID = 'xxxx' group by col2ID, col3 Thanks, jay

    C 1 Reply Last reply
    0
    • J Jaydeanster

      Hello, Is there a way to select a row from a table based on a minimum? Or what is the correct syntax? This query returns two rows when I only need a row with the min column... :( select min(col1ID), col2ID, col3 from tbl where col2ID = 'xxxx' group by col2ID, col3 Thanks, jay

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Jaydeanster wrote: This query returns two rows when I only need a row with the min column... select min(col1ID), col2ID, col3 from tbl where col2ID = 'xxxx' group by col2ID, col3 Your GROUP BY clause causes more than one row to be returned. I am guessing that you want the row with the MIN(col1ID) with the other columns too. So, assuming that each col1ID value is unique then the following should work:

      SELECT m.col1ID, t.col2ID, t.col3
      FROM tbl AS t
      INNER JOIN (SELECT MIN(col1ID) as col1ID
      FROM tbl
      WHERE col2ID = 'xxxx') AS m ON m.col1ID = t.col1ID
      WHERE col2ID = 'xxxx'

      Does this help?


      My: Blog | Photos | Next SQL Presentation WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More

      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