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 Query Required !

SQL Query Required !

Scheduled Pinned Locked Moved Database
databasehelpquestion
5 Posts 3 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.
  • Z Offline
    Z Offline
    Zishan Haider
    wrote on last edited by
    #1

    Hello, I have a table Order. There is a field called "Symbol", another field called "Price",and several other fields. Now I want to select the minimum price records with distinct "Symbol". Id Symbol Price Volume Col1 Col2 Col3... --------------------------------------------------------------------- 1 AAA 19 1000 xxxx xxxx xxxx 2 AAA 40 4000 xxxx xxxx xxxx 3 CBC 150 50000 xxxx xxxx xxxx 4 CBC 149 4000 xxxx xxxx xxxx I want records 1 and 4 to be selected, since CBC has shortest price 149 and AAA has shortest price 19. Can anyone help me out in this regard? Thanks in advance

    D M 2 Replies Last reply
    0
    • Z Zishan Haider

      Hello, I have a table Order. There is a field called "Symbol", another field called "Price",and several other fields. Now I want to select the minimum price records with distinct "Symbol". Id Symbol Price Volume Col1 Col2 Col3... --------------------------------------------------------------------- 1 AAA 19 1000 xxxx xxxx xxxx 2 AAA 40 4000 xxxx xxxx xxxx 3 CBC 150 50000 xxxx xxxx xxxx 4 CBC 149 4000 xxxx xxxx xxxx I want records 1 and 4 to be selected, since CBC has shortest price 149 and AAA has shortest price 19. Can anyone help me out in this regard? Thanks in advance

      D Offline
      D Offline
      David Salter
      wrote on last edited by
      #2

      How about select distinct(symbol), min(price) from Order group by symbol


      Database FAQ

      Z 1 Reply Last reply
      0
      • D David Salter

        How about select distinct(symbol), min(price) from Order group by symbol


        Database FAQ

        Z Offline
        Z Offline
        Zishan Haider
        wrote on last edited by
        #3

        Thanks for the reply but the problem is not that simple.. I want to select the whole record not just 2 fields.. and when I select all records the distinct stops working.. I tried sub query.. didn't work..

        1 Reply Last reply
        0
        • Z Zishan Haider

          Hello, I have a table Order. There is a field called "Symbol", another field called "Price",and several other fields. Now I want to select the minimum price records with distinct "Symbol". Id Symbol Price Volume Col1 Col2 Col3... --------------------------------------------------------------------- 1 AAA 19 1000 xxxx xxxx xxxx 2 AAA 40 4000 xxxx xxxx xxxx 3 CBC 150 50000 xxxx xxxx xxxx 4 CBC 149 4000 xxxx xxxx xxxx I want records 1 and 4 to be selected, since CBC has shortest price 149 and AAA has shortest price 19. Can anyone help me out in this regard? Thanks in advance

          M Offline
          M Offline
          Mike Dimmick
          wrote on last edited by
          #4

          Hey, another place where my correlation trick is useful!

          SELECT Order.*
          FROM Order
          INNER JOIN
          (
          SELECT Symbol, MIN(Price) AS MinPrice
          FROM Order
          GROUP BY Symbol
          ) O2 ON Order.Symbol = O2.Symbol AND Order.Price = O2.MinPrice

          Caveat: this trick won't work directly if there's more than one row for a given Symbol with the minimum Price value. If there is you'll get every row with that price. Stability. What an interesting concept. -- Chris Maunder

          Z 1 Reply Last reply
          0
          • M Mike Dimmick

            Hey, another place where my correlation trick is useful!

            SELECT Order.*
            FROM Order
            INNER JOIN
            (
            SELECT Symbol, MIN(Price) AS MinPrice
            FROM Order
            GROUP BY Symbol
            ) O2 ON Order.Symbol = O2.Symbol AND Order.Price = O2.MinPrice

            Caveat: this trick won't work directly if there's more than one row for a given Symbol with the minimum Price value. If there is you'll get every row with that price. Stability. What an interesting concept. -- Chris Maunder

            Z Offline
            Z Offline
            Zishan Haider
            wrote on last edited by
            #5

            Hey.. this solves my problem.. Thanks a lot.. Best Regards, Zishan

            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