Postgresql错误:必须出现在GROUP BY子句中或用于聚合函数

时间:2015-03-27 11:47:34

标签: sql postgresql

在你把它标记为重复之前,我尝试了在同一个网站上给出的几个解决方案,但没有一个有效。

我的查询

SELECT 
  temp_fk_settlements.sku, 
  temp_fk_settlements.order_item_id, 
  temp_fk_settlements.order_date, 
  sum(temp_fk_settlements.order_item_value), 
  sum(temp_fk_settlements.total_marketplace_fee), 
  sum(temp_fk_settlements.service_tax), 
  sum(temp_fk_settlements.settlement_value), 
  sum(temp_fk_settlements.commission), 
  sum(temp_fk_settlements.fixed_fee), 
  sum(temp_fk_settlements.shipping_fee), 
  sum(temp_fk_settlements.reverse_shipping_fee), 
  sum(temp_fk_settlements.invoice_amount)
FROM 
  temp_fk_returns, 
  temp_fk_settlements
WHERE
  temp_fk_settlements.order_item_id = temp_fk_returns.order_item_id and
  temp_fk_returns.return_type = 'customer_return'
GROUP BY
  temp_fk_settlements.order_item_id

我的错误

ERROR:  column "temp_fk_settlements.sku" must appear in the GROUP BY clause or     
 be used in an aggregate function
LINE 2:   temp_fk_settlements.sku, 

如果我尝试删除列temp_fk_settlements.sku,它仍会在不同的列上给出错误。我被困住,因为没有一个解决方案有效。

我想根据order_item_id对结果进行分组。

我的表结构是

temp_fk_settlements:

Create table temp_fk_settlements(
    settlement_reference_no varchar(50),
    order_type char(20),
    fulfillment_type varchar(25),
    sku varchar(25),
    order_id varchar(25),
    order_item_id varchar(25),
    order_date date,
    dispatch_date date,
    delivery_date date,
    cancellation_date date,
    settlement_date date,
    order_status varchar(25),
    quantity integer,
    order_item_value integer,
    refund integer,
    protection_fund integer,
    total_marketplace_fee double precision,
    service_tax double precision,
    settlement_value double precision,
    commission_rate integer,
    commission double precision,
    fee_discount double precision,
    cancellation_fee double precision,
    fixed_fee integer,
    emi_fee double precision,
    total_weight float,
    shipping_fee double precision,
    reverse_shipping_fee double precision,
    shipping_zone char(15),
    token_of_apology double precision,
    pick_and_pack_fee double precision,
    storage_fee double precision,
    removal_fee double precision,
    invoice_id varchar,
    invoice_date date,
    invoice_amount integer,
    sub_category char(25),
    total_offer_amount double precision,
    my_offer_share double precision,
    flipkart_offer_share double precision
)

temp_fk_returns:

CREATE TABLE temp_fk_returns(
    return_id VARCHAR(25),
    order_id VARCHAR(25),
    order_item_id VARCHAR(25),
    product_id VARCHAR(25),
    title VARCHAR(100),
    sku VARCHAR(25),
    quantity INTEGER,
    total_price INTEGER,
    return_requested_on DATE,
    return_initiated_on DATE,
    return_tracking_id VARCHAR(25),
    status VARCHAR(25),
    reason VARCHAR(150),
    sub_reason VARCHAR(150),
    customer_comments VARCHAR,
    return_type VARCHAR(25)
)

2 个答案:

答案 0 :(得分:2)

如果查看查询的逻辑,可以看到为什么发生此错误:

SELECT 
  temp_fk_settlements.sku, 
  temp_fk_settlements.order_item_id, 
  temp_fk_settlements.order_date

这表示对于每一行,您需要skuorder_item_idorder_date

GROUP BY
  temp_fk_settlements.order_item_id

这表示每个唯一order_item_id都需要一行,无论其他什么不同。

因此,如果同一sku的{​​{1}}或order_date超过order_item_id,则数据库无法知道这些值中的哪一个。 MySQL只是猜测,但Postgres(像大多数其他数据库系统一样)告诉你改变你的查询。

在这种情况下,您可能知道每个order_item_id对应一个skuorder_date的值,因此您只需将它们全部添加到GROUP BY子句中:

GROUP BY
  temp_fk_settlements.sku, 
  temp_fk_settlements.order_item_id, 
  temp_fk_settlements.order_date

这表示您希望这三个字段的每个唯一组合都有一行,因此可以查看组合中的所有项目。

答案 1 :(得分:1)

所有非聚合列都应该在GROUP BY中...而不仅仅是......

之一

所以

group by 
  temp_fk_settlements.sku, 
  temp_fk_settlements.order_item_id, 
  temp_fk_settlements.order_date

select 
string_agg(temp_fk_settlements.sku,',') sku,
temp_fk_settlements.order_item_id, 
string_agg(order_date.order_date,',') order_date,
...etc
GROUP BY
  temp_fk_settlements.order_item_id