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
 General SQL Server Forums
 New to SQL Server Programming
 Order results from middle outward?

Author  Topic 

TheKai
Starting Member

16 Posts

Posted - 2013-06-08 : 04:18:50
Thanks for looking at my question.

I have a data (result) set that looks like this

COL
---
1
2
2
3
3
4
5
---

I would like my query to return the data in the following order:

---
3
3
4
2
2
5
1
---

Basically, if there is a 3 in the data, I want that record (3s are best). If there are no 3s, then I want the next closest thing (either 4s or 2s).

Is this possible?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-06-08 : 04:27:58
[code]
SELECT COL
FROM TABLE
ORDER BY ABS(COL-3),COL DESC
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

TheKai
Starting Member

16 Posts

Posted - 2013-06-08 : 16:35:23
I have no idea why or how it works, but it's bloody perfect! Thank you for your insight!
Go to Top of Page
   

- Advertisement -