# SQL query to get the queue of all the PMs in the system

**URL:** https://meta.discourse.org/t/sql-query-to-get-the-queue-of-all-the-pms-in-the-system/38089
**Category:** Support
**Created:** [17 januari 2016 om 23:06 UTC](https://meta.discourse.org/t/sql-query-to-get-the-queue-of-all-the-pms-in-the-system/38089 "2016-01-17T23:06:09Z")
**Posts on this page:** 1
**Showing post:** 3

<div class="post-metadata">

### Author: ![meglio](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/meglio/32/71444_2.png) [@meglio](https://meta.discourse.org/u/meglio)
#### Post date: [27 januari 2016 om 01:52 UTC](https://meta.discourse.org/t/sql-query-to-get-the-queue-of-all-the-pms-in-the-system/38089/3 "2016-01-27T01:52:35Z")

</div>

Here is the query to get all PMs for last 7 days.

```sql
SELECT p.user_id, p.topic_id, p.post_number, p.raw, p.created_at::date
FROM posts p
LEFT JOIN topics t on t.id = p.topic_id
WHERE t.archetype = 'private_message'
  AND t.title <> 'YOUR_WELCOME_MESSAGE_TITLE'
  AND p.created_at::date > now()::date - 8
ORDER BY p.created_at DESC

```

- Replace `YOUR_WELCOME_MESSAGE_TITLE` by the exact title of your welcome message, so that all welcome messages generated automatically are excluded (is there a better way to exclude automatically generated messages?)

- Adjust how many days to look back for by changing the number in **`now()::date - 8`**.  
8 means 7 days;  
7 means 6 days  
…and so on.

---

_[View the full topic](https://meta.discourse.org/t/sql-query-to-get-the-queue-of-all-the-pms-in-the-system/38089)._
