将 SQL 语句转换为 rails 命令

Converting a SQL statement to rails commabd

我遇到的情况是我只需要从特定的活动记录查询响应中获取少量记录。

@annotations = Annotations.select('*', ROW_NUMBER() OVER (PARTITION BY column_a) ORDER BY column_b)

以上是 @annotations 是我想应用以下逻辑的活动记录响应的查询。有没有更好的方法以 rails 方式编写以下逻辑?

with some_table as
(
 select *, row_number() over (partition by column_a order by column_b) rn
 from the_table
)
select * from some_table
where (column_a = 'ABC' and rn <= 10) or (column_b <> 'AAA')

ActiveRecord 不提供高级 CTE API;然而,使用一点 Arel 我们可以在 FROM 子句中将其作为子查询

annotations_table = Annotation.arel_table 
sub_query = annotations_table.project(
  Arel.star,
  Arel.sql('row_number() over (partition by column_a order by column_b)').as(:rn)
)
query = Arel::Nodes::As.new(sub_query, Arel.sql(annotations_table.name))

Annotation.from(query).where(
  annotations_table[:column_a].eq('ABC').and(
    annotations_table[:rn].lt(10)
  ).or(annotations_table[:column_b].not_eq('AAA'))
)

结果将是使用您的 CTE 和您描述的过滤器的 Annotation 个对象的集合。

SQL:

select annotations.* 
from (
 select *, row_number() over (partition by column_a order by column_b) AS rn
 from annotations
) AS annotations
where (annotations.column_a = 'ABC' and annotations.rn <= 10) or (annotations.column_b <> 'AAA')

备注:

  • 通过一些额外的工作,我们可以使它成为 CTE,但在这种情况下似乎不需要它

  • 我们也可以使用大量技巧将此 row_number() over (partition by column_a order by column_b) 转换为 Arel,但它似乎与问题无关。