Created
January 20, 2015 17:28
-
-
Save mberman84/24c7be63341924b3763e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def calculate_response_time | |
Message.connection.execute(" | |
SELECT AVG(q3.response_time) AS average, DATE(created_at AT TIME ZONE '-08:00') AS day FROM | |
(SELECT q2.* FROM | |
(SELECT q1.*, q1.created_at - LEAD(created_at) OVER (PARTITION BY customer_id ORDER BY 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 = 2 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') AS q3 | |
GROUP BY DATE(created_at AT TIME ZONE '-08:00') | |
") | |
end | |
RESULTED IN: | |
#<PG::Result:0x0000010b3019f0 @connection=#<PG::Connection:0x0000010b50e0e0 @notice_processor=nil, @notice_receiver=nil, @socket_io=nil>> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment