# SQL query for users who have posted on a date (or date range)

**URL:** https://meta.discourse.org/t/sql-query-for-users-who-have-posted-on-a-date-or-date-range/99301
**Category:** Data & reporting
**Tags:** sql-query
**Created:** [October 11, 2018, 4:00pm UTC](https://meta.discourse.org/t/sql-query-for-users-who-have-posted-on-a-date-or-date-range/99301 "2018-10-11T16:00:37Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![cjk77](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cjk77/32/151779_2.png) [@cjk77](https://meta.discourse.org/u/cjk77)
#### Post date: [October 11, 2018, 4:00pm UTC](https://meta.discourse.org/t/sql-query-for-users-who-have-posted-on-a-date-or-date-range/99301/1 "2018-10-11T16:00:37Z")

</div>

Can anyone help me figure out how to select users who’ve posted on a particular date or range of dates?

Thanks in advance!

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [October 11, 2018, 7:07pm UTC](https://meta.discourse.org/t/sql-query-for-users-who-have-posted-on-a-date-or-date-range/99301/2 "2018-10-11T19:07:40Z")

</div>

> [@Active users in the last 30 days](https://meta.discourse.org/t/active-users-in-the-last-30-days/275140):
>
> Active users in the last 30 days select username from users where last\_posted\_at \> current\_timestamp - interval '30' day

---

<div class="post-metadata">

### Author: ![cjk77](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cjk77/32/151779_2.png) [@cjk77](https://meta.discourse.org/u/cjk77)
#### Post date: [October 11, 2018, 7:41pm UTC](https://meta.discourse.org/t/sql-query-for-users-who-have-posted-on-a-date-or-date-range/99301/3 "2018-10-11T19:41:20Z")

</div>

Awesome, thank you!!

---

<div class="post-metadata">

### Author: ![cjk77](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cjk77/32/151779_2.png) [@cjk77](https://meta.discourse.org/u/cjk77)
#### Post date: [October 12, 2018, 12:04pm UTC](https://meta.discourse.org/t/sql-query-for-users-who-have-posted-on-a-date-or-date-range/99301/4 "2018-10-12T12:04:41Z")

</div>

Hmmm … I’m not sure that will do exactly what I’m looking for – say I want to query for users who’ve posted on January 1st, for example. Assuming the user has posted since then, the `last_posted_at` field won’t be helpful in that case.

---

<div class="post-metadata">

### Author: ![SidV](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sidv/32/119460_2.png) [@SidV](https://meta.discourse.org/u/SidV)
#### Post date: [October 12, 2018, 12:24pm UTC](https://meta.discourse.org/t/sql-query-for-users-who-have-posted-on-a-date-or-date-range/99301/5 "2018-10-12T12:24:30Z")

</div>

Mmmm maybe this query:

> <https://github.com/SidVal/discourse-data-explorer/blob/queries/queries/top-50-posters.sql>

---

<div class="post-metadata">

### Author: ![cjk77](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cjk77/32/151779_2.png) [@cjk77](https://meta.discourse.org/u/cjk77)
#### Post date: [October 14, 2018, 1:12am UTC](https://meta.discourse.org/t/sql-query-for-users-who-have-posted-on-a-date-or-date-range/99301/6 "2018-10-14T01:12:49Z")

</div>

Ah, that’s very helpful! I haven’t had time to tweak it, but it looks like just what I need to build the query I’m looking for! Thank you!

---

<div class="post-metadata">

### Author: ![SidV](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sidv/32/119460_2.png) [@SidV](https://meta.discourse.org/u/SidV)
#### Post date: [October 15, 2018, 10:41pm UTC](https://meta.discourse.org/t/sql-query-for-users-who-have-posted-on-a-date-or-date-range/99301/7 "2018-10-15T22:41:01Z")

</div>

> [@cjk77](#):
>
> it looks like just what I need to build the query I’m looking for!

Sure, you should try, and If you need help, share your query here and we can help you. 👍

---

<div class="post-metadata">

### Author: ![cjk77](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cjk77/32/151779_2.png) [@cjk77](https://meta.discourse.org/u/cjk77)
#### Post date: [October 18, 2018, 11:30am UTC](https://meta.discourse.org/t/sql-query-for-users-who-have-posted-on-a-date-or-date-range/99301/8 "2018-10-18T11:30:06Z")

</div>

So it turns out to be easier than I thought! This seems to accomplish what I wanted:

```plaintext
SELECT DISTINCT p.user_id AS user_id
FROM posts p
WHERE p.created_at > make_timestamp(2018, 10, 16, 0, 0, 0)
AND p.created_at < make_timestamp(2018, 10, 17, 0, 0, 0)

```

When I rewrote the query for use with a badge, it looked like this:

```plaintext
SELECT DISTINCT p.user_id AS user_id, created_at AS granted_at, NULL AS post_id
FROM posts p
WHERE p.created_at > make_timestamp(2018, 10, 15, 0, 0, 0)
AND p.created_at < make_timestamp(2018, 10, 16, 0, 0, 0)

```

The “DISTINCT” keyword doesn’t work quite so well here because of the added fields, so I get dupes of the various users who’ve posted during the specified time period. I’ve unchecked the “Badge can be awarded multiple times” option, so I think that’s ok. Am I right?

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [July 11, 2021, 9:07am UTC](https://meta.discourse.org/t/sql-query-for-users-who-have-posted-on-a-date-or-date-range/99301/9 "2021-07-11T09:07:31Z")

</div>


