Given data that looks similar to this:
+---------+-----------+----------+
| country | city | district |
+---------+-----------+----------+
| Japan | Tokyo | 1 |
| Japan | Tokyo | 1 |
| Japan | Tokyo | 2 |
| China | Shanghai | A |
| China | Shanghai | A |
| China | Shanghai | A |
| China | Beijing | X |
| China | Beijing | Y |
| China | Beijing | Z |
| India | Mumbai | 123 |
| India | Mumbai | 123 |
| India | Mumbai | 123 |
| India | New Delhi | 321 |
| India | New Delhi | 321 |
| India | New Delhi | 321 |
+---------+-----------+----------+
I know I can get the data visually by first doing:
SELECT * from that_table
GROUP BY country, city, district
and I'd get:
+---------+-----------+----------+
| country | city | district |
+---------+-----------+----------+
| Japan | Tokyo | 1 |
| Japan | Tokyo | 2 |
| China | Shanghai | A |
| China | Beijing | X |
| China | Beijing | Y |
| China | Beijing | Z |
| India | Mumbai | 123 |
| India | New Delhi | 321 |
+---------+-----------+----------+
where I can see that only Japan/Tokyo and China/Beijing have multiple values for District. However, I have a huge source of data and I'd like to do that in SQL.
How do I form the SQL query to get all Country/City combinations with multiple Districts?
The output I'd like to achieve is:
+---------+-----------+----------+
| country | city | district |
+---------+-----------+----------+
| Japan | Tokyo | 1 |
| Japan | Tokyo | 2 |
| China | Beijing | X |
| China | Beijing | Y |
| China | Beijing | Z |
+---------+-----------+----------+