本文介绍了Ruby on Rails:必须出现在GROUP BY子句中或在聚合函数中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的查询。
ShopifyOrderLineItem.select("shopify_order_line_items.*, sum(amount) as total_price, sum(quantity) as total_quantity").where(:vendor_id => vendor_id).group("shopify_order_line_items.title")
我遇到以下异常。
PG::GroupingError: ERROR: column "shopify_order_line_items.id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT shopify_order_line_items.*, sum(amount) as total_pric...
有什么建议吗?
我已经尝试过了,但是没有用。
I have tried this but it doesn't work.
ShopifyOrderLineItem.select("shopify_order_line_items.*, count(shopify_order_line_items.id) as unique_ids, sum(amount) as total_price, sum(quantity) as total_quantity").where(:vendor_id => vendor_id).group("shopify_order_line_items.title")
推荐答案
当你wa nt进行汇总(如SUM或COUNT),只能选择要进行GROUP BY的列。
When you want to do a aggregation like SUM or COUNT, you can only select the columns on which you do the GROUP BY.
ShopifyOrderLineItem.select("shopify_order_line_items.title, count(shopify_order_line_items.id) as unique_ids, sum(amount) as total_price, sum(quantity) as total_quantity").where(:vendor_id => vendor_id).group("shopify_order_line_items.title")
这篇关于Ruby on Rails:必须出现在GROUP BY子句中或在聚合函数中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!