我在tblVisits中有一个名为'tobacco'的属性。 tblICD是医疗代码列表,icd_jxn是联结表。 tblICD.Descrip是医疗代码(ICD_ID)的文本描述。我试图看看是否有人NOT
在其医疗代码列表中包含字符串'烟草',其bit
'烟草'属性设置为1。下面的查询是我最好的猜测,但它不正确。
SELECT tblVisits.kha_id, tblVisits.tobacco from tblvisits
WHERE NOT EXISTS (SELECT icd_jxn.kha_id, icd_jxn.icd_fk, tblICD.icd_id, tblICD.Descrip
FROM icd_jxn inner join
tblICD on icd_jxn.icd_fk = tblicd.icd_id
WHERE (tblicd.descrip like N'%tobacco%') AND (tblVisits.kha_id = icd_jxn.kha_id) and (tblvisits.tobacco=1))
答案 0 :(得分:1)
案件无法保证
select v.kha_id, v.tobacco
from tblvisits v
where v.tobacco = 1
and not exists (select null from icd_jxn j
inner join tblICD i on j.icd_ICD_FK= i.IDC_ID
where j.KHA_ID = v.KHA_ID
and i.Descrip like N'%tobacco%');
答案 1 :(得分:1)
如果您将此查询视为总结患者,则此查询似乎更简单。患者是否设置了烟草属性?该专利中是否含有烟草?
以下查询使用此逻辑:
select *
from (select tv.kha_id, tv.tobacco,
MAX(case when charindex(N'tobacco', ticd.descrip) > 1 then 1 else 0 end) as tobacco_description,
from tblvisits tv join
icd_jxn jxn
on tv.kha_id = jxn.kha_id join
tblICD ticd
on jxn.icd_fk = ticd.icd_fx
group by tv.kha_id, tv.tobacco
) t
where tobacco_description = 1 and tobacco_description = 0
子查询总结了患者层面的数据。您可以通过添加适当的“having”子句来消除它。但是,我发现子查询更具可读性,它应具有相同的性能。此外,它可以计算烟草,钻头和烟草的所有不同组合的描述。
最后,我用“charindex”替换“like”,因为后者效率更高。