Skip to content
Advertisement

Querying for users who have NOT completed a survey today and Receiving an error

I have a feature I am working on and I need my PostgreSQL db to return all the users that have not completed a survey today.

select distinct *
from users 
left join survey_results
on users.user_id = survey_results.user_id
where customer_id = '9000'
and survey_results.created_at < (DATE_PART('year', survey_results.created_at) = (SELECT EXTRACT(YEAR FROM CURRENT_TIMESTAMP))
                                  AND DATE_PART('month', survey_results.created_at) = (SELECT EXTRACT(MONTH FROM CURRENT_TIMESTAMP)) 
                                  AND DATE_PART('day', survey_results.created_at) = (SELECT EXTRACT(DAY FROM CURRENT_TIMESTAMP)))

I am using a left join to join on my survey results table and filtering by customer_id and where the survey_results.created < today but I am using a date part and extract to get the date. If there is better way to do that chime in but that is what I have.

I am receiving this output when running my query instead of results.

ERROR:  operator does not exist: timestamp with time zone < boolean
LINE 6: and survey_results.created_at < (DATE_PART('year', survey_re...
                                      ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
SQL state: 42883
Character: 155

Advertisement

Answer

Rather than using datepart, compare it with current_date –

select distinct *
from users 
left join survey_results
on users.user_id = survey_results.user_id
where customer_id = '9000'
and survey_results.created_at < CURRENT_DATE

If your survey_results.created_at column is timestamp, use date function to convert it to date then compare it to current_date –

select distinct *
from users 
left join survey_results
on users.user_id = survey_results.user_id
where customer_id = '9000'
and DATE(survey_results.created_at) < CURRENT_DATE
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement