Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Order By Closest to A Number

Author  Topic 

mortoleeuk
Starting Member

5 Posts

Posted - 2007-11-30 : 12:24:25
Hi,

I have a query which returns decimals.

i want to return in an order which is based on how close the value is to a particular value.

So for the following results:

5.47
5.60
10.00
1.50


I want to return them in order of closest to 6.00 so it would return this instead...

5.60 closest to 6.00 (0.40 away)
5.47 (0.53 away)
1.50 (3.50 away)
10.00 furthest from 6.00 (4.00 away)


Thanks for your help.

Cheers,
Lee

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2007-11-30 : 12:52:30
Table1
Value
5.47
5.60
10.00
1.50

SELECT Value,(Value-6) AS 'ordervalue'
FROM Table
ORDER BY ordervalue
Go to Top of Page
   

- Advertisement -