db2 - SQL query that will retrieve set containing all entries from another set -
i have following relations in db:
organization: information political , economical organizations.
name: full name of organization
abbreviation: abbreviation
ismember: memberships in political , economical organizations.
organization: abbreviation of organization
country: code of member country
geo_desert: geographical information deserts
desert: name of desert
country: country code located
province: province of country
my task retrieve organizations have within members full set of countries deserts. organization can have countries without deserts. have set of countries deserts , every organization in result should have of them members , arbitrary amount of other (no desert) countries.
i tried far write following code, doesn't work.
with countrieswithdeserts ( select distinct country dbmaster.geo_desert ), organizationswithalldesertmembers ( select organization dbmaster.ismember ism ( select count(*) ( select * countrieswithdeserts except select country dbmaster.ismember organization = ism.organization ) ) null ), organizationcode ( select name, abbreviation dbmaster.organization ) select oc.name organization organizationcode oc, organizationswithalldesertmembers owadm oc.abbreviation=owadm.organization;
upd: dbms says: "ism.organization not defined"
i'm using db2/linuxx8664 9.7.0
output should this:
name
--------------------------------------------------------------------------------
african, caribbean, , pacific countries
african development bank
agency cultural , technical cooperation
andean group
i find easiest way handle using group by
, having
. want focus on deserts, rest of countries don't matter.
select m.organization ismember m join geo_desert d on m.country = d.country group m.organization having count(distinct m.country) = (select count(distinct d.country) geo_desert);
the having
clause counts number of matching (i.e. desert) countries , checks included.
Comments
Post a Comment