# Count of badges on user page?

**URL:** https://meta.discourse.org/t/count-of-badges-on-user-page/125611
**Category:** Feature
**Created:** [12 augustus 2019 om 22:11 UTC](https://meta.discourse.org/t/count-of-badges-on-user-page/125611 "2019-08-12T22:11:12Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Nicholas\_Tolstoshev](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nicholas_tolstoshev/32/62714_2.png) [@Nicholas\_Tolstoshev](https://meta.discourse.org/u/Nicholas_Tolstoshev)
#### Post date: [12 augustus 2019 om 22:11 UTC](https://meta.discourse.org/t/count-of-badges-on-user-page/125611/1 "2019-08-12T22:11:13Z")

</div>

Would it be possible to get a column added for badge count? I have a lot of badge…um…hounds, yeah that’s the PC word, and they want to see at a glance who is in the lead in terms of collecting the most badges.

---

<div class="post-metadata">

### Author: ![Nicholas\_Tolstoshev](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nicholas_tolstoshev/32/62714_2.png) [@Nicholas\_Tolstoshev](https://meta.discourse.org/u/Nicholas_Tolstoshev)
#### Post date: [21 augustus 2019 om 21:40 UTC](https://meta.discourse.org/t/count-of-badges-on-user-page/125611/2 "2019-08-21T21:40:13Z")

</div>

![](https://global.discourse-cdn.com/meta/original/4X/8/4/1/8410b295631e56f585c6ff596bf1f0f8ab538d34.webp "High School GIF - Find & Share on GIPHY")

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [21 augustus 2019 om 22:05 UTC](https://meta.discourse.org/t/count-of-badges-on-user-page/125611/3 "2019-08-21T22:05:05Z")

</div>

It’s not planned or on any roadmaps at the moment.

---

<div class="post-metadata">

### Author: ![Nicholas\_Tolstoshev](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nicholas_tolstoshev/32/62714_2.png) [@Nicholas\_Tolstoshev](https://meta.discourse.org/u/Nicholas_Tolstoshev)
#### Post date: [21 augustus 2019 om 22:18 UTC](https://meta.discourse.org/t/count-of-badges-on-user-page/125611/4 "2019-08-21T22:18:01Z")

</div>

Thanks for the info - has anyone else asked for something like this, to your knowledge, or am I the lone requestor 🙂

---

<div class="post-metadata">

### Author: ![mattdm](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mattdm/32/216484_2.png) [@mattdm](https://meta.discourse.org/u/mattdm)
#### Post date: [8 december 2021 om 23:59 UTC](https://meta.discourse.org/t/count-of-badges-on-user-page/125611/5 "2021-12-08T23:59:11Z")

</div>

I would _really really_ like this too.

Ruby is a weird foreign language to me, and SQL beyond simple SELECT and maybe GROUP BY gives me hives, but … as I understand it, the things in the user page can be those from the `directory_items` table, which is constructed by SUMming and COUNTing things in various user tables, in [directory\_item.rb](https://github.com/discourse/discourse/blob/main/app/models/directory_item.rb).

In [user\_stat.rb](https://github.com/discourse/discourse/blob/main/app/models/directory_item.rb), I find a `distinct_badge_count`, which would be fine if this were for all time, but we want the various leaderboard time periods, and also for this purpose probably don’t want `distinct` anyway.

I _think_ maybe all that’s needed is to add to the gigantic SQL query to count from `user_badges` where the `granted_at` date is after `since`?

Oh, although I guess also it needs to check and only count the badges that are enabled.

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [3 februari 2022 om 00:40 UTC](https://meta.discourse.org/t/count-of-badges-on-user-page/125611/6 "2022-02-03T00:40:32Z")

</div>

I’m not sure about the technical difficulty of adding badge counts to the users page, but now that we allow optional directory items to be added to the users page, it seems that badge counts would be a logical addition. It would help to make the users page function more as a leaderboard.

Just a thought, but would adding a `badges_received` column to `directory_items` be a possible way of approaching the issue?

---

<div class="post-metadata">

### Author: ![Nicholas\_Tolstoshev](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nicholas_tolstoshev/32/62714_2.png) [@Nicholas\_Tolstoshev](https://meta.discourse.org/u/Nicholas_Tolstoshev)
#### Post date: [11 augustus 2022 om 17:29 UTC](https://meta.discourse.org/t/count-of-badges-on-user-page/125611/7 "2022-08-11T17:29:28Z")

</div>

I’m looking for an all time count, so thanks to your direction I pulled this query from the user\_stat.rb and ran it in the [data explorer](https://meta.discourse.org/t/32566?silent=true) and it gave me a list of all users with their badge count:

```plaintext
SELECT users.id user_id, COUNT(distinct user_badges.badge_id) distinct_badge_count
        FROM users
        LEFT JOIN user_badges ON user_badges.user_id = users.id
                              AND (user_badges.badge_id IN (SELECT id FROM badges WHERE enabled))
        GROUP BY users.id
        ORDER BY distinct_badge_count DESC

```
