正如标题所示,我想选择用GROUP BY分组的每组行中的第一行。
具体来说,如果我有一个如下所示的采购表:
SELECT * FROM purchases;
我的输出:
id |
customer |
total |
1 |
Joe |
5 |
2 |
Sally |
3 |
3 |
Joe |
2 |
4 |
Sally |
1 |
我想查询每个客户的最大购买量(总购买量)。类似于:
SELECT FIRST(id), customer, FIRST(total)
FROM purchases
GROUP BY customer
ORDER BY total DESC;
预期输出:
FIRST(id) |
customer |
FIRST(total) |
1 |
Joe |
5 |
2 |
Sally |
3 |
我通过窗口函数dbfiddle的方法:
将每组的row_number()分配给(按agreement_id、order_id划分)为nrow只取组:filter中的第一行(其中nrow=1)
with intermediate as (select
*,
row_number() over ( partition by agreement_id, order_id ) as nrow,
(sum( suma ) over ( partition by agreement_id, order_id ))::numeric( 10, 2) as order_suma,
from <your table>)
select
*,
sum( order_suma ) filter (where nrow = 1) over (partition by agreement_id)
from intermediate
如果要从聚合行集合中选择任何行(根据特定条件)。如果您想使用除max/min之外的另一个(sum/avg)聚合函数。因此,您不能在DISTINCT ON时使用线索
您可以使用下一个子查询:
SELECT
(
SELECT **id** FROM t2
WHERE id = ANY ( ARRAY_AGG( tf.id ) ) AND amount = MAX( tf.amount )
) id,
name,
MAX(amount) ma,
SUM( ratio )
FROM t2 tf
GROUP BY name
您可以将amount=MAX(tf.amount)替换为任何需要的条件,但有一个限制:此子查询不能返回多行
但是如果你想做这样的事情,你可能需要寻找窗口函数