-1

Please assist me, I need to pick 790 since its in middle but not able to figure it out.

id  Value
12  780
123 796
124  790

Thank you in advance

6
  • 1
    You've been an SO member for 5 years. You should know this is not a high quality post.
    – dfundako
    Commented Sep 20, 2018 at 13:26
  • 1
    Please tell us which database you are using (e.g. MySQL, SQL Server, Oracle, Postgres, etc.). Commented Sep 20, 2018 at 13:26
  • I am sorry @dfundako, I am using SQL database Tim Commented Sep 20, 2018 at 13:35
  • @user2711165 The question is which one?
    – Magnus
    Commented Sep 20, 2018 at 13:38
  • For SQL server see: stackoverflow.com/questions/1342898/…
    – Magnus
    Commented Sep 20, 2018 at 13:39

1 Answer 1

0

This should help to get what you want. Get the average value from your table, then get first record with smallest difference with average value.

SELECT TOP 1 id, value FROM yourTable
ORDER BY ABS(value - (SELECT AVG(value) FROM yourTable)) 

or using variables, depending on the size of your table

DECLARE @median DECIMAL
SELECT @median = AVG(value) FROM yourTable

SELECT TOP 1 * FROM yourTable 
ORDER BY ABS( value - @median) 

Not the answer you're looking for? Browse other questions tagged or ask your own question.