获取 sql 中最大值的行
Get the row with the max value in sql
我想获取列中具有最大值的行。我的情况是我只想 ptint 有 31 的 X。这是我的代码也是输出。
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,但我的 select 将只打印第一辆 Audi。所以这是另一种获取最大值的方法
你可以使用子查询
select marca, totOre
from Private
where totore = (select max(totore) from private)
希望对您有所帮助。
我觉得ALL
构造在这里很有用
SELECT marca, totOre
FROM Private
WHERE totore >= ALL(select totore from private)
我想获取列中具有最大值的行。我的情况是我只想 ptint 有 31 的 X。这是我的代码也是输出。
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,但我的 select 将只打印第一辆 Audi。所以这是另一种获取最大值的方法
你可以使用子查询
select marca, totOre
from Private
where totore = (select max(totore) from private)
希望对您有所帮助。
我觉得ALL
构造在这里很有用
SELECT marca, totOre
FROM Private
WHERE totore >= ALL(select totore from private)