【发布时间】:2018-02-09 08:24:02
【问题描述】:
我想获取列上具有最大值的行。在我的情况下,我只想对具有 31 的 X 进行 ptint。这是我的代码,也是输出。
create temp view Private(marca) as
Select a.marca, count(*) as totaleNoleggi, count(distinct a.targa) as totaleAuto, sum(extract(hour from (n.fine-n.inizio))) as totOre
from auto a join noleggio n on a.targa=n.targa
group by a.marca;
select marca, totOre
from Private
group by marca,totore
order by totore desc
limit 1;
marca totalenoleggi totaleauto totore
1 Audi 1 1 7
2 BMW 5 4 7
3 VW 2 1 1
4 X 2 1 31
marca totore
1 X 31
但这是错误的方法,例如没有 X , Audi 或 BMW 有 7 但我的选择将只打印第一个 Audi。所以这是另一种获取最大值的方法
【问题讨论】:
-
在不涉及聚合函数的情况下为什么要使用 GROUP BY?
标签: sql postgresql greatest-n-per-group postgresql-9.4