select
SD.ID,
SD.DocumentType,
DQ.Code || '-' || CAST(SD.OrdNumber AS VARCHAR(10))|| '/' || P.Code as DisplayName,
( select count(*)
from Relations R
join GeneralLedger GL on GL.ID = R.RightSide_ID
where R.Rel_Def in ({REL_DEF})
and R.LeftSide_ID = A.ID
and GL.Audited = 'A'
) as AuditedCount,
( select count(*)
from Relations R
join GeneralLedger GL on GL.ID = R.RightSide_ID
where R.Rel_Def in ({REL_DEF})
and R.LeftSide_ID = A.ID
) as AccountedCount
from
( select distinct A.ID
from StoreDocuments A
join StoreDocuments2 B on A.ID = B.Parent_ID
where B.RowType = 3
and B.ClosingOrder >= :ClosingOrder
and B.Store_ID = :Store_ID
) A
join StoreDocuments SD on SD.ID = A.ID
join DocQueues DQ on DQ.ID = SD.DocQueue_ID
join Periods P on P.ID = SD.Period_ID
order by P.Code, DQ.Code, SD.OrdNumberGenerated by ABRA Software a.s. 27.10.2021 16:34:11