I am looking for a query to get the closest value exists to the average of all values. for example, I have in column A the values - 1 , 2, 8 The average is 3.667 . So i want the query to return 2 since it is the closest to the average value.
SELECT TOP 1 myTable.*
FROM myTable
CROSS JOIN (SELECT AVG(A) AS averageA FROM myTable) AS averageA
ORDER BY ABS(averageA.averageA - myTable.A)
SQL Fiddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With