* #1 SELECT genus, species ; FROM species JOIN site ON species.pkey = site.fkey ; WHERE site.continent IN ('Asia', 'Africa') ; GROUP BY 1,2 ; HAVING COUNT(*) = 2 ; INTO CURSOR curBoth * #2 SELECT genus, species, 'Asia ' AS continent ; FROM curBoth ; UNION ALL ; SELECT genus, species, 'Africa' AS continent ; FROM curBoth ; INTO CURSOR curAA * Or another #2 SELECT genus, species, continent ; FROM species JOIN site ON species.pkey = site.fkey ; WHERE site.continent IN ('Asia', 'Africa') ; AN genus + species IN ( ; SELECT genus + species FROM curBoth) INTO CURSOR curAA>I'm struggling to write SQL code that identifies the intersection of some values. The tables in question are a site table (containing location of fauna) and species table (containing genus, species, etc). Here's my problem: I want to select all genus and species from the species table that are found in both Asia and Africa (in the site table). The output table should contain 2 records for each common genus and species, one for Asia and one for Africa.
>First SQL statement: > >Select genus, species, continent, genus+species as gsp ; > from species, site ; > where site.continent = 'Asia' ; > species.pkey = site.fkey ; > into cursor curAsia > >Second SQL statement: > >Select genus, species, continent, genus+species as gsp ; > from species, site ; > where site.continent = 'Africa' ; > species.pkey = site.fkey ; > into cursor curAfrica > >Third SQL Statement: > >Select curAsia.genus, curAsia.species, curAsia.continent ; > from curAsia ; > where curAsia.gsp in (select curAfrica.gsp from curAfrica) ; > into cursor curAA ; > order by 1, 2 ; > union ; > Select curAfrica.genus, curAfrica.species, curAfrica.continent ; > from curAfrica ; > where curAfrica.gsp in (select curAsia.gsp from curAsia)