# Awarding badges multiple times with custom SQL

**URL:** https://meta.discourse.org/t/awarding-badges-multiple-times-with-custom-sql/71279
**Category:** Data & reporting
**Tags:** sql-triggered-badge
**Created:** [03.Октябрь.2017 02:53:36 UTC](https://meta.discourse.org/t/awarding-badges-multiple-times-with-custom-sql/71279 "2017-10-03T02:53:36Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![barryvan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/barryvan/32/82635_2.png) [@barryvan](https://meta.discourse.org/u/barryvan)
#### Post date: [03.Октябрь.2017 02:53:36 UTC](https://meta.discourse.org/t/awarding-badges-multiple-times-with-custom-sql/71279/1 "2017-10-03T02:53:36Z")

</div>

I have a badge that’s awarded to people who attend one of our regular events. Because it’s not straightforward to grant badges en masse, I’m using custom SQL to grant the badge. What’s not working, though, is receiving the badge multiple times. I’ve tried to make this work by having one group per event, and including the group’s creation date as the `granted_at` field:

```sql
SELECT
	user_id,
	created_at granted_at,
	NULL post_id
FROM
	group_users
WHERE
	group_id IN (
		SELECT g.id FROM groups g WHERE g.name IN (
			'vit-2016-s2',
			'vit-2017-s1',
			'vit-2017-s2'
		)
	) AND (
		:backfill
		OR user_id IN (:user_ids)
	)

```

Even though I’m a member of all three of these groups, and in [Data Explorer](https://meta.discourse.org/t/32566?silent=true) I see three results for my user, there’s still only one instance of the badge that’s granted.

How would I go about changing this so that the badge will automatically be awarded to each user for each group they’re a member of?

---

<div class="post-metadata">

### Author: ![barryvan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/barryvan/32/82635_2.png) [@barryvan](https://meta.discourse.org/u/barryvan)
#### Post date: [23.Октябрь.2017 05:25:49 UTC](https://meta.discourse.org/t/awarding-badges-multiple-times-with-custom-sql/71279/2 "2017-10-23T05:25:49Z")

</div>

Sorry to bump this, but the next round of events is coming up soon and I’d _love_ to have this fixed ahead of time. 😉 Any ideas on how I can grant a badge multiple times without it being linked to a post? (Or should I set the `post_id` to the announcement post using an ever-growing `case` statement?)

---

<div class="post-metadata">

### Author: ![barryvan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/barryvan/32/82635_2.png) [@barryvan](https://meta.discourse.org/u/barryvan)
#### Post date: [25.Октябрь.2017 10:12:32 UTC](https://meta.discourse.org/t/awarding-badges-multiple-times-with-custom-sql/71279/3 "2017-10-25T10:12:32Z")

</div>

Использование оператора `case` и привязка к постам, похоже, решило проблему. Получается, что значки, которые не выдаются вручную, могут присваиваться несколько раз только в том случае, если они привязаны к постам. Стоит ли сделать это более понятным на странице? Мне стоит повторить это в #Contribute > UX или #Contribute > Feature, чтобы предложить это изменение?

---

<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: [15.Август.2023 09:45:39 UTC](https://meta.discourse.org/t/awarding-badges-multiple-times-with-custom-sql/71279/4 "2023-08-15T09:45:39Z")

</div>


