set dateformat dmy declare date_start_period varchar 32 01 01 2018 dat

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
set dateformat dmy
declare @date_start_period varchar(32) = '01.01.2018',
@date_end_period varchar(32) = '31.12.2018'
-- КАНЦЕЛЯРИЯ (rkk.PriznDok = 'И')
select distinct mr.NameAn, jr.NameAn, sum(1)
from MBAnalit rkk
inner join MBAnalit jr on jr.Analit = rkk.GruppaDel
inner join MBAnalit mr on mr.Analit = rkk.MestoReg and mr.NameAn = 'Канцелярия'
inner join SBLinks lnk on lnk.SourceType = 'R' and lnk.DestType = 'E' and lnk.SourceID = rkk.Analit
where rkk.Vid = 3174 and rkk.PriznDok = 'И' and
rkk.DatOpen >= @date_start_period and
rkk.DatOpen <= @date_end_period and exists (select 1 from SBEDocProtocol dp where dp.EDocID = lnk.DestID and dp.ActionType in ('C','E','R','G'))
group by mr.NameAn, jr.NameAn
union all
-- ВСЕ ВНУТРЕННИЕ ЖУРНАЛЫ ЦЕХОВ
select distinct mr.NameAn, jr.NameAn, sum(1)
from MBAnalit rkk
inner join MBAnalit jr on jr.Analit = rkk.GruppaDel
inner join MBAnalit mr on mr.Analit = rkk.MestoReg and mr.NameAn <> 'Канцелярия'
inner join SBLinks lnk on lnk.SourceType = 'R' and lnk.DestType = 'E' and lnk.SourceID = rkk.Analit
where rkk.Vid = 3174 and rkk.PriznDok = 'У' and
rkk.DatOpen >= @date_start_period and
rkk.DatOpen <= @date_end_period and exists (select 1 from SBEDocProtocol dp where dp.EDocID = lnk.DestID and dp.ActionType in ('C','E','R','G'))
group by mr.NameAn, jr.NameAn
order by mr.NameAn, jr.NameAn