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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Query with time sorting...
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

NewMedia42
Starting Member

35 Posts

Posted - 03/08/2014 :  18:10:34  Show Profile  Reply with Quote
I have an interesting problem - right now I have a table which has a pool of things that need to be done. It's important that these things NOT be done in insert order, so right now I simple do an "ORDER BY newid()" so I get some random value. The problem with this is that we still need things done in a somewhat time sensitive way - meaning that the older something is, the more likely it would be to be selected.

I can get around this manually in the application, by just getting the oldest item also, then using some weighted value for oldest vs random - but my question is, is there some alternative method I could use, other than ORDER BY newid(), that would still give random returns, but could also use a timedate column to give precedence to older item.

Thanks for any insight anyone has!

k_a_f_k_a
Starting Member

Belgium
2 Posts

Posted - 03/09/2014 :  07:59:58  Show Profile  Reply with Quote
Something like this perhaps?
select *
from mytable
order by datediff(d, mytimedatecolumn, getdate()) + abs(checksum(NEWID())) % 1000 desc
You can in/decrease that number, depending on how much variance you want in your results.
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.03 seconds. Powered By: Snitz Forums 2000