选择组中每个组的最大记录

时间:2015-05-10 22:28:47

标签: sql postgresql aggregate-functions greatest-n-per-group

我正在使用PostgreSQL。 我需要选择每个组的最大值,情况是该表代表每天销售的产品,我想知道每天销售量最高的产品。

SELECT sum(detalle_orden.cantidad) as suma,detalle_orden.producto_id as producto
      ,to_char(date_trunc('day',orden.fecha AT TIME ZONE 'MST'),'DY') as dia
FROM detalle_orden
LEFT JOIN orden ON orden.id = detalle_orden.order_id
GROUP BY orden.fecha,detalle_orden.producto_id 
ORDER BY dia,suma desc

回归:

suma  producto  dia
4     1         FRI
1     2         FRI
5     3         TUE
2     2         TUE

我想得到:

suma  producto  dia
4     1         FRI
5     3         TUE

仅限每天的最佳产品(每组max(suma))。

我尝试了不同的方法,比如子查询,但使用的聚合函数使事情变得有点困难。

3 个答案:

答案 0 :(得分:2)

您可以(ab)使用SELECT DISTINCT ON和相应的排序条款。假设您将之前的查询放入视图中:

SELECT DISTINCT ON (dia, producto) * FROM some_view ORDER BY dia, producto, suma DESC;

DISTINCT确保您每天只保留一行产品,ORDER BY确保其保留正确的

答案 1 :(得分:1)

通过窗口函数:RANK,您可以轻松获得它:

select * from
(
select suma,producto,dia, rank() over (partition by dia order by suma desc) as ranking
from your_query
)A
where ranking = 1

所以你最终的查询将是:

select * from
(
select suma,producto,dia, rank() over (partition by dia order by suma desc) as ranking
from 
(
SELECT sum(detalle_orden.cantidad) as suma,detalle_orden.producto_id as     producto,to_char(date_trunc
    ('day',orden.fecha AT TIME ZONE 'MST'),'DY') as dia FROM detalle_orden     LEFT JOIN
    orden ON orden.id= detalle_orden.order_id GROUP by
    orden.fecha,detalle_orden.producto_id ) B
) A
where ranking = 1

答案 2 :(得分:1)

您仍然可以使用DISTINCT ON在没有子查询的单个查询级别中完成此操作,因为在DISTINCT和聚合函数之后(以及在窗口函数之后)应用GROUP BY

SELECT DISTINCT ON (3)
       sum(d.cantidad) AS suma
     , d.producto_id AS producto
     , to_char(o.fecha AT TIME ZONE 'MST', 'DY') AS dia
FROM   detalle_orden d
LEFT   JOIN orden o ON o.id = d.order_id
GROUP  BY o.fecha, d.producto_id 
ORDER  BY 3, 1 DESC NULLS LAST, d.producto_id;

注释