# 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:** [18 Giugno 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:** 1

<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:** [18 Giugno 2016, 9:17am UTC](https://meta.discourse.org/t/null-first-visited-at-dates-in-the-topic-users-table/45996/1 "2016-06-18T09:17:15Z")

</div>

While investigating the topic users table last month

[Mysterious entries in topic\_users table](https://meta.discourse.org/t/mysterious-entries-in-topic-users-table/44080):

I noticed quite a few rows had NULL first\_visited\_at dates even though the last\_visited\_at date was not null.

Not certain whether or not it might be due to my dev set-up I ran this query to clean up the cruft.

```plaintext
UPDATE topic_users 
SET first_visited_at = last_visited_at - interval '1 hour'
WHERE last_visited_at IS NOT NULL 
AND first_visited_at IS NULL

```

(\* I didn’t think to give a total\_msecs\_viewed - a query for another day)

Shortly after more of the mysterious NULL first\_visited\_at fields appeared.

I’ve finally narrowed it down to Category Watching.

i.e. when a member is Watching a Category a row is created in the topic\_users table. The first\_visited\_at date is NULL when the member hasn’t visited the topic yet.

Unfortunately, when the member does get around to visiting the topic the first\_visited\_at field does not get populated.

These two queries should give much the same result

```plaintext
SELECT COUNT(id) 
FROM topic_users 
WHERE topic_users.first_visited_at IS NULL 
AND topic_users.last_visited_at IS NOT NULL

```

```plaintext
SELECT COUNT(topic_users.user_id)
FROM topic_users
JOIN topics 
ON topics.id = topic_users.topic_id
WHERE topic_users.first_visited_at IS NULL 
AND topic_users.last_visited_at IS NOT NULL
AND topic_users.notification_level = 3
AND topics.category_id IS NOT NULL
AND topics.category_id IN( SELECT category_users.category_id
    FROM category_users
    WHERE category_users.notification_level = 3 )
AND topic_users.user_id IN( SELECT category_users.user_id
    FROM category_users
    WHERE category_users.notification_level = 3 )

```

---

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