You can unpivot and aggregate:
select city, sum(flight), sum(hotel)
from ((select ogn_city as city, 1 as flight, 0 as hotel
from t
) union all
(select dst_city, 1, 0
from t
) union all
(select city, 0, 1
from t
)
) t
where city is not null;
group by city;
Note: The above uses the contents of the columns to determine if they are included. You can also use mode
instead:
select city, sum(flight), sum(hotel)
from ((select ogn_city as city, 1 as flight, 0 as hotel
from t
where mode = 'flight'
) union all
(select dst_city, 1, 0
from t
where mode = 'flight'
) union all
(select city, 0, 1
from t
where mode = 'hotel'
)
) t
group by city;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…