SQL Badge Query - Quick Responder!

AI-generated summary

In the SQL Badge Query - Quick Responder! discussion, user ivanrlio sought help debugging a PostgreSQL query designed to award a badge for responding to a new question within 4 hours. The user encountered a syntax error near “posts” in their initial attempts, which involved joining users, posts, and topics tables while filtering for non-system users and ensuring the response time fell within the specified interval. Despite trying different timestamp extraction methods, the error persisted.

User stevenpslade tested the query and identified the issue: the original code used curly quotes (‘4 HOURS’) instead of standard single quotes (‘4 HOURS’) for the interval definition. After correcting the quotation marks, the query executed successfully. ivanrlio confirmed the fix worked and expressed gratitude for the assistance.

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!

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.

This… works. Thanks a lot!