# A silver badge for \_each\_ 5 bronze badges

**URL:** <https://meta.discourse.org/t/a-silver-badge-for-each-5-bronze-badges/283284>\
**Category:** Data & reporting\
**Tags:** sql-triggered-badge\
**Created:** [October 24, 2023, 3:33pm UTC](https://meta.discourse.org/t/a-silver-badge-for-each-5-bronze-badges/283284 "2023-10-24T15:33:49Z")\
**Posts on this page:** 1\
**Showing post:** 13

<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:** [November 6, 2023, 6:56pm UTC](https://meta.discourse.org/t/a-silver-badge-for-each-5-bronze-badges/283284/13 "2023-11-06T18:56:34Z")

</div>

Hmmm. 🤔 I think I see where you’re going with this. Let me have another go…

* * *

@ganncamp - I think we may have some success… 🙂

Based on Bronze Badge A being awarded through the post wrench or by giving a reason on the `/admin/users/{user_id}/{username}/badges` page:

 ![post wrench](https://global.discourse-cdn.com/meta/original/4X/6/a/e/6aefde827f7282a42816cdab73df20b19cc839cb.png) ![/admin/users/user_id/username/badges](https://global.discourse-cdn.com/meta/original/4X/8/4/9/8496f83b7b90c599d954d030b043d1cb01cc910e.png)

Then I think this is indeed possible. 🥳

```sql
WITH badge_count AS (

    SELECT 
        user_id,
        granted_at,
        post_id,
        ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY granted_at DESC) AS row
    FROM user_badges
    WHERE badge_id = 110
      AND post_id IS NOT NULL
)

SELECT user_id, granted_at, post_id
FROM badge_count
WHERE row % 5 = 0
  AND (:backfill OR post_id IN (:post_ids))

```

(Adding in the `AND post_id IS NOT NULL` protects it from if someone is awarded one without a reason, otherwise it breaks)

 ![other badge settings](https://global.discourse-cdn.com/meta/original/4X/b/1/7/b17aac38b391f2c6e627d4489c3adaf7b8efbc2c.png)

I have just tried it out and fast-forwarded by triggering the GrantBadge background job and my test user has finally received the full credit they deserve. 🙂

 ![test_eight - first run](https://global.discourse-cdn.com/meta/original/4X/2/0/1/2014246d73b3d7cbaaf0bd8f2d7ebfde743a63ac.png)

I then awarded them 5 more Badge A for 5 more different posts and ran it again: 🎉

 ![image](https://global.discourse-cdn.com/meta/original/4X/1/2/4/1241f7a162323369558aea1e3df2f046f7763358.png)

---

_[View the full topic](https://meta.discourse.org/t/a-silver-badge-for-each-5-bronze-badges/283284)._
