-1

This is my query.

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")

I'm getting the following exception.

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...

Any suggestions?

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")
Kirti Thorat
  • 52,578
  • 9
  • 101
  • 108
Kevin
  • 304
  • 3
  • 14

1 Answers1

1

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")
Raj
  • 22,346
  • 14
  • 99
  • 142
  • What if I'm interested in other columns in shopify_order_line_items table that I don't want to group by? – Kevin May 02 '14 at 16:55