我开发了我的网站,它在我的本地机器上运行得非常好。查询由一位 Stackoverflow 成员提供 in this thread .
schema.rb portion that is relevant
本地数据与 Heroku 上的数据相同。
select *
from (
select *
, row_number() over (partition by odds_type order by odds_index desc) as rn2
from (
select *
, row_number() over (partition by event_id, bookmaker_id, odds_type
order by created_at desc) as rn1
from Odds
where event_id = #{e.id}
) sub1
where rn1 = 1
) sub2
where rn2 = 1
控制台报错
Odds Load (88.4ms) select * from ( select *, row_number() over (partition by odds_type order by odds_index desc) as rn2 from (select *, row_number() over (partition by event_id, bookmaker_id, odds_type order by created_at desc) as rn1 from Odds where event_id = 21 ) sub1 where rn1 = 1 ) sub2where rn2 = 1
ActiveRecord::StatementInvalid: PG::Error: ERROR: syntax error at or near "over"
LINE 1: select * from ( select *, row_number() over (partition by od...
^
最佳答案
row_number() 和 window functions通常在 PostgreSQL 8.4 或更高版本中可用。该错误表明您正在旧版本(8.3?)上尝试此查询,因此它无法工作。
更新:在使用 PG 9.x 研究了原始问题和答案之后,我相信您可以使用 PostgreSQL 特定的 DISTINCT ON 获得相同的结果。子句,非标准但在旧版本的 PG 中可用,并且在您的情况下非常方便。这是我的建议:
SELECT DISTINCT on (ot) bi,ot,oi FROM
(select distinct on (bi,ot) bi,ot,oi from odds
where ei=1 order by bi,ot,created_at desc) subq
ORDER BY ot,oi DESC;
关于ruby-on-rails-3 - PostgreSQL 查询在本地 (9.x) 工作,但在 Heroku (8.x) 上给出语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10995418/