--Dt
我有两个#temp表,列名相同。我想加入两个表,但是使用内部联接,它会像这样发送结果
我希望结果如何:
联盟的问题在于它没有比较两个结果。
使用的查询:
--Detox
select distinct client.CL_CASENO , p.PR_NAME , a.AD_EFFDT,a.AD_EXPDT, a.CR_CODE
into #d
from MCCSADPF as sud
inner join PCHDOCPF as doc on sud.SAP_DOCID = doc.DC_RCDID
inner join v_clients as client on client.CL_RCDID = doc.DCF_CLTID
inner join v_Providers as p on sud.SAF_APRVID = p.PR_RCDID
left outer join CODCODPF as code on sud.SAF_TDTXTP = code.CO_RCDID
inner join v_Authorizations as a on a.CL_CASENO = client.CL_CASENO
where sud.SAF_APRVID in ('10118','10115','191853' )
and sud.SAF_TDTXTP = '16774' --Detox
and CR_CODE in ('H0010')
and AD_EFFDT >= '2014-10-1' and AD_EXPDT <= GETDATE()
--Ambulatory
select distinct client.CL_CASENO , p.PR_NAME , a.AD_EFFDT , a.AD_EXPDT,a.CR_CODE
into #amb
from MCCSADPF as sud
inner join PCHDOCPF as doc on sud.SAP_DOCID = doc.DC_RCDID
inner join v_clients as client on client.CL_RCDID = doc.DCF_CLTID
inner join v_Providers as p on sud.SAF_APRVID = p.PR_RCDID
left outer join CODCODPF as code on sud.SAF_TDTXTP = code.CO_RCDID
inner join v_Authorizations as a on a.CL_CASENO = client.CL_CASENO
where sud.SAF_APRVID in ('191847','10115','191853')
and sud.SAF_TDTXTP in ('16778') --OutPatient
and CR_CODE in('H0020')
and AD_EFFDT >= '2014-10-1' and AD_EXPDT <= GETDATE()
----------------------------------------------------------------------
--JOIN--
Select * from #d as d
inner join #amb as a on d.CL_CASENO = a.CL_CASENO
where d.AD_EFFDT <= a.AD_EFFDT
答案 0 :(得分:0)
如果您需要的是两个表中都存在的患者,那么您的查询几乎是正确的。您需要的就是抓住第一张桌子。
SELECT d.*
FROM #d d
JOIN #amb a ON d.CL_CASENO = a.CL_CASENO
WHERE d.AD_EFFDT <= a.AD_EFFDT