I am attempting to get counts of items in multiple columns in a union all
-ed query.
I have a MySQL database of trips taken, with location data for origin and destination for each trip. The columns are as such:
o_continent | o_country | o_state | o_city | d_continent | d_country | d_state | d_city | tripdate |
---|---|---|---|---|---|---|---|---|
North America | United States | California | San Diego | North America | United States | Arizona | Phoenix | 2020-10-01 |
North America | United States | California | Los Angeles | North America | United States | California | Santa Rosa | 2020-04-03 |
North America | United States | California | Los Angeles | North America | United States | Washington | Spokane | 2020-03-01 |
North America | United States | Oregon | Portland | North America | Canada | Alberta | Calgary | 2020-01-22 |
North America | Canada | Alberta | Edmonton | North America | United States | California | Los Angeles | 2020-04-01 |
I am merging the origins and the destinations using UNION ALL
, as such:
SELECT o__continent as continent, o__country as country, o__state_name as state, o__city_name as city, o_ AS code
FROM db.trips
WHERE year(tripdate) = 2020
UNION ALL
SELECT d__continent as continent, d__country as country, d__state_name as state, d__city_name as city, d_ AS code
FROM db.trips
WHERE year(tripdate) = 2020
This produces a table that looks like this:
continent | country | state | city |
---|---|---|---|
North America | Canada | Alberta | Calgary |
North America | Canada | Alberta | Edmonton |
North America | United States | Arizona | Phoenix |
North America | United States | California | Los Angeles |
North America | United States | California | Los Angeles |
North America | United States | California | Los Angeles |
North America | United States | California | San Diego |
North America | United States | California | Santa Rosa |
North America | United States | Oregon | Portland |
North America | United States | Washington | Spokane |
I am seeking out how I might be able to group these together (obviously traditionally you would use GROUP BY continent, country, state, city
), but also to count the instances of each column in separate columns. The ideal final result would look something like this (note: the Los Angeles
entries are grouped):
continent | country | state | city | continent_count | country_count | state_count | city_count |
---|---|---|---|---|---|---|---|
North America | Canada | Alberta | Calgary | 10 | 2 | 2 | 1 |
North America | Canada | Alberta | Edmonton | 10 | 2 | 2 | 1 |
North America | United States | Arizona | Phoenix | 10 | 8 | 1 | 1 |
North America | United States | California | Los Angeles | 10 | 8 | 5 | 3 |
North America | United States | California | San Diego | 10 | 8 | 5 | 1 |
North America | United States | California | Santa Rosa | 10 | 8 | 5 | 1 |
North America | United States | Oregon | Portland | 10 | 8 | 1 | 1 |
North America | United States | Washington | Spokane | 10 | 8 | 1 | 1 |
I have been sifting for several days through other potential answers to this question on StackOverflow with little luck. Ideally I would not query the db.trips
database over and over, as it is large. Is there a way to do this without losing too much efficiency?
Any help offered is most kindly and greatly appreciated.