Given that I don't have PostGres, I tried it in Oracle. ... I hate Oracle's timestamp/datestamp stuff (probably because I haven't used it enough) - but here goes:
select id, event_time
from table
order by to_char(sysdate+(to_timestamp(to_char(event_time,'MM/DD/YYYY'
+)||' '||to_char(systimestamp,'HH24:MI'), 'MM/DD/YYYY HH24:MI')
+1-event_time),'HH24:MI') desc;
Basically if you get through all the conversion crap, it should look like this:
select id, event_time
from table
order by to_char(systimestamp+1-event_time,'HH24:MI') desc;
Which is basically saying, add a day to wherever you are now. Then subtract the time only portion (event_time) from that, and sort in descending order (largest to smallest). The idea is that anything that is the farthest away from tomorrow at this time, will be the closest thing to today at this time, because the window is 24 hours and your event_time only has a range of 24 hours.
(e.g. if it's 5PM right now and you have a job that runs at 4PM and one at 6PM and one at 1AM... tomorrow at 5PM it will be 14 hours difference between 1 AM, 23 hours for the 6PM and 1 hour for the 4PM - so , the order is 23, 14, 1 or 5PM, 1AM, 4PM - Capice? =)
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.