我有这个查询
SELECT *
FROM (SELECT mi.visit_id, mi.event_id, mi.patient_id, mi.mrn, mi.reg_date,
mi.d_date, mi.bml_count, mi.TYPE, mblp.baby_patient_id,
mblp.baby_birthdate
FROM ajmid.km0076_motherinfo_test mi LEFT JOIN alfayezb2.mbl_patients mblp
ON mblp.mother_patient_id = mi.patient_id
--works here
AND ( TO_CHAR (mblp.baby_birthdate, 'mm/dd/YYYY') =
TO_CHAR (mi.reg_date, 'mm/dd/YYYY')
OR TO_CHAR (mblp.baby_birthdate, 'mm/dd/YYYY') =
TO_CHAR (mi.reg_date - 1, 'mm/dd/YYYY')
OR TO_CHAR (mblp.baby_birthdate, 'mm/dd/YYYY') =
TO_CHAR (mi.reg_date + 1, 'mm/dd/YYYY')
)
) bml
LEFT OUTER JOIN --doesn't work here
(SELECT ROW_NUMBER () OVER (PARTITION BY vis.patient_id ORDER BY vis.admission_date_time)
num,
vis.admission_date_time, vis.visit_id, vis.patient_id,
vis.facility_id
FROM visit vis) v ON bml.baby_patient_id = v.patient_id
WHERE v.num = 1
ORDER BY bml.reg_date
bml本身返回118行,而整个查询返回117,原因是bml返回1行,baby_patient_id为null,所以我使用左外连接来显示它,但它仍然没有显示!! < / p>
我该怎么做才能显示bml的所有行?
我使用Toad 9.6
谢谢
答案 0 :(得分:0)
可能的原因是你的filter / criteria(where子句)正在为v.num消除一个空值。 WHERE在加入后过滤结果。
WHERE v.num = 1 -- Are all v.num equal to 1 ?
通过定义NULL意味着对字段使用条件的单纯操作会消除该行,因为无法计算NULL。你可以说“WHERE id!= 1”并期望获得id为null的行,因为null!= 1对吗?错误。 id!= NULL没有逻辑定义。这就是我们在处理NULL时说“IS或IS not NULL”的原因。
答案 1 :(得分:0)
检查查询:
SELECT ROW_NUMBER () OVER (PARTITION BY vis.patient_id ORDER BY vis.admission_date_time)
num,
vis.admission_date_time, vis.visit_id, vis.patient_id,
vis.facility_id
FROM visit vis
它返回118而不是null patient_id&#39;
如果它返回117,那可能就是原因。(LEFT OUTER JOIN
不会选择两个表都为空的记录)
另外,您确定baby_patient_id
表中bml
的空值实际上是NULL
值而不是空字符吗?(&#39;&#39;)。
答案 2 :(得分:0)
OR bml.baby_patient_id IS NULL
到where子句,所以最后的脚本是
SELECT *
FROM (SELECT mi.visit_id, mi.event_id, mi.patient_id, mi.mrn, mi.reg_date,
mi.d_date, mi.bml_count, mi.TYPE, mblp.baby_patient_id,
mblp.baby_birthdate
FROM ajmid.km0076_motherinfo_test mi LEFT JOIN alfayezb2.mbl_patients mblp
ON mblp.mother_patient_id = mi.patient_id
AND ( TO_CHAR (mblp.baby_birthdate, 'mm/dd/YYYY') =
TO_CHAR (mi.reg_date, 'mm/dd/YYYY')
OR TO_CHAR (mblp.baby_birthdate, 'mm/dd/YYYY') =
TO_CHAR (mi.reg_date - 1, 'mm/dd/YYYY')
OR TO_CHAR (mblp.baby_birthdate, 'mm/dd/YYYY') =
TO_CHAR (mi.reg_date + 1, 'mm/dd/YYYY')
)
) bml
LEFT OUTER JOIN
(SELECT ROW_NUMBER () OVER (PARTITION BY vis.patient_id ORDER BY vis.admission_date_time)
num,
vis.admission_date_time, vis.visit_id, vis.patient_id,
vis.facility_id
FROM visit vis) v ON bml.baby_patient_id = v.patient_id
WHERE v.num = 1
OR bml.baby_patient_id IS NULL
ORDER BY bml.reg_date
我不知道这有多大帮助,我希望有人能帮我解释一下! 谢谢大家