Skip to content

Instantly share code, notes, and snippets.

@mberman84
Created January 21, 2015 18:59
Show Gist options
  • Select an option

  • Save mberman84/5e883e3532051b2c7ae6 to your computer and use it in GitHub Desktop.

Select an option

Save mberman84/5e883e3532051b2c7ae6 to your computer and use it in GitHub Desktop.
def calculate_response_time
res = Message.connection.execute("
SELECT DATE(q2.created_at AT TIME ZONE '-08:00'), q2.response_time, q2.id, q2.customer_id FROM
(SELECT q1.*, (LEAD(created_at) OVER (PARTITION BY customer_id ORDER BY created_at) - q1.created_at) AS response_time FROM
(SELECT m.*, LEAD(direction) OVER (PARTITION BY customer_id ORDER BY created_at) AS next_direction FROM messages m
WHERE company_id = #{self.id} AND via_api = false AND mass_message_id IS NULL) AS q1
WHERE q1.direction <> q1.next_direction AND q1.next_direction IS NOT NULL) AS q2
WHERE q2.direction = 'received' ORDER BY created_at
")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment