通过选择查询获取组中的ID

时间:2015-12-31 10:19:35

标签: sql postgresql postgresql-9.3

我有下表

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

我想获取具有相同project_id,category_id和supplier_id的行的purchase_order_detail_id。使用group by project_id,category_id,supplier_id将不会给出purchase_order_detail_id。请帮忙。

1 个答案:

答案 0 :(得分:1)

您可以使用:

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