Select airport airport_code , Round(AVG(cder/31),0) flights_count from
(Select distinct airport , sum(cdep) cder from
(Select distinct departure_airport as airport, count(*) cdep from flights
where actual_departure is not null and actual_departure>'20170801'
group by departure_airport
union all
Select distinct arrival_airport , count(*) cdep from flights
where actual_arrival is not null and actual_arrival>'20170801'
group by arrival_airport) l
group by airport) k
group by airport
having AVG(cder/31) <=3
order by AVG(cder/31), airport
;
select * from query_cost('Select airport airport_code , Round(AVG(cder/31),0) flights_count from
(Select distinct airport , sum(cdep) cder from
(Select distinct departure_airport as airport, count(*) cdep from flights
where actual_departure is not null and actual_departure>''20170801''
group by departure_airport
union all
Select distinct arrival_airport , count(*) cdep from flights
where actual_arrival is not null and actual_arrival>''20170801''
group by arrival_airport) l
group by airport) k
group by airport
having AVG(cder/31) <=3
order by AVG(cder/31), airport
');