Christopher Kings-Lynne <chriskl@familyhealth.com.au> writes:
> I'm trying to avoid doing it in C as it seems like it would be a pita.
I think it would be simpler in C than this mess in SQL is ;-). You
would not of course implement it in any way that would look like the
SQL query ... but there are existing utility subroutines for most of
the bits that are being done with joins here.
regards, tom lane