I'm not skilled at all in Postgres. Limitations are: I'm using someone else's database so no changes to tables can be made. Postgres 8.3.3 and no upgrading allowed. Accessing the database through pgAdmin III from Windows.
I'm running into a block that's killing me. If a value in one table doesn't exist in the associated table, I get no return at all. Table B has a numerical column (C_id) that references a numerical column (id) in Table C but is optional in Table B, so it can be 0 to indicate empty. There is no 0 record in C.id.
I want to select a complete list from Table B that replaces the numerical value with a label from Table C. This works as long as there is a matching number, but if B.C_id = 0 I get no line at all for that record. So table B has 66 rows but 11 of those have C_id = 0 so I only get 55 rows in my output.
WHERE B.C_id = C.id to match the tables seems to exclude the non-matching rows (B.C_id = 0) but if I leave it out, the results loop a 66 records table into 1300+ lines of output (multiplying the two tables' rows). My searching says COALESCE or LEFT JOIN should work, but COALESCE does not change the output. I presume because the disconnect is on 0 not NULL. I have not been able to get any statements to work with LEFT JOIN or CASE.
To complicate matters, both tables are addressed from another table. A simplified example of the structure:
Table A: (id, label, B_id)
Table B: (id, label, C_id)
Table C: (id, label)
All id fields are numbers and labels are text. All records in A.B_id contain a non-zero number, but B.C_id may contain 0. No rows exist in any table with id = 0.
I want to output to look like:
A.label, C.label
or, with B.C_ID = 0:
A.label, 'none'