I have a query that is 99% working, but I cannot query NULL
or empty records for
dbo.HUR_STAFF_DEMOGRAPHICS.DEMOGRAPHICS_POSITION = '' OR
dbo.HUR_STAFF_DEMOGRAPHICS.DEMOGRAPHICS_POSITION IS NULL
I tried both, but the SQL statement below returns no rows if I add the lines above. However it DOES query rows for ALL staff that have a location and or position entered into the database, but my goal is to query this information for all staff that have a location and or position in the database, or if have a location and or position is NULL or BLANK (empty).
I queried the Demographics
table alone, and I get a total of 1120 records, I also calculated the columns that have BLANK or NULL
(so no position title or Location) and that is 316 records.
Sure enough, when I run this query I get 804 records returned, which means the query is not querying Null
or empty columns for Location
or Position Title
.
Can anyone see why the query will not return the null columns?
SELECT DISTINCT
dbo.HUR_STAFF_DEMOGRAPHICS.employee_no,
first_name, last_name, PREFERRED_NAME,
location,
DEMOGRAPHICS_POSITION,
EMPLOYEE_EMAIL,
Location_name,
Description
FROM
dbo.HUR_STAFF_DEMOGRAPHICS,
dbo.HUR_STAFF_INTERLINK,
dbo.HUR_LOCATION,
dbo.HUR_POSITION_CODE,
dbo.HUR_EMAIL_ADDRESS
WHERE
dbo.HUR_STAFF_DEMOGRAPHICS.employee_no = dbo.HUR_STAFF_INTERLINK.employee_no
AND dbo.HUR_STAFF_DEMOGRAPHICS.location = dbo.HUR_LOCATION.Location_no
AND dbo.HUR_STAFF_DEMOGRAPHICS.DEMOGRAPHICS_POSITION = dbo.HUR_POSITION_CODE.POSITION_CODE
AND dbo.HUR_STAFF_DEMOGRAPHICS.EMPLOYEE_NO = dbo.HUR_EMAIL_ADDRESS.EMPLOYEE_NO
AND dbo.HUR_STAFF_DEMOGRAPHICS.employee_status = 'A'
AND dbo.HUR_STAFF_DEMOGRAPHICS.DEMOGRAPHICS_POSITION IS NULL
ORDER BY
last_name
Thanks in advance for your time and expertise.
1条答案
按热度按时间3htmauhk1#
I believe your issue is that you are joining to the
DEMOGRAPHICS_POSITION
table onDEMOGRAPHICS_POSITION = POSITION_CODE
, but are also looking forDEMOGRAPHICS_POSITION = '' or DEMOGRAPHICS_POSITION = NULL
values. Assuming that theDEMOGRAPHICS_POSITION
table has no empty code entry, the join to that table will fail for both of these conditions and no record will be returned.The solution is to change that join to a
LEFT JOIN
, but the query must be first rewritten to use ANSI join syntax.Using table aliases is also a good practice to simplify the statement and improve readability.
Try:
Alternately, for this particular usage, you can drop the
HUR_POSITION_CODE
join entirely, along with the corresponding select list references (Is that whereDescription
comes from?)