select custid, keyid from patients where between(age, 0, 18) group by custid into cursor crsUniqueChildren select custid, keyid from patients where age > 18 group by custid into cursor crsUniqueAdults select insurance, count(*) kount from patients where keyid in (select keyid from crsUniqueChildren) ; group by insurance ; into cursor crsChild select insurance, count(*) kount from patients where keyid in (select keyid from crsUniqueAdults) ; group by insurance ; into cursor crsAdult select *, 'C' Age from crsChild ; union ; select *, 'A' Age from crsAdult ; order by 1 ; into cursor crsKounts