Я работаю над запросом, в котором у меня есть 2 левых соединения, но у меня только 1 одно подмножество, если в другом есть данные. Это включает либо основанные на записях.
Я ищу, чтобы получить результаты 'b' или набор результатов 'исключений', но не оба. Так оно и есть в настоящее время, я получу либо или.
SELECT
top 500
excepts.msgs_id as a_id,
b.msgs_id as b_id,
excepts.hipaa837insvcline_id as a_svc,
b.hipaa837insvcline_id as b_svc,
msgs.hipaa837insvcline_id as msgs_svc
FROM hipaa837inmast
INNER JOIN Hipaa837INSubmitter ON Hipaa837INSubmitter.batch_Number = hipaa837inmast.Batch_Number
LEFT JOIN Provider ON Provider.prov_id = Hipaa837INSubmitter.Prov_ID
INNER JOIN Hipaa837INSubscriber ON Hipaa837INSubscriber.Batch_Number = Hipaa837INSubmitter.Batch_Number
INNER JOIN hipaa837inbillingpayTo ON hipaa837inbillingpayTo.hipaa837inbillingpayTo_Id = Hipaa837INSubscriber.hipaa837inbillingpayTo_Id
INNER JOIN Hipaa837INPatientClaim clm ON clm.Hipaa837INSubscriber_ID = Hipaa837INSubscriber.Hipaa837INSubscriber_ID
INNER JOIN Hipaa837INSVCLine svc ON svc.Hipaa837INPatientClaim_ID = clm.Hipaa837INPatientClaim_ID
LEFT JOIN patient ON patient.Patient_ID = clm.Patient_ID
LEFT JOIN (select DISTINCT ecpexcept.msgs_id,ecpexcept.hipaa837insvcline_id
from incedo.ecpexcept
INNER JOIN [incedo].[ecpreason] WITH (NOLOCK) on ecpreason.msgs_id = ecpexcept.msgs_id
INNER JOIN [incedo].[hipaa837insvcline] t WITH (NOLOCK) on ecpexcept.hipaa837insvcline_id = t.hipaa837insvcline_id
WHERE ecpexcept.msgs_id in (13)
) msgs ON msgs.hipaa837insvcline_id = svc.hipaa837insvcline_id
--attempt to only show 0 hierarchy if no other except
LEFT JOIN (select DISTINCT ecpexcept.msgs_id,ecpexcept.hipaa837insvcline_id
from incedo.ecpexcept
INNER JOIN (SELECT TOP 1 ecpreason.msgs_id,hierarchy,ecpreason.reason
FROM [incedo].[ecpreason] WITH (NOLOCK)
INNER JOIN [incedo].[ecpexcept] WITH (NOLOCK) on ecpreason.msgs_id = ecpexcept.msgs_id
INNER JOIN [incedo].[hipaa837insvcline] t WITH (NOLOCK) on ecpexcept.hipaa837insvcline_id = t.hipaa837insvcline_id
WHERE hierarchy > 0
ORDER BY hierarchy
) a on a.Msgs_ID in (13) AND a.msgs_id = ecpexcept.msgs_id
) excepts ON excepts.hipaa837insvcline_id = svc.hipaa837insvcline_id AND msgs.hipaa837insvcline_id IS NOT NULL
LEFT JOIN (SELECT DISTINCT ecpreason.msgs_id, T.hipaa837insvcline_id
FROM [incedo].[ecpreason] WITH (NOLOCK)
LEFT JOIN [incedo].[ecpexcept] zero WITH (NOLOCK) on ecpreason.msgs_id = zero.msgs_id AND hierarchy = 0
LEFT JOIN [incedo].[ecpexcept] great WITH (NOLOCK) on ecpreason.msgs_id = great.msgs_id AND great.msgs_id IN (13)
INNER JOIN [incedo].[hipaa837insvcline] t WITH (NOLOCK) on zero.hipaa837insvcline_id = t.hipaa837insvcline_id
WHERE zero.msgs_id IS NOT NULL AND great.msgs_id IS NULL
) b on svc.hipaa837insvcline_id = b.hipaa837insvcline_id AND excepts.hipaa837insvcline_id IS NULL
WHERE ((msgs.hipaa837insvcline_id IS NULL AND b.hipaa837insvcline_id IS NOT NULL AND excepts.hipaa837insvcline_id IS NULL) OR --hierarchy=0
(msgs.hipaa837insvcline_id IS NOT NULL AND b.hipaa837insvcline_id IS NULL AND excepts.hipaa837insvcline_id IS NOT NULL)) AND --hierarchy>0