# Should be enjoying Christmas! badge

**URL:** https://meta.discourse.org/t/should-be-enjoying-christmas-badge/281536
**Category:** Data & reporting
**Tags:** sql-triggered-badge
**Created:** [January 6, 2019, 8:01pm UTC](https://meta.discourse.org/t/should-be-enjoying-christmas-badge/281536 "2019-01-06T20:01:31Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Alexander\_Wright](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alexander_wright/32/119518_2.png) [@Alexander\_Wright](https://meta.discourse.org/u/Alexander_Wright)
#### Post date: [January 6, 2019, 8:01pm UTC](https://meta.discourse.org/t/should-be-enjoying-christmas-badge/281536/1 "2019-01-06T20:01:31Z")

</div>

## Should be enjoying Christmas!

This code give every user who logs in between ‘Day of Year’ = 358 and 2 days into the new year a badge. Word it as you like!

SQL:

```sql
SELECT distinct(user_id), CURRENT_DATE as granted_at FROM user_visits WHERE
date_part('doy', visited_at) >= 358
OR
date_part('doy', visited_at) <= 2

```

What I’d like it to do is award the badge every day they visit between the two dates. **Help please!**

---

<div class="post-metadata">

### Author: ![Rafael\_Lima\_Vasconce](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rafael_lima_vasconce/32/137309_2.png) [@Rafael\_Lima\_Vasconce](https://meta.discourse.org/u/Rafael_Lima_Vasconce)
#### Post date: [April 10, 2019, 6:15pm UTC](https://meta.discourse.org/t/should-be-enjoying-christmas-badge/281536/2 "2019-04-10T18:15:05Z")

</div>

> [@Alexander\_Wright](#):
>
> What I’d like it to do is award the badge every day they visit between the two dates. **Help please!**

I think if you add another field like the “visited\_at” it woks.

Because you’re using just user\_id and current\_timestamp, and they are distinct just in user\_id, because current\_timestamp would be the same every row.  
If you add visited\_at they could be distinct, and them one badge for every day.

But maybe you already solve this 😉

Edit1: Actually, I dont think this query need the distinct

---

<div class="post-metadata">

### Author: ![MoltonMontro](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/moltonmontro/32/165777_2.png) [@MoltonMontro](https://meta.discourse.org/u/MoltonMontro)
#### Post date: [April 20, 2019, 8:53pm UTC](https://meta.discourse.org/t/should-be-enjoying-christmas-badge/281536/3 "2019-04-20T20:53:45Z")

</div>

> [@Alexander\_Wright](#):
>
> What I’d like it to do is award the badge every day they visit between the two dates. **Help please!**

Building on from what @Rafael_Lima_Vasconce suggested, I created the following as an example “Easter 2019” badge.

> [@](#):
>
> `
> SELECT distinct(user_id), visited_at /* Badge is obtained the day they first visit during the recognized timespan. */ as granted_at FROM user_visits WHERE
> date_part('doy', visited_at) >= 110 /* April 20 */
> AND
> date_part('doy', visited_at) <= 119 /* April 29 */
> AND
> date_part('year', visited_at) = 2019 /* Checks for the year. */
> `

I have absolutely no experience with SQL besides just modifying numerical values on some of the cool badge queries that have been posted here, but I don’t see any indication that this shouldn’t work (assuming you’re still trying to find a solution).

If you wanted people to receive the badge multiple times, I believe just enabling that tick option when setting up your badge should do so. Otherwise, it should only give them the badge the _first time_ they visit during the specified time-span.
