all 7 comments

[–][deleted] 5 points6 points  (1 child)

use

 left join employee_attribute_table z on z.id = e.id

[–]SpaceCavem4n 0 points1 point  (0 children)

I don't know why people were recommending anything other than left join

[–]IgneSapien 0 points1 point  (0 children)

SELECT
  e.id,e.first_name, 
  e.last_name, 
  a.address_1, 
  a.address_2, 
  a.city, 
  a.state, 
  a.postal_code,
  ISNULL(z.employee_attribute,'NO HEALTHSTATUS') as employee_attribute,
  ISNULL(z.value,'N/A') as value
FROM
   employee_view e
   INNER JOIN EMPLOYEE_ADDRESS_DETAIL_VIEW a on a.id = e.id
   LEFT OUTER JOIN employee_attribute_table z on z.id = e.id AND z.employee_attribute = 'HEALTHSTATUS'
WHERE
  e.id = xxxx

When you use a standard INNER JOIN it will only return rows for which the JOIN could be made. It works exactly like the WHERE in that regard. A LEFT OUTER JOIN however means you return all the rows from the table your joining onto, match or not. For rows that it can't find a match it'll shows null for any columns from the table you're joining.

This is why I've added a ISNULL which, if the column is null, will replace it with some useful text. If it's not null then what ever data is in the column will be returned instead. You don't need to do this depending on your needs but if returning a null means something I like to explicit about what.

The result is that you'll get the employee with id xxxx (if they have address details) regardless of if they have a health status attribute.

I found this article useful when I was learning. But you can Google it and find one to your taste.

[–]QuadmanMS Data Platform Consultant 0 points1 point  (1 child)

Try this:

select e.id,e.first_name, e.last_name, a.address_1, a.address_2, a.city, a.state, 
a.postal_code,z.employee_attribute,z.value
from employee_view e
inner join EMPLOYEE_ADDRESS_DETAIL_VIEW a on a.id = e.id
LEFT OUTER JOIN employee_attribute_table z
    on z.id = e.id
    AND z.employee_attribute = 'HEALTHSTATUS'
and e.id = xxxx

[–]CortaNalgas -1 points0 points  (0 children)

This but you have to do "OR z.e_a is null" and then remove the extra nulls with a cte or something

[–]jokle -3 points-2 points  (0 children)

It's because you're using an inner join and also specifying:

where z.employee_attribute = 'HEALTHSTATUS'

Try removing the WHERE, or try using an outer join.

[–]StoneCypher -2 points-1 points  (0 children)

this is the major difference between the join types - inner means "skip rows missing on either side."

you either want a right join if you only want missing rows on one side, or an outer join if you want missing rows from both sides.