0
我有一个Rails应用程序,它使用下面显示的自定义sql字符串(带有postgres数据库)。该字符串正常工作。我试图在查询中添加一个时间条件,以便只从过去7天内创建的answer_votes,best_answers和贡献中检索记录。因此,我今天的日期和减去7天如何在此sql查询中包含时间条件
date = Date.today - 7
和取代在下面的SQL字符串的语句是这样的
(select Coalesce(sum(value),0) from answer_votes where (answer_votes.user_id = users.id) AND (answer_votes.created_at <= #{date}))
但是,它给了我好几个错误
PG::Error: ERROR: operator does not exist: timestamp without time zone <= integer
你能告诉我正确的方法吗?请注意,虽然我只显示了我对answer_votes的尝试,但我会尝试对best_answers和贡献进行相同的操作,但解决方案将与此相同(我假设)。
sql_string = "with cte_scoring as (
select
users.id, users.name, users.email,
(select Coalesce(sum(value),0) from answer_votes where answer_votes.user_id = users.id) +
(select Coalesce(sum(value),0) from best_answers where best_answers.user_id = users.id) +
(select Coalesce(sum(value),0) from contributions where contributions.user_id = users.id) total_score
from
users join
contacts on (contacts.user_id = users.id)
where
users.lawyer = 'true')
select id,
name,
email,
total_score
from cte_scoring
order by total_score desc
limit 2 "