sql - rails order by time with reversed 24 hour periods -


i have model stores events each have start time.

the start time handled rails time, stored in postgres datetime (i'm assuming) , rails ignores date , stores 2000-01-01...

the problem have ordering events start after midnight show afterwards , not before. how can sort them differently , split events in 24 hour period first morning 12 hour events show after second half of evening events. (if makes since)

any ideas?

c=event.last c.event_items.order(:time_start) 

which order \"event_items\".time_start asc"

+-----+----------+-----------+-------------------------+---------+-------------------------+-------------------------+ | id  | event_id | artist_id | time_start              | area_id | created_at              | updated_at              | +-----+----------+-----------+-------------------------+---------+-------------------------+-------------------------+ | 155 | 63       | 111       | 2000-01-01 00:40:00 utc |         | 2013-08-24 21:21:57 utc | 2013-08-26 00:07:44 utc | | 153 | 63       | 133       | 2000-01-01 01:10:00 utc |         | 2013-08-24 21:21:57 utc | 2013-08-26 00:07:44 utc | | 152 | 63       | 128       | 2000-01-01 02:00:00 utc |         | 2013-08-24 21:21:57 utc | 2013-08-26 00:07:44 utc | | 151 | 63       | 148       | 2000-01-01 22:10:00 utc |         | 2013-08-24 21:21:57 utc | 2013-08-26 00:07:44 utc | | 194 | 63       | 124       | 2000-01-01 23:00:00 utc |         | 2013-08-26 00:07:44 utc | 2013-08-26 00:07:44 utc | | 154 | 63       | 98        | 2000-01-01 23:50:00 utc |         | 2013-08-24 21:21:57 utc | 2013-08-26 00:07:44 utc | +-----+----------+-----------+-------------------------+---------+-------------------------+-------------------------+ 

i dates before 12h (24h clock) come after 12h+...

eg in example order 22:10, 23:00, 23:50, 00:40, 01:10, 02:00

there date_part function in postgres think this

event.order("cast(date_part('hour', time_start) integer)/12 desc,              cast(date_part('hour', time_start) integer)%12 asc") 

if want divide time in different modular try different divider.

update

i guess need elaborate bit.

basically extract hour part of time_start timestamp , convert (or cast) integer want work integer not time. if divide hour 12 give interval, have on link postgres doc above more details.

so first expression cast(date_part('hour', time_start) integer)/12 give either 0 or 1; 0 if hour before 12 , 1 12 onwards. want, put hours after 12 on top of hours after 12. sorted 23 > 12, 11 > 0 though, not want.

hence second cast(date_part('hour', time_start) integer)%12. give use 23 => 11, 22 => 10 ... 12 => 0 , 11 => 11 ... 0 => 0 etc. can sort them in ascending order.

by way, doesn't sort minutes may want add time_start asc third sort criteria. think add cast(date_part('hour', time_start) integer) select statement , name refer name on order by.

hope :)


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -