2

Im sure there's a simple solution to this but I can't figure it out at all.

I have a table of photos that have states and countries associated with them. I have two simple queries, one to get the number of photos per country:

select country, count(*) from photos group by country;

+-------------+----------+
| country     | count(*) |
+-------------+----------+
| Argentina   |        6 |
| Australia   |        5 |
| Chile       |        3 |
| Ecuador     |       10 |
| France      |        1 |
| Hong Kong   |        4 |
| Indonesia   |        6 |
| Ireland     |       16 |
| Malaysia    |        1 |
| New Zealand |        3 |
| Peru        |        8 |
+-------------+----------+

The other gets the number of photos per state (and country):

select country, state, count(*) from photos group by country, state;

+-------------+---------------------+----------+
| country     | state               | count(*) |
+-------------+---------------------+----------+
| Argentina   | Misiones            |        2 |
| Argentina   | Salta               |        4 |
| Australia   | New South Wales     |        1 |
| Australia   | Victoria            |        4 |
| Chile       | Antofagasta         |        3 |
| Ecuador     | Galapagos           |        5 |
| Ecuador     | Sucumbios           |        5 |
| France      | Aquitaine           |        1 |
| Hong Kong   | Hong Kong           |        4 |
| Indonesia   | Bali                |        3 |
| Indonesia   | Nusa Tenggara Barat |        1 |
| Indonesia   | Yogyakarta          |        2 |
| Ireland     | Antrim              |        1 |
| Ireland     | Cork                |        1 |
| Ireland     | Derry               |        2 |
| Ireland     | Donegal             |        8 |
| Ireland     | Kerry               |        1 |
| Ireland     | Sligo               |        1 |
| Ireland     | Waterford           |        1 |
| Ireland     | Wexford             |        1 |
| Malaysia    | Sabah               |        1 |
| New Zealand | Manawatu            |        2 |
| New Zealand | Westland            |        1 |
| Peru        | Cusco               |        6 |
| Peru        | La Libertad         |        2 |
+-------------+---------------------+----------+

What I'd like to do is to perform a join on these queries that results in the following:

+-------------+---------------------+----------+----------+
| country     | state               |   statet | countryt |
+-------------+---------------------+----------+----------+
| Argentina   | Misiones            |        2 |        6 |
| Argentina   | Salta               |        4 |        6 |
| Australia   | New South Wales     |        1 |        5 |
| Australia   | Victoria            |        4 |        5 |
| Chile       | Antofagasta         |        3 |        3 |
| Ecuador     | Galapagos           |        5 |       10 |
| Ecuador     | Sucumbios           |        5 |       10 |
| France      | Aquitaine           |        1 |        1 |
| Hong Kong   | Hong Kong           |        4 |        4 |
| Indonesia   | Bali                |        3 |        6 |
| Indonesia   | Nusa Tenggara Barat |        1 |        6 |
| Indonesia   | Yogyakarta          |        2 |        6 |
| Ireland     | Antrim              |        1 |       16 |
| Ireland     | Cork                |        1 |       16 |
| Ireland     | Derry               |        2 |       16 |
| Ireland     | Donegal             |        8 |       16 |
| Ireland     | Kerry               |        1 |       16 |
| Ireland     | Sligo               |        1 |       16 |
| Ireland     | Waterford           |        1 |       16 |
| Ireland     | Wexford             |        1 |       16 |
| Malaysia    | Sabah               |        1 |        1 |
| New Zealand | Manawatu            |        2 |        3 |
| New Zealand | Westland            |        1 |        3 |
| Peru        | Cusco               |        6 |        8 |
| Peru        | La Libertad         |        2 |        8 |
+-------------+---------------------+----------+----------+

I've been playing around with joins all morning and can't figure it out, any pointers would be really appreciated!

7 Answers 7

1
select cs.country, cs.state, count(*) as statet, c.countryt
from photos cs
left join (select country, count(*) countryt from photos group by country) as c
on c.country=cs.country
group by cs.country, cs.state;
Sign up to request clarification or add additional context in comments.

Comments

1

Does it work as you expecting?

 SELECT country, state, COUNT(DISTINCT country), COUNT(state)
  FROM photos 
  GROUP BY country, state

Also, try to remove DISTINCT keyword if you need to count the same country per different states

3 Comments

May be, it returns COUNTRY - STATE - NUMBER_OF_COUNTRIES - NUMBER_OF_STATE_PER_COUNTRY, also by removing DISTING country you may get more expanded view
this will always return 1 for country, because every records in country-state is always having same country
Absolutely, and for me it makes more sense rather than return for the unique country name COUNT more thatn 1 ;) But if question author want to see count of unique pairs - then DISTINCT should be removed
1

Personally, I'd go subquery:

select 
     country, 
     state, 
     count(*) as state, 
     (select count(*) from photos inr 
          where inr.country = otr.country) 
from photos otr group by country, state;

1 Comment

WHERE needs to replace HAVING in the subquery.
0

This should do the trick.

SELECT country_state.country, country_state.state, 
       country_state.statet, country.countryt
FROM
(SELECT country, count(*) as countryt FROM photos group by country) AS country
JOIN
(SELECT country, state, count(*) as statet FROM photos group by country, state) AS country_state ON country.country = country_state.country

Comments

0

try

select * 
from (
    select country, state, count(*) from photos group by country, state
) a
inner join (
    select country, count(*) from photos group by country
) b on a.country = b.country

Comments

0

My solution, tested and should give the exact result:

SELECT a.country AS country, p.state AS state, COUNT(*) AS statet, a.count AS countryt 
FROM ( SELECT p1.country, COUNT(*) AS count FROM photos p1 GROUP BY p1.country) a 
JOIN photos p ON (p.country = a.country) 
GROUP BY p.state;

Enjoy!

Comments

0

To avoid having to query your table twice, you could use analytics.

select country
  , state
  ,group_by_country
  ,group_by_state
from (select country
       , state
       , count(*) over  (partition by country) group_by_country
       , count(*) over  (partition by country,state) group_by_state
       ,row_number() over (partition by country,state order by country,state) row_num
  from photos
  order by country,state)
where row_num = 1;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.