在 PostgreSQL 中是否不需要将唯一列添加到 group by 子句中?
我通常使用 Microsoft SQL Server,我们必须将所有需要的列添加到 group by 子句中。
PostgreSQL 查询:
DROP TABLE IF EXISTS temp_invoice_detail;
CREATE TEMP TABLE temp_invoice_detail(
invoice_id integer,
item_id integer,
qty integer,
warehouse_id integer
);
ALTER TABLE temp_invoice_detail ADD CONSTRAINT temp_invoice_detail_result_pkey PRIMARY KEY (invoice_id, item_id);
insert into temp_invoice_detail (invoice_id, item_id, qty, warehouse_id) values (1, 1, 100, 1);
insert into temp_invoice_detail (invoice_id, item_id, qty, warehouse_id) values (1, 2, 200, 1);
insert into temp_invoice_detail (invoice_id, item_id, qty, warehouse_id) values (2, 1, 100, 1);
select invoice_id, item_id, sum(qty) as total_qty, warehouse_id
from temp_invoice_detail
group by invoice_id, item_id --should I add "warehouse_id" in group by clause?
order by invoice_id, item_id;
DROP TABLE IF EXISTS temp_invoice_detail;
我希望 PostgreSQL 显示错误消息:
列“temp_invoice_detail.warehouse_id”必须出现在 GROUP BY 子句中或在聚合函数中使用
但是查询成功运行并返回 3 条记录(上面插入语句中的所有 3 条记录)。
invoice_id item_id total_qty warehouse_id
1 1 100 1
1 2 200 1
2 1 100 1
在 MS-SQL 中,为了让这个查询可以正常运行,group by 子句必须是:
按 invoice_id、item_id、warehouse_id 分组