0

我盯着我的代码看了一个多小时,但我仍然无法弄清楚我的查询出了什么问题。我收到错误消息:ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR: syntax error at or near ")" LINE 38: WHERE (abs(ax2 - ?) / ? < ?)

query = %Q{

  SELECT 

    avg(ax2), 
    avg(ay2), 
    avg(az2) 

  FROM (

    SELECT 
      avg(t1) as t2,
      avg(n1) as n2,
      avg(ax1) as ax2,
      avg(ay1) as ay2,
      avg(az1) as az2

    FROM ( 

      SELECT  
        avg(t) as t1, 
        avg(n) as n1, 
        avg(ax) as ax1, 
        avg(ay) as ay1, 
        avg(az) as az1 

      FROM obds 
      WHERE uid = #{uid}  AND (ev = 6011 or ev = 6012)
      GROUP BY 
        round(t,3), 
        round(n,3)
    ) 

    AS derivedTable1 
    GROUP BY round(t1 + 0.0005,3), round(n1 + 0.0005,3)
  ) 
  AS derivedTable2
  WHERE (abs(ax2 - ?) / ? < ?)  #<<<<< Line with error
}

binds = [ uid, x_average, x_average, THRESHOLD_FOR_RESTING ]
result = Ozd.find_by_sql query, binds
4

2 回答 2

1

The answer is that the query has to be apart of the bind array.

 binds = [ query, uid, x_average, x_average, THRESHOLD_FOR_RESTING ]
 results = Ozd.find_by_sql binds
于 2013-11-26T18:35:05.687 回答
0

试试WHERE (abs((ax2 - ?) / ? < ?))这个

于 2013-10-20T16:13:44.187 回答