# Direct Message Look-Up (interval)

**URL:** https://meta.discourse.org/t/direct-message-look-up-interval/277364
**Category:** Data & reporting
**Tags:** chat, sql-query
**Created:** [August 31, 2023, 2:39pm UTC](https://meta.discourse.org/t/direct-message-look-up-interval/277364 "2023-08-31T14:39:50Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [August 31, 2023, 2:39pm UTC](https://meta.discourse.org/t/direct-message-look-up-interval/277364/1 "2023-08-31T14:39:50Z")

</div>

A deep dive into all messages sent and received by a user within the given timeframe.

```sql
-- [params]
-- user_id :user
-- string :interval = 1 month

SELECT 
    chat_channel_id,
    user_id,
    message,
    created_at AS reltime$time,
    created_at 
FROM chat_messages
WHERE chat_channel_id IN (
       SELECT cc.id
       FROM chat_channels cc
       JOIN chat_messages cm ON cm.chat_channel_id = cc.id
       WHERE cc.chatable_type = 'DirectMessage'
       AND cm.user_id = :user
   )
AND created_at::date > CURRENT_TIMESTAMP - INTERVAL :interval
ORDER BY chat_channel_id DESC, created_at ASC

```

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [September 21, 2023, 8:28pm UTC](https://meta.discourse.org/t/direct-message-look-up-interval/277364/2 "2023-09-21T20:28:33Z")

</div>

This one is still functional, but I have spotted that it needs a tiny tweak to account for some table changes that were made a while back. It only affects the channel ids though, so not a breaking issue.
