我有一张包含以下结构的表格。
Flight_ID | Source_city | DestinatinCity
1 NYC LONDON
2 LONDON TOKYO
3 LONDON NYC
我想找到城市数量,即涉及源和目的地的城市,
city | count
LONDON 3
NYC 2
TOKYO 1
如何使用基本SQL功能解决它(没有PL)。
答案 0 :(得分:3)
使用union all
和聚合。
select city,count(*) as cnt
from (
select flight_Id,source_city as city from t
union all
select flight_Id,destination_city from t
) x
group by city
答案 1 :(得分:1)
您可以使用union all
select source_city as city, count(*) from table_name union all select dest_city,count(*) from table_name group by source_city;