Select help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aechosti
    New Member
    • Nov 2007
    • 1

    Select help

    I have a table

    ID(int auto) MemberCount(int ) Rate(decimal(8, 2))
    1 0 150.00
    2 21 175.00
    3 31 200.00
    4 41 225.00

    I need a query where I will pass in @membercount and get back the closest matching row.

    Say member count is actually 25 when this is passed to the query I want to get back the row with id = 2.

    Thanks in advance
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    A crude way....

    [CODE=sql]select * from tablename where ID = (select max(ID) from tablename
    where memberCount <= @memberCount)[/CODE]

    Have you done anything in it as yet?

    Comment

    • amitpatel66
      Recognized Expert Top Contributor
      • Mar 2007
      • 2358

      #3
      Originally posted by aechosti
      I have a table

      ID(int auto) MemberCount(int ) Rate(decimal(8, 2))
      1 0 150.00
      2 21 175.00
      3 31 200.00
      4 41 225.00

      I need a query where I will pass in @membercount and get back the closest matching row.

      Say member count is actually 25 when this is passed to the query I want to get back the row with id = 2.

      Thanks in advance
      In case if the data is something like this:

      ID(int auto) MemberCount(int ) Rate(decimal(8, 2))
      1 0 150.00
      2 21 175.00
      3 29 200.00
      4 31 200.00
      5 41 225.00
      6 51 225.00

      What would you expect as output if 25 is paased as input value?
      21 or 29?

      Comment

      Working...