Sql-server – SQL query not selecting records

sql server

I'm trying to select * records. The problem is some only have values ('MNT', 'NPT', 'OM') for attrib_type 1-3 in this case. Attribute types 4 & 5 do not exist in the older records therefore there are 4500 records that are not being selected. I would like to be able to select ALL records even where there is no attrib_type 4 & 5 value.

The SQL I have tried is below

SELECT
   central_site.site_code,
   central_site.site_name,
   ward.ward_name,
   area.area_name,
   feature_type.feature_type_code,
   feature_type.feature_type_name,
   feature.feat_cent_east,
   feature.feat_cent_north,
   feat_measurement.feature_quantity,
   feat_attrib_type.attrib_value_code,
   feat_attrib_type_2.attrib_value_code,
   feat_attrib_type_3.attrib_value_code,
   feat_attrib_type_4.attrib_value_code,
   feat_attrib_type_5.attrib_value_code
FROM
   area,
   central_site,
   feat_attrib_type,
   feat_attrib_type feat_attrib_type_2,
   feat_attrib_type feat_attrib_type_3,
   feat_attrib_type feat_attrib_type_4,
   feat_attrib_type feat_attrib_type_5,
   feat_measurement,
   feature,
   feature_type,
   site,
   ward
WHERE
   central_site.site_code = site.site_code AND
   site.site_code = feature.site_code AND
   ward.ward_code = feature.ward_code AND
   area.area_code = feature.area_code AND
   feature_type.feature_type_code = feature.feature_type_code AND
   feature.site_code = feat_attrib_type.site_code AND
   feature.plot_number = feat_attrib_type.plot_number AND
   (feat_attrib_type.attrib_type_code = 'MNT' OR feat_attrib_type.attrib_type_code IS NULL) AND
   feature.site_code = feat_attrib_type_2.site_code AND
   feature.plot_number = feat_attrib_type_2.plot_number AND
   (feat_attrib_type_2.attrib_type_code = 'NPT' OR feat_attrib_type_2.attrib_type_code IS NULL) AND
   feature.site_code = feat_measurement.site_code AND
   feature.plot_number = feat_measurement.plot_number AND
   (feat_measurement.measurement_code = 'NSNP' OR feat_measurement.measurement_code IS NULL) AND
   feature.site_code = feat_attrib_type_3.site_code AND
   feature.plot_number = feat_attrib_type_3.plot_number AND
   (feat_attrib_type_3.attrib_type_code = 'OM' OR feat_attrib_type_3.attrib_type_code IS NULL) AND
   feature.site_code = feat_attrib_type_4.site_code AND
   feature.plot_number = feat_attrib_type_4.plot_number AND
   (feat_attrib_type_4.attrib_type_code = 'PMNT' OR feat_attrib_type_4.attrib_type_code IS NULL) AND
   feature.site_code = feat_attrib_type_5.site_code AND
   feature.plot_number = feat_attrib_type_5.plot_number AND
   (feat_attrib_type_5.attrib_type_code = 'WMNT' OR feat_attrib_type_5.attrib_type_code IS NULL)
ORDER BY
   central_site.site_code ASC

Best Answer

As Mihai said, use a LEFT JOIN on feat_attrib_type_4 and feat_attrib_type_5. Either old style (as you have) or ANSI joins style would work. For the former add (+) after every reference to a column in the where clause from those two aliases. For the latter, convert the statement to ANSI join syntax and add LEFT before the word JOIN for those two tables.