I have an events list of events that are ongoing or upcoming. That is, an event stays on the list as long as it is still happening, then disappears once it's over. I'd like the list to sort so that the closest starting or ending date is at the top.

E.g., given the following events as of April 30:

  1. May 1 to May 31
  2. May 6 to May 6
  3. May 16 to May 20
  4. May 19 to May 19

I'd like the following sort:

April 30 through May 3 same as above

May 4 through May 6:

  1. May 6 to May 6
  2. May 1 to May 31
  3. May 16 to May 20
  4. May 19 to May 19

May 7 and May 8:

  1. May 1 to May 31
  2. May 16 to May 20
  3. May 19 to May 19

May 9:

  1. May 16 to May 20
  2. May 1 to May 31
  3. May 19 to May 19

May 10: Same as May 9 or same as May 11 through 17

May 11 through 17:

  1. May 16 to May 20
  2. May 19 to May 19
  3. May 1 to May 31

May 18 and 19:

  1. May 19 to May 19
  2. May 16 to May 20
  3. May 1 to May 31

May 20:

  1. May 16 to May 20
  2. May 1 to May 31

May 21 to 31:

  1. May 1 to May 31

Views gives me the following SQL:

SELECT node.nid AS nid,
   node_data_field_time.field_event_image_tile_fid AS node_data_field_time_field_event_image_tile_fid,
   node_data_field_time.field_event_image_tile_list AS node_data_field_time_field_event_image_tile_list,
   node_data_field_time.field_event_image_tile_data AS node_data_field_time_field_event_image_tile_data,
   node.type AS node_type,
   node.vid AS node_vid,
   node.title AS node_title,
   node_data_field_time.field_time_value AS node_data_field_time_field_time_value,
   node_data_field_time.field_time_value2 AS node_data_field_time_field_time_value2,
   node_revisions.teaser AS node_revisions_teaser,
   node_revisions.format AS node_revisions_format
 FROM node node 
 LEFT JOIN content_type_event node_data_field_time ON node.vid = node_data_field_time.vid
 LEFT JOIN node_revisions node_revisions ON node.vid = node_revisions.vid
 WHERE ((node.type in ('event')) AND (node.status = 1))
    AND (DATE_FORMAT(node_data_field_time.field_time_value2, '%Y-%m-%d') >= '2012-04-30')
   ORDER BY node_data_field_time_field_time_value ASC, node_data_field_time_field_time_value2 ASC

I'd like to change that to:

SELECT node.nid AS nid,
   node_data_field_time.field_event_image_tile_fid AS node_data_field_time_field_event_image_tile_fid,
   node_data_field_time.field_event_image_tile_list AS node_data_field_time_field_event_image_tile_list,
   node_data_field_time.field_event_image_tile_data AS node_data_field_time_field_event_image_tile_data,
   node.type AS node_type,
   node.vid AS node_vid,
   node.title AS node_title,
   node_data_field_time.field_time_value AS node_data_field_time_field_time_value,
   node_data_field_time.field_time_value2 AS node_data_field_time_field_time_value2,
   node_revisions.teaser AS node_revisions_teaser,
   node_revisions.format AS node_revisions_format,

LEAST(ABS(DATEDIFF(node_data_field_time.field_time_value, '2012-04-30')),ABS(DATEDIFF(node_data_field_time.field_time_value2,'2012-04-30'))) AS view_time_field_distance

FROM node node 
 LEFT JOIN content_type_event node_data_field_time ON node.vid = node_data_field_time.vid
 LEFT JOIN node_revisions node_revisions ON node.vid = node_revisions.vid
 WHERE ((node.type in ('event')) AND (node.status = 1))
    AND (DATE_FORMAT(node_data_field_time.field_time_value2, '%Y-%m-%d') >= '2012-04-30')

ORDER BY view_time_field_distance ASC,

node_data_field_time_field_time_value ASC, node_data_field_time_field_time_value2 ASC

How do I accomplish this in Drupal 6?

Comments

Kuldip Gohil’s picture

Have you tried hook_views_query_alter?

More detail with example available at - http://www.appnovation.com/using-hook-views-query-alter

Thank you,
Kuldip Gohil