select
case
when II.ID is not null then II.ID
when CR.ID is not null then CR.ID
when ICN.ID is not null then ICN.ID
when RCR.ID is not null then RCR.ID
else null
end as Document_ID,
case
when II.ID is not null then DQ_II.DocumentType
when CR.ID is not null then DQ_CR.DocumentType
when ICN.ID is not null then DQ_ICN.DocumentType
when RCR.ID is not null then DQ_RCR.DocumentType
else null
end as DocumentType
from
StoreDocuments2 SDocs2
join StoreDocuments SDocs on SDocs2.Parent_ID = SDocs.ID
/*FV*/
left join IssuedInvoices2 II2 on (II2.ProvideRow_ID = SDocs2.ID)
left join IssuedInvoices II on II2.Parent_ID = II.ID
left join DocQueues DQ_II on II.DocQueue_ID = DQ_II.ID
/*PP*/
left join CashReceived2 CR2 on (CR2.ProvideRow_ID = SDocs2.ID)
left join CashReceived CR on CR2.Parent_ID = CR.ID
left join DocQueues DQ_CR on CR.DocQueue_ID = DQ_CR.ID
/*DV*/
left join IssuedCreditNotes2 ICN2 on (ICN2.ProvideRow_ID = SDocs2.ID)
left join IssuedCreditNotes ICN on ICN2.Parent_ID = ICN.ID
left join DocQueues DQ_ICN on ICN.DocQueue_ID = DQ_ICN.ID
/*VP*/
left join RefundedCashReceived2 RCR2 on (RCR2.ProvideRow_ID = SDocs2.ID)
left join RefundedCashReceived RCR on RCR2.Parent_ID = RCR.ID
left join DocQueues DQ_RCR on RCR.DocQueue_ID = DQ_RCR.ID
where
SDocs2.StoreCard_ID = :StoreCard_ID and
SDocs.DocumentType = '21' and /* DL */
SDocs.Firm_ID = :Firm_IDGenerated by ABRA Software a.s. 27.10.2021 16:34:11