# Help with sql - autoclose/repeat results/week filter

**URL:** https://meta.discourse.org/t/help-with-sql-autoclose-repeat-results-week-filter/231038
**Category:** Data & reporting
**Tags:** sql-query
**Created:** [June 24, 2022, 5:25pm UTC](https://meta.discourse.org/t/help-with-sql-autoclose-repeat-results-week-filter/231038 "2022-06-24T17:25:17Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Matt0x01](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/matt0x01/32/264811_2.png) [@Matt0x01](https://meta.discourse.org/u/Matt0x01)
#### Post date: [June 24, 2022, 5:25pm UTC](https://meta.discourse.org/t/help-with-sql-autoclose-repeat-results-week-filter/231038/1 "2022-06-24T17:25:17Z")

</div>

Hey so I’ve created a query(at the bottom) that displays topic, category, the vote count of a topic, if its closed, when it was created and the user who has created. If more info is needed please feel free to let me know.

I have a few issues and questions I’d like some help or guidance with since I’m new to sql.

1. When running this query it displays the same topic hundreds of times over. How can I fix this

2. Is there any way to get and show when a topic will auto close?

3. I want only topics within a week to display. But the date\_trunc i added shows some items a little over a week ago?

```plaintext
SELECT
    t.id as topic_id,
    t.category_id,
    dvc.votes_count,
    t.closed,
    t.created_at,
    t.user_id
FROM topics t, discourse_voting_topic_vote_count dvc
WHERE t.closed = true
    AND t.category_id = 20
    AND dvc.votes_count >= 1
    AND t.created_at <= date_trunc('week', current_date)::date
    AND t.created_at >= date_trunc('week', current_date)::date - 7

```

---

<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: [June 24, 2022, 6:50pm UTC](https://meta.discourse.org/t/help-with-sql-autoclose-repeat-results-week-filter/231038/2 "2022-06-24T18:50:29Z")

</div>

Try adding this after the `WHERE` line.

```
 AND t.id = dvc.topic_id

```

I didn’t test it, but I think that’s your big problem (fixes the “same topic” problem)

---

<div class="post-metadata">

### Author: ![Matt0x01](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/matt0x01/32/264811_2.png) [@Matt0x01](https://meta.discourse.org/u/Matt0x01)
#### Post date: [June 24, 2022, 7:05pm UTC](https://meta.discourse.org/t/help-with-sql-autoclose-repeat-results-week-filter/231038/3 "2022-06-24T19:05:30Z")

</div>

Awesome! Thanks Jay it works 😃

Would you know how to go about the other issues?

---

<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: [June 24, 2022, 7:19pm UTC](https://meta.discourse.org/t/help-with-sql-autoclose-repeat-results-week-filter/231038/4 "2022-06-24T19:19:41Z")

</div>

My guess on (3) is that it’s a time zone issue, and/or that it’s based on the second that you run the script and you want the previous or next midnight. (Not quite sure how to fix either of those offhand).

Not sure if this’ll help but you can try

To get when they’re going to close, you’d need to add something to the `SELECT` part (which you can infer from the available fields) and change `FROM` like

```
   FROM topics t, discourse_voting_topic_vote_count dvc, topic_timers tt

```

and add this after `WHERE`

```plaintext
    AND tt.topic_id=t.id

```

(just like the one you added before).

---

<div class="post-metadata">

### Author: ![Matt0x01](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/matt0x01/32/264811_2.png) [@Matt0x01](https://meta.discourse.org/u/Matt0x01)
#### Post date: [June 24, 2022, 7:42pm UTC](https://meta.discourse.org/t/help-with-sql-autoclose-repeat-results-week-filter/231038/5 "2022-06-24T19:42:18Z")

</div>

Thanks again Jay you’re a lifesaver (:

---

<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: [June 25, 2022, 9:36am UTC](https://meta.discourse.org/t/help-with-sql-autoclose-repeat-results-week-filter/231038/6 "2022-06-25T09:36:45Z")

</div>

Wow! That worked? That’s awesome. Glad it helped.

---

<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 25, 2022, 9:37am UTC](https://meta.discourse.org/t/help-with-sql-autoclose-repeat-results-week-filter/231038/7 "2022-07-25T09:37:20Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
