2

I want to get the row with the maximum value on a column. I my case I want to ptint only X that has 31. This is my code and also the output.

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

But it's a wrong approach, for example without the X , Audi or BMW has 7 but my select will print just the first Audi. So it's another method to get the maximum value

a_horse_with_no_name
  • 497,550
  • 91
  • 775
  • 843
Trip Therapy
  • 307
  • 5
  • 16

2 Answers2

4

You can use subquery

select marca, totOre
from Private 
where totore = (select max(totore) from private)

Hope it will help.

Pirate
  • 2,672
  • 4
  • 21
  • 39
2

I feel that ALL construct can be useful here

SELECT marca, totOre
FROM Private 
WHERE totore >= ALL(select totore from private)
Radim Bača
  • 10,526
  • 1
  • 18
  • 32