Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DROP TABLE IF EXISTS messages;
- DROP TABLE IF EXISTS chats;
- CREATE TABLE chats (
- client integer NOT NULL,
- provider integer NOT NULL,
- PRIMARY KEY (client, provider)
- );
- CREATE TABLE messages (
- id serial PRIMARY KEY,
- client integer NOT NULL,
- provider integer NOT NULL,
- sender integer NOT NULL CHECK(sender = client OR sender = provider),
- created_at timestamp with time zone NOT NULL,
- FOREIGN KEY (client, provider) REFERENCES chats(client, provider) ON UPDATE CASCADE ON DELETE CASCADE
- );
- INSERT INTO chats VALUES (1,2), (3,2), (1,3);
- INSERT INTO messages (client, provider, sender, created_at) VALUES
- (1, 2, 1, '2017-06-13 17:00:00+0'),
- (1, 2, 1, '2017-06-13 17:00:10+0'),
- (1, 2, 1, '2017-06-13 17:01:00+0'),
- (1, 2, 2, '2017-06-13 17:10:00+0'),
- (1, 2, 2, '2017-06-13 17:10:10+0'),
- (1, 2, 2, '2017-06-13 17:10:20+0'),
- (1, 2, 1, '2017-06-13 17:11:00+0'),
- (1, 2, 2, '2017-06-13 17:11:10+0');
- INSERT INTO messages (client, provider, sender, created_at) VALUES
- (3, 2, 2, '2017-06-13 17:05:00+0'),
- (3, 2, 2, '2017-06-13 17:05:10+0'),
- (3, 2, 2, '2017-06-13 17:05:20+0'),
- (3, 2, 3, '2017-06-13 17:12:00+0'),
- (3, 2, 2, '2017-06-13 17:12:10+0'),
- (3, 2, 2, '2017-06-13 17:12:15+0'),
- (3, 2, 3, '2017-06-13 17:12:30+0'),
- (3, 2, 3, '2017-06-13 17:14:00+0');
- INSERT INTO messages (client, provider, sender, created_at) VALUES
- (1, 3, 1, '2017-06-13 17:05:00+0'),
- (1, 3, 1, '2017-06-13 17:05:10+0');
- postgres=# table messages order by client, provider, created_at;
- id | client | provider | sender | created_at
- ----+--------+----------+--------+------------------------
- 1 | 1 | 2 | 1 | 2017-06-13 13:00:00-04
- 2 | 1 | 2 | 1 | 2017-06-13 13:00:10-04
- 3 | 1 | 2 | 1 | 2017-06-13 13:01:00-04
- --
- 4 | 1 | 2 | 2 | 2017-06-13 13:10:00-04
- 5 | 1 | 2 | 2 | 2017-06-13 13:10:10-04
- 6 | 1 | 2 | 2 | 2017-06-13 13:10:20-04
- --
- 7 | 1 | 2 | 1 | 2017-06-13 13:11:00-04
- --
- 8 | 1 | 2 | 2 | 2017-06-13 13:11:10-04
- --
- 17 | 1 | 3 | 1 | 2017-06-13 13:05:00-04
- 18 | 1 | 3 | 1 | 2017-06-13 13:05:10-04
- --
- 9 | 3 | 2 | 2 | 2017-06-13 13:05:00-04
- 10 | 3 | 2 | 2 | 2017-06-13 13:05:10-04
- 11 | 3 | 2 | 2 | 2017-06-13 13:05:20-04
- --
- 12 | 3 | 2 | 3 | 2017-06-13 13:12:00-04
- --
- 13 | 3 | 2 | 2 | 2017-06-13 13:12:10-04
- 14 | 3 | 2 | 2 | 2017-06-13 13:12:15-04
- --
- 15 | 3 | 2 | 3 | 2017-06-13 13:12:30-04
- 16 | 3 | 2 | 3 | 2017-06-13 13:14:00-04
- (18 rows)
- client | provider | sender | first_created_at
- --------+----------+--------+------------------------
- 1 | 2 | 1 | 2017-06-13 13:00:00-04
- 1 | 2 | 2 | 2017-06-13 13:10:00-04
- 1 | 2 | 1 | 2017-06-13 13:11:00-04
- 1 | 2 | 2 | 2017-06-13 13:11:10-04
- 1 | 3 | 1 | 2017-06-13 13:05:00-04
- 3 | 2 | 2 | 2017-06-13 13:05:00-04
- 3 | 2 | 3 | 2017-06-13 13:12:00-04
- 3 | 2 | 2 | 2017-06-13 13:12:10-04
- 3 | 2 | 3 | 2017-06-13 13:12:30-04
- postgres=# SELECT * FROM messages WHERE (client, provider) = (1,2) ORDER BY created_at;
- id | client | provider | sender | created_at
- ----+--------+----------+--------+------------------------
- 1 | 1 | 2 | 1 | 2017-06-13 13:00:00-04
- 2 | 1 | 2 | 1 | 2017-06-13 13:00:10-04
- 3 | 1 | 2 | 1 | 2017-06-13 13:01:00-04
- --
- 4 | 1 | 2 | 2 | 2017-06-13 13:10:00-04
- 5 | 1 | 2 | 2 | 2017-06-13 13:10:10-04
- 6 | 1 | 2 | 2 | 2017-06-13 13:10:20-04
- --
- 7 | 1 | 2 | 1 | 2017-06-13 13:11:00-04
- --
- 8 | 1 | 2 | 2 | 2017-06-13 13:11:10-04
- (8 rows)
- postgres=# SELECT * FROM messages WHERE (client, provider) = (3,2) ORDER BY created_at;
- id | client | provider | sender | created_at
- ----+--------+----------+--------+------------------------
- 9 | 3 | 2 | 2 | 2017-06-13 13:05:00-04
- 10 | 3 | 2 | 2 | 2017-06-13 13:05:10-04
- 11 | 3 | 2 | 2 | 2017-06-13 13:05:20-04
- --
- 12 | 3 | 2 | 3 | 2017-06-13 13:12:00-04
- --
- 13 | 3 | 2 | 2 | 2017-06-13 13:12:10-04
- 14 | 3 | 2 | 2 | 2017-06-13 13:12:15-04
- --
- 15 | 3 | 2 | 3 | 2017-06-13 13:12:30-04
- 16 | 3 | 2 | 3 | 2017-06-13 13:14:00-04
- (8 rows)
- postgres=# SELECT * FROM messages WHERE (client, provider) = (1,3) ORDER BY created_at;
- id | client | provider | sender | created_at
- ----+--------+----------+--------+------------------------
- 17 | 1 | 3 | 1 | 2017-06-13 13:05:00-04
- 18 | 1 | 3 | 1 | 2017-06-13 13:05:10-04
- (2 rows)
- SELECT client, provider, sender, min(created_at) AS first_created_at
- FROM (
- SELECT count(CASE WHEN is_reset THEN 1 END) OVER (ORDER BY created_at) AS grp,
- client,
- provider,
- sender,
- created_at
- FROM (
- SELECT
- (client,provider,sender) <> lag( (client,provider,sender) ) OVER (ORDER BY created_at) AS is_reset,
- client,
- provider,
- sender,
- created_at
- FROM messages
- ) AS t1
- ) AS t2
- GROUP BY grp, client, provider, sender
- HAVING count(*) > 1;
- SELECT
- (client,provider,sender) <> lag( (client,provider,sender) ) OVER (ORDER BY created_at) AS is_reset,
- client,
- provider,
- sender,
- created_at
- FROM messages
- is_reset | client | provider | sender | created_at
- ----------+--------+----------+--------+------------------------
- | 1 | 2 | 1 | 2017-06-13 12:00:00-05
- f | 1 | 2 | 1 | 2017-06-13 12:00:10-05
- f | 1 | 2 | 1 | 2017-06-13 12:01:00-05
- t | 3 | 2 | 2 | 2017-06-13 12:05:00-05
- t | 1 | 3 | 1 | 2017-06-13 12:05:00-05
- f | 1 | 3 | 1 | 2017-06-13 12:05:10-05
- t | 3 | 2 | 2 | 2017-06-13 12:05:10-05
- f | 3 | 2 | 2 | 2017-06-13 12:05:20-05
- t | 1 | 2 | 2 | 2017-06-13 12:10:00-05
- f | 1 | 2 | 2 | 2017-06-13 12:10:10-05
- f | 1 | 2 | 2 | 2017-06-13 12:10:20-05
- t | 1 | 2 | 1 | 2017-06-13 12:11:00-05
- t | 1 | 2 | 2 | 2017-06-13 12:11:10-05
- t | 3 | 2 | 3 | 2017-06-13 12:12:00-05
- t | 3 | 2 | 2 | 2017-06-13 12:12:10-05
- f | 3 | 2 | 2 | 2017-06-13 12:12:15-05
- t | 3 | 2 | 3 | 2017-06-13 12:12:30-05
- f | 3 | 2 | 3 | 2017-06-13 12:14:00-05
- (18 rows)
- SELECT count(CASE WHEN is_reset THEN 1 END) OVER (ORDER BY created_at) AS grp,
- client,
- provider,
- sender,
- created_at
- FROM (
- SELECT
- (client,provider,sender) <> lag( (client,provider,sender) ) OVER (ORDER BY created_at) AS is_reset,
- client,
- provider,
- sender,
- created_at
- FROM messages
- ) AS t1;
- grp | client | provider | sender | created_at
- -----+--------+----------+--------+------------------------
- 0 | 1 | 2 | 1 | 2017-06-13 12:00:00-05
- 0 | 1 | 2 | 1 | 2017-06-13 12:00:10-05
- 0 | 1 | 2 | 1 | 2017-06-13 12:01:00-05
- 2 | 3 | 2 | 2 | 2017-06-13 12:05:00-05
- 2 | 1 | 3 | 1 | 2017-06-13 12:05:00-05
- 3 | 1 | 3 | 1 | 2017-06-13 12:05:10-05
- 3 | 3 | 2 | 2 | 2017-06-13 12:05:10-05
- 3 | 3 | 2 | 2 | 2017-06-13 12:05:20-05
- 4 | 1 | 2 | 2 | 2017-06-13 12:10:00-05
- 4 | 1 | 2 | 2 | 2017-06-13 12:10:10-05
- 4 | 1 | 2 | 2 | 2017-06-13 12:10:20-05
- 5 | 1 | 2 | 1 | 2017-06-13 12:11:00-05
- 6 | 1 | 2 | 2 | 2017-06-13 12:11:10-05
- 7 | 3 | 2 | 3 | 2017-06-13 12:12:00-05
- 8 | 3 | 2 | 2 | 2017-06-13 12:12:10-05
- 8 | 3 | 2 | 2 | 2017-06-13 12:12:15-05
- 9 | 3 | 2 | 3 | 2017-06-13 12:12:30-05
- 9 | 3 | 2 | 3 | 2017-06-13 12:14:00-05
- (18 rows)
- postgres=# SELECT
- *,
- lag((client, provider, sender)) OVER chats_window
- FROM messages
- WINDOW chats_window AS (
- PARTITION BY client, provider
- ORDER BY created_at
- );
- id | client | provider | sender | created_at | lag
- ----+--------+----------+--------+------------------------+---------
- 1 | 1 | 2 | 1 | 2017-06-13 13:00:00-04 |
- 2 | 1 | 2 | 1 | 2017-06-13 13:00:10-04 | (1,2,1)
- 3 | 1 | 2 | 1 | 2017-06-13 13:01:00-04 | (1,2,1)
- 4 | 1 | 2 | 2 | 2017-06-13 13:10:00-04 | (1,2,1)
- 5 | 1 | 2 | 2 | 2017-06-13 13:10:10-04 | (1,2,2)
- 6 | 1 | 2 | 2 | 2017-06-13 13:10:20-04 | (1,2,2)
- 7 | 1 | 2 | 1 | 2017-06-13 13:11:00-04 | (1,2,2)
- 8 | 1 | 2 | 2 | 2017-06-13 13:11:10-04 | (1,2,1)
- 17 | 1 | 3 | 1 | 2017-06-13 13:05:00-04 |
- 18 | 1 | 3 | 1 | 2017-06-13 13:05:10-04 | (1,3,1)
- 9 | 3 | 2 | 2 | 2017-06-13 13:00:30-04 |
- 10 | 3 | 2 | 2 | 2017-06-13 13:00:50-04 | (3,2,2)
- 11 | 3 | 2 | 2 | 2017-06-13 13:05:20-04 | (3,2,2)
- 12 | 3 | 2 | 3 | 2017-06-13 13:12:00-04 | (3,2,2)
- 13 | 3 | 2 | 2 | 2017-06-13 13:12:10-04 | (3,2,3)
- 14 | 3 | 2 | 2 | 2017-06-13 13:12:15-04 | (3,2,2)
- 15 | 3 | 2 | 3 | 2017-06-13 13:12:30-04 | (3,2,2)
- 16 | 3 | 2 | 3 | 2017-06-13 13:14:00-04 | (3,2,3)
- (18 rows)
- postgres=# SELECT
- client, provider, sender, created_at,
- coalesce(lag((client, provider, sender)) OVER chats_window <> (client, provider, sender), true) AS thread_starter
- FROM messages
- WINDOW chats_window AS (
- PARTITION BY client, provider
- ORDER BY created_at
- );
- client | provider | sender | created_at | thread_starter
- --------+----------+--------+------------------------+----------------
- 1 | 2 | 1 | 2017-06-13 13:00:00-04 | t
- 1 | 2 | 1 | 2017-06-13 13:00:10-04 | f
- 1 | 2 | 1 | 2017-06-13 13:01:00-04 | f
- 1 | 2 | 2 | 2017-06-13 13:10:00-04 | t
- 1 | 2 | 2 | 2017-06-13 13:10:10-04 | f
- 1 | 2 | 2 | 2017-06-13 13:10:20-04 | f
- 1 | 2 | 1 | 2017-06-13 13:11:00-04 | t
- 1 | 2 | 2 | 2017-06-13 13:11:10-04 | t
- 1 | 3 | 1 | 2017-06-13 13:05:00-04 | t
- 1 | 3 | 1 | 2017-06-13 13:05:10-04 | f
- 3 | 2 | 2 | 2017-06-13 13:00:30-04 | t
- 3 | 2 | 2 | 2017-06-13 13:00:50-04 | f
- 3 | 2 | 2 | 2017-06-13 13:05:20-04 | f
- 3 | 2 | 3 | 2017-06-13 13:12:00-04 | t
- 3 | 2 | 2 | 2017-06-13 13:12:10-04 | t
- 3 | 2 | 2 | 2017-06-13 13:12:15-04 | f
- 3 | 2 | 3 | 2017-06-13 13:12:30-04 | t
- 3 | 2 | 3 | 2017-06-13 13:14:00-04 | f
- (18 rows)
- postgres=# WITH thread_starts AS (
- SELECT
- client, provider, sender, created_at,
- coalesce(lag((client, provider, sender)) OVER chats_window <> (client, provider, sender), true) AS thread_starter
- FROM messages
- WINDOW chats_window AS (
- PARTITION BY client, provider
- ORDER BY created_at
- )
- )
- SELECT
- client, provider, sender, created_at,
- lead(created_at) OVER chats_window AS responded_at,
- count(*) OVER (PARTITION BY client, provider) chat_threads_count
- FROM thread_starts
- WHERE thread_starter
- WINDOW chats_window AS (
- PARTITION BY client, provider
- ORDER BY created_at
- );
- client | provider | sender | created_at | responded_at | chat_threads_count
- --------+----------+--------+------------------------+------------------------+---------------
- 1 | 2 | 1 | 2017-06-13 13:00:00-04 | 2017-06-13 13:10:00-04 | 4
- 1 | 2 | 2 | 2017-06-13 13:10:00-04 | 2017-06-13 13:11:00-04 | 4
- 1 | 2 | 1 | 2017-06-13 13:11:00-04 | 2017-06-13 13:11:10-04 | 4
- 1 | 2 | 2 | 2017-06-13 13:11:10-04 | | 4
- 1 | 3 | 1 | 2017-06-13 13:05:00-04 | | 1
- 3 | 2 | 2 | 2017-06-13 13:00:30-04 | 2017-06-13 13:12:00-04 | 4
- 3 | 2 | 3 | 2017-06-13 13:12:00-04 | 2017-06-13 13:12:10-04 | 4
- 3 | 2 | 2 | 2017-06-13 13:12:10-04 | 2017-06-13 13:12:30-04 | 4
- 3 | 2 | 3 | 2017-06-13 13:12:30-04 | | 4
- (9 rows)
- postgres=# WITH thread_starts AS (
- SELECT
- client, provider, sender, created_at,
- coalesce(lag((client, provider, sender)) OVER chats_window <> (client, provider, sender), true) AS thread_starter
- FROM messages
- WINDOW chats_window AS (
- PARTITION BY client, provider
- ORDER BY created_at
- )
- ), response_intervals AS (
- SELECT
- client, provider, sender, created_at,
- lead(created_at) OVER chats_window AS responded_at,
- count(*) OVER (PARTITION BY client, provider) threads_count
- FROM thread_starts
- WHERE thread_starter
- WINDOW chats_window AS (
- PARTITION BY client, provider
- ORDER BY created_at
- )
- )
- SELECT
- provider,
- avg(coalesce(responded_at, CURRENT_TIMESTAMP) - created_at) AS response_interval_avg
- FROM response_intervals
- WHERE sender = client AND (responded_at IS NOT NULL OR threads_count = 1)
- GROUP BY provider;
- provider | response_interval_avg
- ----------+-----------------------
- 2 | 00:03:26.666667
- 3 | 23:19:46.228611
- (2 rows)
Add Comment
Please, Sign In to add comment