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 "