SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Order results from middle outward?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

TheKai
Starting Member

16 Posts

Posted - 06/08/2013 :  04:18:50  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 06/08/2013 :  04:27:58  Show Profile  Reply with Quote

SELECT COL
FROM TABLE
ORDER BY ABS(COL-3),COL DESC


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

TheKai
Starting Member

16 Posts

Posted - 06/08/2013 :  16:35:23  Show Profile  Reply with Quote
I have no idea why or how it works, but it's bloody perfect! Thank you for your insight!
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.14 seconds. Powered By: Snitz Forums 2000