我有以下查询:
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
表格有name
和category_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
列出项目?谢谢
答案 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])
现在似乎有效。谢谢!