这是我的疑问。
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...
有什么建议吗?
我试过这个,但它没有用。
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")
答案 0 :(得分:1)
如果要执行SUM或COUNT之类的聚合,则只能选择执行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")