Oracle SQL grouping to one row

时间:2017-06-20 12:34:16

标签: sql oracle

I have the following table

+---------+----------+------------+------------+----------+----------+
| PN      | code     | date       | F2         | PO       | Supplier |
+---------+----------+------------+------------+----------+----------+
| CDS0055 | NBR00008 | 16.06.2017 | 19.06.2017 | 9872786  | S00021XC |
| CDS0055 | NBR00008 | 16.06.2017 | 03.07.2017 |          | S00021XC |
| CDS0055 | NBR00008 | 16.06.2017 | 04.07.2017 |          | S0000000 |
+---------+----------+------------+------------+----------+----------+

and I want to get this result (group min (f2)):

+---------+----------+------------+------------+---------+----------+
| PN      | code     | date       | F2         | PO      | Supplier |
+---------+----------+------------+------------+---------+----------+
| CDS0055 | NBR00008 | 16.06.2017 | 19.06.2017 | 9872786 | S00021XC |
+---------+----------+------------+------------+---------+----------+

Query:

select distinct PN, min(f2), date, PO, siplier from Order
group by pn, po, date, supplier

1 个答案:

答案 0 :(得分:0)

您可以使用ROW_NUMBER()分析函数获取每个组中的列的最小值(以及所有其他相应的值):

SELECT *
FROM   (
  SELECT t.*,
         ROW_NUMBER() OVER (
             PARTITION BY pn,po,"date",supplier
             ORDER BY f2
         ) AS rn
  FROM   your_table t
)
WHERE  rn = 1