我有三张桌子:
clinic = (id,name,short_name,region,country,continent,area)
result_month =(id,year,result_quarter_id,month)
test_value_in_range_count =(clinic_id,result_month_id,patient_sub_set_id,test_value_range_id,number_of_values)
示例数据:
诊所
id region country continent area
3299 Kazakhstan Kazakhstan Eurasia Middle East/Asia
result_month
id year result_quarter_id month
200801 2008 2008Q1 1
test_value_in_range_count
诊所表中没有诊所ID 3299的数据。但JOINS必须返回
我需要使用 test_value_in_range_count 中的空值包含 result_month 表中的所有行。问题是WHERE
子句。这会停止生成行,因为显然从 result_month 到 test_value_range_id 的某些行不存在数据。
预期结果
clinic region country continent area ym vf
3299 Kazakhstan Kazakhstan Eurasia Middle East/Asia 201511 null
我已经尝试过各种各样的查询,但是没有运气。任何帮助或指示都会非常感激。
SELECT
apc.id AS clinic,
apc.region,
apc.country,
apc.continent,
apc.area,
vrm.id AS ym,
SUM(CASE test_value_range_id WHEN '1124_1' THEN number_of_values ELSE 0 END) AS avf
FROM result_month vrm
LEFT JOIN test_value_in_range_count vt on vrm.id = vt.result_month_id
LEFT OUTER JOIN clinic apc on vt.clinic_id = apc.id
WHERE (vt.test_value_range_id IN ('1124_1', '1124_2', '1124_3', '1124_4', '1124_5')) AND (vt.patient_sub_set_id = 'ALL')
GROUP BY apc.id,
apc.region,
apc.country,
apc.continent,
apc.area,
vrm.id
;
答案 0 :(得分:4)
从where子句中删除条件并将其添加到join:SELECT apc.id AS诊所, apc.region, apc.country, apc.continent, apc.area, vrm.id AS ym,
before='1\r\n2\r\n'
before='3\r\n4\r\n'
before='5\r\n6\r\n'
before='7\r\n8\r\n'
before='9\r\n10\r\n'
否则where子句从左连接中进行连接