I’m over my head here.
I have a table that due to a miss in the code logic has some NULL timestamp fields
SELECT COUNT(id)
FROM topic_users
WHERE ( topic_users.last_visited_at IS NOT NULL
AND topic_users.first_visited_at IS NULL )
* result = 91
Although it is impossible to determine what the actual first_visited_at timestamp should be or would have been, it is possible to get a “best (or at least better) guess” by using values when they exist in either of two other tables.
For example, if a user makes a post in a topic and later visits the topic the posts created_at timestamp value would be lower than the last_visited_at and be closer to, if not the, actual unrecorded first_visited_at
Similarly, if a user visited a topic and Liked a post and later visits the topic the post_actions created_at timestamp value would be lower than the last_visited_at and be closer to, if not the, actual unrecorded first_visited_at
I’ve cobbled together a mess of a query, and as it returns 5398 (instead of 91) I know I’m missing something
The three tables all have a user_id and a timestamp.
But the posts table is the only way to “connect” the topic_users table to the post_actions table.
topic_users.user_id
topic_users.last_visited_at
topic_users.topic_id
post_actions.user_id
post_actions.created_at
post_actions.post_id
posts.user_id
posts.created_at
posts.topic_id
posts.id
Prepare yourself …
WITH p_table AS (
SELECT posts.topic_id
, posts.user_id
, MIN(posts.created_at)
FROM posts
WHERE posts.topic_id IN (
SELECT DISTINCT topic_users.topic_id
FROM topic_users
WHERE topic_users.posted
AND ( topic_users.last_visited_at IS NOT NULL
AND topic_users.first_visited_at IS NULL )
)
AND posts.user_id IN (
SELECT DISTINCT topic_users.user_id
FROM topic_users
WHERE topic_users.posted
AND ( topic_users.last_visited_at IS NOT NULL
AND topic_users.first_visited_at IS NULL )
)
GROUP BY posts.topic_id, posts.user_id
),
pa_table AS (
SELECT post_actions.post_id
, post_actions.user_id
, MIN(post_actions.created_at)
FROM post_actions
WHERE post_actions.post_id IN (
SELECT posts.id
FROM posts
WHERE posts.topic_id IN (
SELECT DISTINCT topic_users.topic_id
FROM topic_users
WHERE ( topic_users.last_visited_at IS NOT NULL
AND topic_users.first_visited_at IS NULL )
)
)
AND post_actions.user_id IN (
SELECT posts.user_id
FROM posts
WHERE posts.user_id IN (
SELECT DISTINCT topic_users.user_id
FROM topic_users
WHERE ( topic_users.last_visited_at IS NOT NULL
AND topic_users.first_visited_at IS NULL )
)
)
GROUP BY post_actions.post_id, post_actions.user_id
),
tu_table AS (
SELECT topic_users.user_id
, topic_users.topic_id
, topic_users.last_visited_at
FROM topic_users
WHERE ( topic_users.last_visited_at IS NOT NULL
AND topic_users.first_visited_at IS NULL )
)
SELECT COUNT(LEAST(p_table.min, pa_table.min, tu_table.last_visited_at))
FROM p_table, pa_table, tu_table
WHERE ( tu_table.user_id = p_table.user_id
OR tu_table.user_id = pa_table.user_id )
AND tu_table.topic_id = p_table.topic_id