The code below almost gets it. I guess I need to explain more. My design docs state (Not done by me, BTW):
"For each record in table A if doc_status is not null then access table B where B.status = A.doc_status. If no record matches then use table B where archive_flag = 'C'."
Table A is a list of sites. Table B contains demographic studies for the sites. There is more than one demographic study for each site. What they are trying to get is the demographic study for a site where the status is equal to the site status. If that doesn't exist, then we want to bring back the demographic study for that site whose demo study archive_flag is 'C', for current.
Quote:
Originally posted by andrewst
That sounds like a cartesian product - for each row in A where condition1 is false, return ALL rows from B where condition2 is true...?
Something like:
Code:
WHERE a.site_id = b.site_id
AND ( (a.doc_status IS NULL OR a.doc_status = b.status)
OR (a.doc_status IS NOT NULL
AND NOT EXISTS (SELECT 1 FROM b b2
WHERE a.doc_status = b2.status)
AND b.archive_flag='C')
)
Seems very odd...
|