# Badge query for topic views

**URL:** https://meta.discourse.org/t/badge-query-for-topic-views/228174
**Category:** Data & reporting
**Tags:** sql-triggered-badge
**Created:** [May 26, 2022, 3:36pm UTC](https://meta.discourse.org/t/badge-query-for-topic-views/228174 "2022-05-26T15:36:37Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![codergautam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codergautam/32/261083_2.png) [@codergautam](https://meta.discourse.org/u/codergautam)
#### Post date: [May 26, 2022, 3:36pm UTC](https://meta.discourse.org/t/badge-query-for-topic-views/228174/1 "2022-05-26T15:36:37Z")

</div>

Is there a badge query that gives the badge if any one of a users topic has over 1,000 views?

---

<div class="post-metadata">

### Author: ![not-ethan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/not-ethan/32/282617_2.png) [@not-ethan](https://meta.discourse.org/u/not-ethan)
#### Post date: [May 26, 2022, 4:08pm UTC](https://meta.discourse.org/t/badge-query-for-topic-views/228174/2 "2022-05-26T16:08:33Z")

</div>

I found this badge query.

```SQL
SELECT p.user_id, min(p.created_at) granted_at, MIN(p.id) post_id
FROM badge_posts p
JOIN topics t ON t.id = p.topic_id
WHERE category_id = (
  SELECT id FROM categories WHERE name ilike 'Technology'
) AND p.post_number = 1
AND views >= 10
and (:backfill OR ( p.id IN (:post_ids) ))
GROUP BY p.user_id

```

Note this is not mine. Here is the [OP](https://meta.discourse.org/t/what-cool-badge-queries-have-you-come-up-with/18978/212).

---

<div class="post-metadata">

### Author: ![codergautam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codergautam/32/261083_2.png) [@codergautam](https://meta.discourse.org/u/codergautam)
#### Post date: [May 26, 2022, 5:24pm UTC](https://meta.discourse.org/t/badge-query-for-topic-views/228174/3 "2022-05-26T17:24:48Z")

</div>

> [@How\_To\_Roblox](#):
>
> ```plaintext
> SELECT p.user_id, min(p.created_at) granted_at, MIN(p.id) post_id
> FROM badge_posts p
> JOIN topics t ON t.id = p.topic_id
> WHERE category_id = (
> SELECT id FROM categories WHERE name ilike 'Technology'
> ) AND p.post_number = 1
> AND views >= 10
> and (:backfill OR ( p.id IN (:post_ids) ))
> GROUP BY p.user_id
> 
> ```

It doesn’t work sadly, works on the preview, but when i save it says “Sorry, an error has occurred.”

---

<div class="post-metadata">

### Author: ![codergautam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codergautam/32/261083_2.png) [@codergautam](https://meta.discourse.org/u/codergautam)
#### Post date: [May 26, 2022, 5:36pm UTC](https://meta.discourse.org/t/badge-query-for-topic-views/228174/4 "2022-05-26T17:36:33Z")

</div>

Wait nvm, it works, i had a nother badge wih the same name

---

<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: [June 25, 2022, 5:36pm UTC](https://meta.discourse.org/t/badge-query-for-topic-views/228174/5 "2022-06-25T17:36:48Z")

</div>

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