Postgres 14
db fiddle updated - Link
EDIT: It seems like I need 1 more row: CREATED AT - specifies when conversation was created.
SELECT DISTINCT ON (date_trunc('day', sent_time), conv_id, user_id)
*
FROM messages
ORDER BY date_trunc('day', sent_time), conv_id, user_id, sent_time;
Here, I want to count every first message sent. Can we use minimum of sent_time of every single date? I want the Count.
Returns:
| conv_id | user_id | content | sent_time | created_time |
|---|---|---|---|---|
1 |
1 |
1st_msg |
01-01-2021 00:01 |
01-01-2021 00:01 |
| 1 | 1 | 4th_msg | 01-01-2021 00:01 | 01-01-2021 00:01 |
| 1 | 2 | 2nd_msg | 01-01-2021 00:01 | 01-01-2021 00:01 |
| 1 | 2 | 3rd_msg | 01-01-2021 00:01 | 01-01-2021 00:01 |
2 |
1 |
1st_msg |
02-01-2021 00:02 |
01-01-2021 00:01 |
| 2 | 1 | 4th_msg | 02-01-2021 00:02 | 01-01-2021 00:01 |
| 2 | 2 | 2nd_msg | 02-01-2021 00:02 | 01-01-2021 00:01 |
| 2 | 2 | 3rd_msg | 02-01-2021 00:02 | 01-01-2021 00:01 |
3 |
2 |
2nd_msg |
03-01-2021 00:03 |
01-01-2021 00:01 |
| 3 | 2 | 4th_msg | 03-01-2021 00:03 | 01-01-2021 00:01 |
Expected Answer:
- First message on 1st Day of Jan was - 1st msg - COUNT 1
- First message on 2nd Day of Jan was - 1st msg - COUNT 2 . . . n - counts
- What is
nornumber of first messages?
Can we use minimum of sent_time of every single date? I want the Count.. No, you can't! The dates are pretty arbitrary per conversation - you could have 5 conversations start on the same day - each with different 1st message times. Also, you could have a long conversation which started on day x and went over into x+1 at 10s after midnight Unless I've misunderstood, is this what you want? If so, I can write it up as an answer. – Vérace Jun 24 '22 at 14:31