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. between max() and min()

between max() and min()

Scheduled Pinned Locked Moved Database
comtutorialquestion
4 Posts 4 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.
  • N Offline
    N Offline
    nelsonpaixao
    wrote on last edited by
    #1

    Hi, how can i get info on the 2nd max(id) or 3rd min(money) record on a table. I only know how to get min(X) or max(X) record on a table Thanks ;P

    nelsonpaixao@yahoo.com.br

    T B M 3 Replies Last reply
    0
    • N nelsonpaixao

      Hi, how can i get info on the 2nd max(id) or 3rd min(money) record on a table. I only know how to get min(X) or max(X) record on a table Thanks ;P

      nelsonpaixao@yahoo.com.br

      T Offline
      T Offline
      TheFM234
      wrote on last edited by
      #2

      Select Max(id)
      From table
      where id != (Select Max(id) From table)

      The above will give you the second maximum id.

      1 Reply Last reply
      0
      • N nelsonpaixao

        Hi, how can i get info on the 2nd max(id) or 3rd min(money) record on a table. I only know how to get min(X) or max(X) record on a table Thanks ;P

        nelsonpaixao@yahoo.com.br

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

        Another way: select top 1 ColumName from TableName where ColumName < (select max(ColumName ) from TableName) order by TableName desc


        I Love T-SQL "Don't torture yourself,let the life to do it for you."

        1 Reply Last reply
        0
        • N nelsonpaixao

          Hi, how can i get info on the 2nd max(id) or 3rd min(money) record on a table. I only know how to get min(X) or max(X) record on a table Thanks ;P

          nelsonpaixao@yahoo.com.br

          M Offline
          M Offline
          MatthysDT
          wrote on last edited by
          #4

          To select the 5th lowest (or n-th lowest) replace the "5" in this query with the number you want.

          Select max(X) from
          (Select top 5 id as X from idTable order by id asc)t

          To select the n-th highest replace the "5" in this query with the number you want.

          Select min(X) from
          (Select top 5 id as X from idTable order by id desc) t

          Cheers!

          Doggy treat[^]

          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