Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql Rails按属性限制记录_Sql_Ruby On Rails_Postgresql_Subquery - Fatal编程技术网

Sql Rails按属性限制记录

Sql Rails按属性限制记录,sql,ruby-on-rails,postgresql,subquery,Sql,Ruby On Rails,Postgresql,Subquery,我有两种型号的型号和电话 呼叫属于\u类别 呼叫具有类别id和优先级 我只需要接到每个类别的10个电话,优先级别较低的优先 有没有办法通过一次查询来实现这一点?您可以这样实现: Call.from("(select calls.*, row_number() over (partition by category_id order by priority) as num from calls) c") .where("num <

我有两种型号的型号和电话

呼叫属于\u类别

呼叫具有类别id和优先级

我只需要接到每个类别的10个电话,优先级别较低的优先


有没有办法通过一次查询来实现这一点?

您可以这样实现:

Call.from("(select calls.*, row_number() over (partition by category_id order by priority) as num 
            from calls) c")
    .where("num <= 10")
    .select("c.id, c.category_id, c.priority")

对我来说似乎是标准的SQL,只需检查sur ActiveRecord doc即可,但我认为应该是这样possible@SylvainChaugny我检查了一下,但没有发现任何有用的东西