# SQL Badge Query - Quick Responder!

**URL:** https://meta.discourse.org/t/sql-badge-query-quick-responder/36399
**Category:** Data & reporting
**Tags:** sql-triggered-badge
**Created:** [7. Dezember 2015 um 20:14 UTC](https://meta.discourse.org/t/sql-badge-query-quick-responder/36399 "2015-12-07T20:14:04Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ivanrlio](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ivanrlio/32/48497_2.png) [@ivanrlio](https://meta.discourse.org/u/ivanrlio)
#### Post date: [7. Dezember 2015 um 20:14 UTC](https://meta.discourse.org/t/sql-badge-query-quick-responder/36399/1 "2015-12-07T20:14:04Z")

</div>

Hello again,

So I have this query here that’s aimed at giving users that respond to a new question within 4 hours a badge.  
What I have is:

```
SELECT users.id user_id, topics.id topic_id, posts.id post_id FROM users
JOIN posts ON (users.id = posts.user_id) 
JOIN topics ON (posts.topic_id = topics.
WHERE users.id NOT IN (-1, 1, 19)
AND topics.user_id != posts.user_id  
AND timestamp posts.created_at BETWEEN timestamp topics.created_at AND topic.created_at + interval
‘4 HOURS’ ORDER BY topics.id

```

I can’t seem to get it right, I keep getting 'ERROR: syntax error at or near “posts”.

Another thought that I have is:

```
SELECT users.id user_id, topics.id topic_id, posts.id post_id
FROM users 
JOIN posts ON (users.id = posts.user_id) 
JOIN topics ON (posts.topic_id = topics.id)
WHERE EXTRACT(EPOCH FROM posts.created_at) < EXTRACT(EPOCH FROM topics.created_at + INTERVAL '4 HOURS')
AND users.id NOT IN (-1, 1, 19)
AND topics.user_id != posts.user_id  
ORDER BY topics.id

```

But I’m also getting the same syntax error.

For the time-being, I’m not worried about the ‘granted\_at’ and ‘:backfill’ as I’m testing in our my postgresql database.

Any help would be appreciated!

---

<div class="post-metadata">

### Author: ![stevenpslade](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/stevenpslade/32/53550_2.png) [@stevenpslade](https://meta.discourse.org/u/stevenpslade)
#### Post date: [7. Dezember 2015 um 21:27 UTC](https://meta.discourse.org/t/sql-badge-query-quick-responder/36399/2 "2015-12-07T21:27:51Z")

</div>

I tested your query and it works on my end. The only discrepancy I can find, and you might kick yourself for this one, is that [quote=“ivanrlio, post:1, topic:36399”]  
‘4 HOURS’  
[/quote]  
has different quotation mark characters than:[quote=“ivanrlio, post:1, topic:36399”]  
‘4 HOURS’  
[/quote]  
I would presume that the latter is correct and the first one is the reason for throwing you a syntax error.

---

<div class="post-metadata">

### Author: ![ivanrlio](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ivanrlio/32/48497_2.png) [@ivanrlio](https://meta.discourse.org/u/ivanrlio)
#### Post date: [7. Dezember 2015 um 21:29 UTC](https://meta.discourse.org/t/sql-badge-query-quick-responder/36399/3 "2015-12-07T21:29:42Z")

</div>

This… works. Thanks a lot!

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [7. Dezember 2015 um 21:30 UTC](https://meta.discourse.org/t/sql-badge-query-quick-responder/36399/4 "2015-12-07T21:30:47Z")

</div>


