Getting non_NULL right-side values on a non-matching join?

From: Ken Tanzer <ken(dot)tanzer(at)gmail(dot)com>
To: PG-General Mailing List <pgsql-general(at)postgresql(dot)org>
Cc: Michael James <mjames(at)plymouthhousing(dot)org>
Subject: Getting non_NULL right-side values on a non-matching join?
Date: 2013-11-21 21:25:45
Message-ID: CAD3a31XQCYnAOw590B7qcNhCRfC-QXuNev4qzp7Xj+Y4BzspMQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello. In doing a left join with a particular view as the right table, and
non-matching join criteria, I am getting values returned in a few fields.
All the rest are NULL. I would expect all the right side values to be
NULL.

(The view is large and messy, but it doesn't seem like that should matter.
I've attached the create statement for the view anyway.)

Am I missing something really really obvious about LEFT JOINs here? This
statement seems to confirm my expectations:

"This query is called a left outer join because the table mentioned on the
left of the join operator will have each of its rows in the output at least
once, whereas the table on the right will only have those rows output that
match some row of the left table. *When outputting a left-table row for
which there is no right-table match, empty (null) values are substituted
for the right-table columns.*"
http://www.postgresql.org/docs/9.2/static/tutorial-join.html

Thanks in advance.

Ken

-- Nothing special about the -1 id here, just an example of a non-matching
value.

ag_spc=> SELECT foo.client_id AS foo_id,rent_info.client_id AS
ri_id,move_in_type,annual_income,monthly_income_total,dependent_count from
(SELECT -1 AS client_id) foo LEFT JOIN rent_info USING (client_id);
foo_id | ri_id | move_in_type | annual_income | monthly_income_total |
dependent_count
--------+-------+--------------+---------------+----------------------+-----------------
-1 | | Move-in | 0 | 0 |
0
(1 row)

ag_spc=> SELECT * FROM rent_info WHERE client_id = -1;
effective_date | effective_date_end | rent_amount_tenant_calculated |
rent_amount_tenant | rent_amount_spc | project_date | own_date |
is_active_manual | residence_own_id | client_id | housing_project_code |
housing
_project_label | housing_unit_code | residence_date | residence_date_end |
was_received_hap | was_received_compliance | moved_from_code |
chronic_homeless_status_code | move_in_type | lease_on_file | moved_to_code
|
moved_to_unit | departure_type_code | departure_reason_code |
move_out_was_code | returned_homeless | unit_rent_manual |
tenant_pays_deposit | comment_damage | comment_deposit | comment |
income_id | income_date | in
come_date_end | annual_income | monthly_income_total |
monthly_income_primary | income_primary_code | monthly_income_secondary |
income_secondary_code | monthly_income_tertiary | income_tertiary_code |
monthly_intere
st_income | other_assistance_codes | income_certification_type_code |
child_care | handicap_assistance | medical_expense | fund_type_code |
rent_date_effective | rent_date_end | housing_unit_id | housing_unit_label |
housing_unit_date | housing_unit_date_end | unit_type_code | tax_credit |
max_occupant | alternate_address_id | mailing_address_unit | street_address
| mailing_address_client | housing_unit_subsidy_id | housing_unit
_subsidy_date | housing_unit_subsidy_date_end | unit_subsidy_amount |
unit_rent | tenant_vendor_number | vendor_number | utility_allowance_manual
| utility_allowance_unit | utility_allowance | utility_allowance_code
| security_deposit | fair_market_rent | rent_amount_tenant_manual |
dependent_count | cid | reg_spc_date | reg_spc_date_end | grant_number_code
| agency_code | agency_phone | agency_label | agency_contact | fake_key
----------------+--------------------+-------------------------------+--------------------+-----------------+--------------+----------+------------------+------------------+-----------+----------------------+--------
---------------+-------------------+----------------+--------------------+------------------+-------------------------+-----------------+------------------------------+--------------+---------------+---------------+-
--------------+---------------------+-----------------------+-------------------+-------------------+------------------+---------------------+----------------+-----------------+---------+-----------+-------------+---
--------------+---------------+----------------------+------------------------+---------------------+--------------------------+-----------------------+-------------------------+----------------------+---------------
----------+------------------------+--------------------------------+------------+---------------------+-----------------+----------------+---------------------+---------------+-----------------+--------------------+
-------------------+-----------------------+----------------+------------+--------------+----------------------+----------------------+----------------+------------------------+-------------------------+-------------
--------------+-------------------------------+---------------------+-----------+----------------------+---------------+--------------------------+------------------------+-------------------+------------------------
+------------------+------------------+---------------------------+-----------------+-----+--------------+------------------+-------------------+-------------+--------------+--------------+----------------+----------
(0 rows)

--
AGENCY Software
A data system that puts you in control
100% Free Software
*http://agency-software.org/ <http://agency-software.org/>*
ken(dot)tanzer(at)agency-software(dot)org
(253) 245-3801

Subscribe to the mailing
list<agency-general-request(at)lists(dot)sourceforge(dot)net?body=subscribe>
to
learn more about AGENCY or
follow the discussion.

Attachment Content-Type Size
create.view.rent_info.sql text/x-sql 8.0 KB

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Joey Quinn 2013-11-21 21:38:11 Re: Primary Key
Previous Message Mike Broers 2013-11-21 21:07:51 Re: corruption issue after server crash - ERROR: unexpected chunk number 0