通过select查询获取组中的id

Get the id in a group by select query

我有以下table

CREATE TEMPORARY TABLE temp_detail
(
  purchase_order_detail_id INTEGER,
  item_id integer,
  qty numeric(18,2),
  project_id integer,
  category_id integer,
  supplier_id integer,
  rate numeric(18,2)
)

我想获取 purchase_order_detail_id 行,这些行具有相同的 project_id、category_id 和 supplier_id。使用 group by project_id、category_id、supplier_id 不会得到 purchase_order_detail_id。请帮忙。

您可以使用:

SELECT array_agg(purchase_order_detail_id), project_id, category_id, supplier_id 
   FROM temp_detail 
   GROUP BY project_id, category_id, supplier_id

获取数组中对应的purchase_order_detail_id