# 徽章无法工作

**URL:** https://meta.discourse.org/t/badge-not-working/39426
**Category:** Data & reporting
**Tags:** sql-triggered-badge
**Created:** [2016年二月11日 20:52 UTC](https://meta.discourse.org/t/badge-not-working/39426 "2016-02-11T20:52:25Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![zainab](https://avatars.discourse-cdn.com/v4/letter/z/258eb7/32.png) [@zainab](https://meta.discourse.org/u/zainab)
#### Post date: [2016年二月11日 20:52 UTC](https://meta.discourse.org/t/badge-not-working/39426/1 "2016-02-11T20:52:25Z")

</div>

Hi,

I’m not sure if I’m posting in the correct category.

anyway theres a badge for the forum called help desk and its not working, users get 10 accepted answers yet they dont get a badge

here’s the SQL code:

```plaintext
SELECT p.user_id, p.id post_id, p.updated_at granted_at
FROM badge_posts p
WHERE p.post_number > 1 AND 
p.id IN (
SELECT post_id FROM (
SELECT pc.post_id, row_number() 
OVER (PARTITION BY p1.user_id ORDER BY pc.created_at) as rnum
FROM post_custom_fields pc
JOIN badge_posts p1 ON p1.id = pc.post_id
JOIN topics t1 ON p1.topic_id = t1.id
WHERE name = 'is_accepted_answer' AND
p1.user_id <> t1.user_id AND 
(
:backfill OR 
p1.user_id IN (
select user_id from posts where p1.id IN (:post_ids)
)
)
) X WHERE rnum = 1)

```

---

<div class="post-metadata">

### Author: ![dax](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dax/32/244677_2.png) [@dax](https://meta.discourse.org/u/dax)
#### Post date: [2016年二月11日 21:44 UTC](https://meta.discourse.org/t/badge-not-working/39426/2 "2016-02-11T21:44:03Z")

</div>

I don’t know if the query change in the meanwhile but the original code is this:

```plaintext
SELECT id user_id, current_timestamp granted_at
FROM users
WHERE id IN (
       SELECT p1.user_id 
       FROM post_custom_fields pc
       JOIN badge_posts p1 ON p1.id = pc.post_id
       JOIN topics t1 ON p1.topic_id = t1.id
       WHERE p1.user_id <> t1.user_id AND 
                    name = 'is_accepted_answer' AND 
            p1.user_id IN (
                   SELECT user_id 
                   FROM posts 
                   WHERE :backfill OR p1.id IN (:post_ids)
            )
        GROUP BY p1.user_id
        HAVING COUNT(*) > 9
)
```

e.g. in your code I don’t see the line “HAVING COUNT” for count the number of accepted answers

EDIT: @zee the code you paste in your post is for 1 accepted answer, not for 10

---

<div class="post-metadata">

### Author: ![zainab](https://avatars.discourse-cdn.com/v4/letter/z/258eb7/32.png) [@zainab](https://meta.discourse.org/u/zainab)
#### Post date: [2016年二月12日 03:10 UTC](https://meta.discourse.org/t/badge-not-working/39426/3 "2016-02-12T03:10:57Z")

</div>

thank you.. I shall change it

---

<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: [2016年六月9日 16:46 UTC](https://meta.discourse.org/t/badge-not-working/39426/4 "2016-06-09T16:46:56Z")

</div>

@Trash, in this code, with the last version of discourse I get this error:

 ![](https://global.discourse-cdn.com/meta/original/3X/f/a/faa8b4adbeeb910bd305bd953f2c78d0c95a6784.png)

Could you check it please?

* * *

My mistake, solved here:  
[https://meta.discourse.org/t/10-accepted-answer-badge-giving-an-error/34560/3?u=sidv](https://meta.discourse.org/t/10-accepted-answer-badge-giving-an-error/34560/3)

Thanks!

---

<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: [2024年六月8日 12:45 UTC](https://meta.discourse.org/t/badge-not-working/39426/5 "2024-06-08T12:45:33Z")

</div>

此主题已在 3039 天后自动关闭。不再允许回复。
