Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL select a column2 based on values in column1 in two rows

Tags:

sql

sql-server

I have a table like this:

To_day     |   percent
-----------------------
  5        |     1.5

  8        |      2

2147483647 |      3 

I want to write a SELECT that take a day, find fitted range in To_day column and give back Related percent. for example:

@day = 3 -------> percent = 1.5

@day = 5 -------> percent = 1.5

@day = 7 -------> percent = 2

@day = 15 ------> percent = 3
like image 403
Mohsen Avatar asked Nov 20 '25 19:11

Mohsen


1 Answers

Try this:

SELECT TOP 1 [percent]
FROM mytable
WHERE To_day >= @d
ORDER BY to_day 
like image 168
Giorgos Betsos Avatar answered Nov 22 '25 08:11

Giorgos Betsos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!