为什么 SQL 查询只返回 'WHERE' 'IN' 子句中单个项目的记录?

Why is SQL query only returning records for a single item in 'WHERE' 'IN' clause?

提问人:mountainscaler 提问时间:11/15/2023 最后编辑:mountainscaler 更新时间:11/15/2023 访问量:25

问:

为什么此查询:

SELECT
        dr.facility_id,
        ds.sys_sample_code,
        ds.sys_loc_code,
        ds.matrix_code,
        ds.sample_type_code,
        ds.start_depth,
        ds.end_depth,
        ds.sample_date,
        ds.task_code,
        dt.prep_date,
        dt.analysis_date,
        dt.lab_name_code,
        dt.lab_sdg,
        dt.lab_sample_id,
        dt.analytic_method,
        dt.prep_method,
        dr.cas_rn,
        dr.custom_field_1 as chemical_name, 
        dr.detect_flag,
        dr.result_numeric as result_value_rdl,
        dr.result_numeric as result_value_mdl,
        dr.result_numeric as result_text_rdl,
        dr.result_numeric as result_text_mdl,
        dr.validator_qualifiers as final_qualifier,
        dr.lab_qualifiers,
        dr.result_unit,
        dr.method_detection_limit,
        dr.quantitation_limit,
        dr.reporting_detection_limit,
        dr.detection_limit_unit,
        dr.reportable_result,
        dr.result_type_code,
        dt.percent_moisture,
        dt.fraction, dt.basis,
        dt.dilution_factor,
        dr.custom_field_2 as validation_date,
        dr.dqm_remark as validation_comment,
        dr.custom_field_3 as validator_name,
        dr.validated_yn, 
        NULL as result_comment,
        dr.approval_code,
        dc.x_coord, dc.y_coord,
        'Groundwater' AS matrix_name
    FROM [dbo].dt_sample ds
    INNER JOIN [dbo].dt_test dt ON dt.sample_id = ds.sample_id
    INNER JOIN [dbo].dt_result dr ON dr.test_id = dt.test_id
    LEFT JOIN [dbo].dt_coordinate dc ON ds.sys_loc_code = dc.sys_loc_code
    WHERE dt.lab_sdg IN ('X3A0217', 'X3B0341', 'X3C0002', 'X3D0121', 'X3E0289', 'X3E0336')
    AND dt.facility_id = 928061
) subq;

在以下情况下仅返回“X3B0341”的记录:

SELECT dt.lab_sdg, COUNT(*)
FROM [dbo].dt_test dt
WHERE dt.lab_sdg IN ('X3A0217', 'X3B0341', 'X3C0002', 'X3D0121', 'X3E0289', 'X3E0336')
--AND dt.facility_id = 928061
GROUP BY dt.lab_sdg;

给出此输出(显然数据可用性不是问题):

X3B0341 408
X3C0002 239
X3D0121 438
X3E0289 673
X3E0336 303

我已经厌倦了更改以选择更少的值,但无济于事。我可以使用一个巨大的脚本获得所需的结果,每个脚本都有乘数并集,但我想避免这种情况。非常感谢任何和所有的帮助。WHEREWHERE dt.lab_sdg = 'X3D0121' OR dt.lab_sdg = 'X3E0336'dt.lab_sdg

sql 联接 where-clause in-clause

评论

0赞 Adrian Maxwell 11/15/2023
) subq;这是否表示您还有更多未提供的查询,您是否独立运行此查询?
0赞 Craig 11/15/2023
为什么您的第二个示例查询将“dt.facility_id = 928016”注释掉了......这是否意味着只有“X3B0341”的记录具有该设施 ID,而其他记录具有不同的设施 ID?
0赞 mountainscaler 11/16/2023
@AdrianMaxwell号我相信这是一件神器。
0赞 mountainscaler 11/16/2023
@Craig因为不需要facility_id因为所有lab_sdg都与facility_id 928016相关联。

答:

1赞 Asif Ahmed Sourav 11/15/2023 #1

我认为您还应该检查那些 INNER JOIN 逻辑。对于数据“X3B0341”,这些表中可能没有数据。 我会逐个注释掉 INNER JOIN 中使用的表格,以找出答案。

SELECT *
    FROM [dbo].dt_sample ds
    INNER JOIN [dbo].dt_test dt ON dt.sample_id = ds.sample_id
    --INNER JOIN [dbo].dt_result dr ON dr.test_id = dt.test_id
    --LEFT JOIN [dbo].dt_coordinate dc ON ds.sys_loc_code = dc.sys_loc_code
    WHERE dt.lab_sdg IN ('X3A0217', 'X3B0341', 'X3C0002', 'X3D0121', 'X3E0289', 'X3E0336')
    AND dt.facility_id = 928061;

还要检查没有 where 子句,您是否获得“X3B0341”以外的数据。

SELECT *
    FROM [dbo].dt_sample ds
    INNER JOIN [dbo].dt_test dt ON dt.sample_id = ds.sample_id
    INNER JOIN [dbo].dt_result dr ON dr.test_id = dt.test_id
    LEFT JOIN [dbo].dt_coordinate dc ON ds.sys_loc_code = dc.sys_loc_code
    WHERE 
    --dt.lab_sdg IN ('X3A0217', 'X3B0341', 'X3C0002', 'X3D0121', 'X3E0289', 'X3E0336')
    dt.facility_id = 928061;
0赞 Adrian Maxwell 11/15/2023 #2

更改表格的顺序,然后向左加入其余表格。其他表不得具有与所需列表匹配的相关值:例如:

SELECT
      dt.lab_sdg
    , dr.facility_id
    , ds.sys_sample_code
    , ds.sys_loc_code
    -- more as needed
FROM  [dbo].dt_test dt
LEFT JOIN [dbo].dt_sample ds ON dt.sample_id = ds.sample_id
LEFT JOIN [dbo].dt_result dr ON dr.test_id = dt.test_id
LEFT JOIN [dbo].dt_coordinate dc ON ds.sys_loc_code = dc.sys_loc_code
WHERE dt.lab_sdg IN ('X3A0217', 'X3B0341', 'X3C0002', 'X3D0121', 'X3E0289', 'X3E0336')
    AND dt.facility_id = 928061