Skip to content

Instantly share code, notes, and snippets.

@mberman84
Created January 21, 2015 07:49
Show Gist options
  • Select an option

  • Save mberman84/607909bc9a0b2c42917f to your computer and use it in GitHub Desktop.

Select an option

Save mberman84/607909bc9a0b2c42917f 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 AS q3 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'
")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment