Elixir / Phoenix查询ERROR(grouping_error)

时间:2016-06-21 06:29:40

标签: elixir phoenix-framework psql

我有以下查询:

fooditems = Repo.all(from p in OrderItem, join: f in Food,
                                          on: p.food_id == f.id,
                                          where: p.inserted_at >= ^date_from and p.inserted_at <= ^date_to,
                                          group_by: f.name,
                                          select: %{name: f.name, quantity: sum(p.quantity)},
                                          order_by: [desc: f.category_id])

OrderItem表格有food_id列,而Food表格有namecategory_id。我需要category_id表的Food排序结果。但是,查询似乎不起作用。它正在回归:

ERROR (grouping_error): column "f1.category_id" must appear in the GROUP BY clause or be used in an aggregate function

但是,我不想按类别对其进行分组,只是以同一个category_id中的项目一起显示的方式呈现它(order_by)。 如何使查询按category_id列出项目?谢谢

1 个答案:

答案 0 :(得分:0)

根据@HarrisonLucas的建议,将我的查询更改为以下内容:

fooditems = Repo.all(from p in OrderItem, full_join: f in Food,
                                          on: p.food_id == f.id,
                                          where: p.inserted_at >= ^date_from and p.inserted_at <= ^date_to,
                                          group_by: f.name,
                                          group_by: f.category_id,
                                          select: %{name: f.name, quantity: sum(p.quantity)},
                                          order_by: [asc: f.category_id])

现在似乎有效。谢谢!