# NULL first\_visited\_at dates in the topic\_users table

**URL:** <https://meta.discourse.org/t/null-first-visited-at-dates-in-the-topic-users-table/45996>\
**Category:** Development\
**Created:** [June 18, 2016, 9:17am UTC](https://meta.discourse.org/t/null-first-visited-at-dates-in-the-topic-users-table/45996 "2016-06-18T09:17:15Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![Mittineague](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mittineague/32/114259_2.png) [@Mittineague](https://meta.discourse.org/u/Mittineague)\
**Post date:** [July 2, 2016, 10:27pm UTC](https://meta.discourse.org/t/null-first-visited-at-dates-in-the-topic-users-table/45996/6 "2016-07-02T22:27:51Z")

</div>

I have the feeling that when I first looked into the weird topic id problem I may have opened a can of worms.

I’ve put together some queries that can help find the earliest “known” first\_visited\_at timestamps.

```plaintext
SELECT topic_users.topic_id
 , topic_users.user_id
 , topic_users.last_visited_at
FROM topic_users
WHERE ( topic_users.first_visited_at IS NULL
      AND topic_users.last_visited_at IS NOT NULL ) 

```

```plaintext
SELECT topic_users.topic_id
 , topic_users.user_id
 , MIN(posts.created_at)
FROM topic_users
JOIN posts
ON posts.topic_id = topic_users.topic_id 
AND posts.user_id = topic_users.user_id
WHERE topic_users.posted 
 AND ( topic_users.first_visited_at IS NULL
      AND topic_users.last_visited_at IS NOT NULL )   
GROUP BY topic_users.topic_id, topic_users.user_id  

```

```plaintext
SELECT tu.topic_id
 , tu.user_id
 , MIN(pa.created_at)
FROM ( SELECT topic_users.topic_id
        , topic_users.user_id
      FROM topic_users
      WHERE ( topic_users.first_visited_at IS NULL
            AND topic_users.last_visited_at IS NOT NULL )
      ) AS tu 
JOIN LATERAL ( SELECT posts.id
              FROM posts
              WHERE posts.topic_id = tu.topic_id 
              ) AS pt
ON true JOIN LATERAL ( SELECT post_actions.created_at
              FROM post_actions
              WHERE post_actions.post_id = pt.id 
              AND post_actions.user_id = tu.user_id 
              ) AS pa
ON true
GROUP BY tu.topic_id, tu.user_id

```

But I’m wondering just how important getting a more accurate first\_visited\_at timestamp really is.

I’ve noticed that when a member is mentioned or invited, a row in the topic\_users table is created.  
Both the first\_visited\_at and last\_visited\_at fields are given :now even if the member _never visited_ the topic.

---

_[View the full topic](https://meta.discourse.org/t/null-first-visited-at-dates-in-the-topic-users-table/45996)._
