Created 03-15-2018 05:27 AM
Hi,
I have a table, "airports" with below schema in Hive
airport_id string
name string
city string
country string
iata_faa string
icao string
latitude string
longitude string
altitude string
timezone string
dst string
tz string
I want to find the country with max number of airports. I have created below script that is giving me the max number of airport in a country but I am not able to print the country name along with it. Can I get a help pls?
select max(c.airportcount) from
(select count(airport_id) as airportcount,country from airports group by country) c;
Created 03-15-2018 09:11 AM
select count(airport_id) as airportcount, country from airports group by country order by airportcount desc Limit 1;
Created 03-15-2018 09:52 AM
You can use this query which will return the country with max no of airports. It will return also multiple countries if they have same no of airports(max airports)
select c.countryName, c.airportcount from (select d.country as countryName, count(*) as airportcount from airport d group by d.country ) c join (select max(f.airportcount) as countmax from (select cnt.country as countryName, count(*) as airportcount from airport cnt group by cnt.country ) f) g where c.airportcount = g.countmax;
You can also use solution mentioned by @Nde Gerald Awa , but it will not handle if more than 1 country has same no of max aiports.
Thanks,
Aditya
Created 03-15-2018 11:44 AM
Could we do that?
select c.countryName, c.airportcount
from (select d.country as countryName, count(*) as airportcount from airport d group by d.country ) c
where c.airportcount = (select max(f.airportcount) from (select count(*) from airport cnt group by cnt.country)));
Created 03-15-2018 12:32 PM
We cannot run exactly the same query which you have mentioned . I have modified it a bit
select c.countryName, c.airportcount from (select d.country as countryName, count(*) as airportcount from airport d group by d.country ) c where c.airportcount IN (select max(f.airportcount) from (select count(*) as airportcount from airport cnt group by cnt.country)f);
Created 03-15-2018 05:23 PM
Thanks guys for the help. I have used below script. The only modification I have done is, I have removed alias d for airports table. Can any one please advise if keeping alias gives any advantage even though I am getting same result? @Aditya Sirna @Yassine @Nde Gerald Awa
select c.countryName, c.airportcount
from (select country as countryName, count(*) as airportcount from airports group by country ) c
where c.airportcount IN (select max(f.airportcount) from (select count(*) as airportcount from airports cnt group by cnt.country)f);
Created 03-15-2018 05:34 PM
Can you please paste the output/screenshot. This should work
Created on 03-15-2018 06:01 PM - edited 08-18-2019 01:11 AM
Created 03-15-2018 06:15 PM
For me looks like something is retured but not visible. Can you try redirecting the output to a file and check the output
Created 03-15-2018 06:52 PM
I have to check how to re-direct output to a file. The output is generating the count of airports only. It is not showing country due to data issues.