So currently my query shows result of all the "preferred" travel destinations of countries based on the continent, and I'm adding a having clause to show the continents that have more than 1 preferred destination. But as I soon as I add more columns I want to show, let's the say cities, my query returns nothing.
This is the query that work, it shows all continents that have more than 2 preferred countries as travel destination(the preferred flag is Y).
select d.continent,d.country_name,di.is_preferred, count(1)
from continent c, countries ct
where c.cid = ct.id
and ct.preferred = Y
group by d.continent,d.country_name,ct.is_preferred
having count(1) > 1
order by d.country_name asc;`
Let's say I want to have the same result showing, but I want to show all the list of cities of the countries that where continent have more than 1 preferred country. The cities column is from continent C table, as soon as I add to the having statement, the result is blank?
What did I do wrong?
Here is my modified query:
select d.continent,d.country_name,di.is_preferred, c.cities, count(1)
from continent c, countries ct
where c.cid = ct.id
and ct.preferred = Y
group by d.continent,d.country_name,ct.is_preferred, c.cities
having count(1) > 1
order by d.country_name asc;
I've tried modified my queries but got nothing in my result
If you
GROUP BY continent, country_namethen you are finding the pairing of continent and country where there are 2-or-more preferred entries; if you want it bycontinentonly (as per the question) then you only want toGROUP BY continent.If you have the sample data:
and you want to
GROUP BYeachcontinentand show a list of the preferredcitiesthen onlyGROUP BY continentand useLISTAGGto aggregate the names of the cities into a delimited list:Which outputs:
fiddle