# Badge Query for Profile Views

**URL:** https://meta.discourse.org/t/badge-query-for-profile-views/228182
**Category:** Data & reporting
**Tags:** sql-triggered-badge
**Created:** [May 26, 2022, 5:41pm UTC](https://meta.discourse.org/t/badge-query-for-profile-views/228182 "2022-05-26T17:41:07Z")
**Posts on this page:** 3
**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, 5:41pm UTC](https://meta.discourse.org/t/badge-query-for-profile-views/228182/1 "2022-05-26T17:41:08Z")

</div>

Is there a way to give a badge if a profile has more than X views?

---

<div class="post-metadata">

### Author: ![SaraDev](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/saradev/32/335139_2.png) [@SaraDev](https://meta.discourse.org/u/SaraDev)
#### Post date: [June 1, 2022, 11:10pm UTC](https://meta.discourse.org/t/badge-query-for-profile-views/228182/2 "2022-06-01T23:10:42Z")

</div>

Hi Coder,

You should be able to use the following SQL to create a custom badge that’s granted once the user has more than X number of views.

### Badge Query for Profile Views

```plaintext
SELECT 
  user_profile_views.user_profile_id AS user_id, 
  COUNT(user_profile_views.user_profile_id),
  current_timestamp granted_at
FROM user_profile_views
GROUP BY user_profile_views.user_profile_id
HAVING COUNT(user_profile_views.user_profile_id) > X 
ORDER BY COUNT(user_profile_views.user_profile_id) DESC

```

In case you’re curious, here’s a corresponding [Data Explorer](https://meta.discourse.org/t/32566?silent=true) SQL query for this as well.

### SQL [Data Explorer](https://meta.discourse.org/t/32566?silent=true) Query for Profile Views

```plaintext
-- [params]
-- int :view_count = X

SELECT 
  user_profile_views.user_profile_id AS user_id, 
  COUNT(user_profile_views.user_profile_id) AS "Views",
FROM user_profile_views
GROUP BY user_profile_views.user_profile_id
HAVING COUNT(user_profile_views.user_profile_id) > :view_count
ORDER BY COUNT(user_profile_views.user_profile_id) DESC

```

I hope this helps!

---

<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: [July 1, 2022, 11:10pm UTC](https://meta.discourse.org/t/badge-query-for-profile-views/228182/3 "2022-07-01T23:10:43Z")

</div>

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