1
|
use kv2021
|
2
|
|
3
|
create table #adr (kod_adres bigint,
|
4
|
p varchar(500))
|
5
|
--drop table #adr
|
6
|
|
7
|
declare @code tinyint, @kod_adres bigint, @p varchar(500)
|
8
|
declare adr cursor forward_only
|
9
|
for
|
10
|
|
11
|
select kod_adres
|
12
|
from spr..contact
|
13
|
where stel like '(9%'
|
14
|
and kod_adres in (
|
15
|
select kod_adres
|
16
|
from nach_t
|
17
|
where kod_postav in (select code from spr..postav where name like '%???%' or name like '%?????%????%')
|
18
|
group by kod_adres
|
19
|
having sum(summa)>0)
|
20
|
group by kod_adres
|
21
|
|
22
|
open adr
|
23
|
fetch next from adr into @kod_adres--, @p
|
24
|
while @@FETCH_STATUS=0
|
25
|
begin
|
26
|
|
27
|
insert into #adr
|
28
|
select top 1 @kod_adres, stel
|
29
|
from spr..contact
|
30
|
where kod_adres=@kod_adres and stel like '(9%'
|
31
|
group by stel
|
32
|
|
33
|
|
34
|
|
35
|
fetch next from adr into @kod_adres--, @p
|
36
|
end
|
37
|
close adr
|
38
|
deallocate adr
|
39
|
|
40
|
declare @contacts table (region varchar(100), code bigint,phone varchar(20),phone_kassa varchar(10))
|
41
|
|
42
|
Insert @contacts
|
43
|
select v.region, a.kod_adres, '+7'+Replace(Replace(Replace(p,'(',''),')',''),'-','') as p, Right(replace(tel,'-',''),5) as kassa
|
44
|
from #adr a inner join
|
45
|
spr..vadres v on v.code=a.kod_adres inner join
|
46
|
spr..org o on v.code_region=o.region
|
47
|
where kod_adres in (
|
48
|
select kod_adres
|
49
|
from nach_t
|
50
|
where kod_postav in (select code from spr..postav where name like '%???%' or name like '%?????%????%')
|
51
|
group by kod_adres
|
52
|
having sum(summa)>0)
|
53
|
and v.region not like '%?. ??????%'
|
54
|
group by v.region, a.kod_adres, p, tel
|
55
|
order by v.region
|
56
|
|
57
|
|
58
|
drop table #adr
|
59
|
|
60
|
|
61
|
|
62
|
-- ??????? ?????? ?? ????????, ? ??????? ???? ?????????
|
63
|
declare @mes tinyint
|
64
|
|
65
|
select @mes=mes
|
66
|
from spr..year
|
67
|
where tek=1
|
68
|
|
69
|
select a.region ?????, round(sum(saldo), 2) ??????, round(sum(kt), 2) ??, round(sum(dt), 2) ??, round(sum(tek), 2) ???????, round(sum(pos), 2) ????????????,
|
70
|
cnts.phone, cnts.phone_kassa
|
71
|
from (select kod_adres, saldo
|
72
|
, case when saldo < 0 then saldo * -1 else 0 end as kt
|
73
|
, case when saldo > 0 then saldo else 0 end as dt
|
74
|
, case when saldo > 0 then case when nach < saldo then nach else saldo end else 0 end as tek
|
75
|
, case when saldo > 0 then saldo else 0 end - case when saldo > 0 then case when nach < saldo then nach else saldo end else 0 end as pos
|
76
|
from (select kod_adres, sum(case when vid = 0 then summa else 0 end) as saldo, sum(case when vid = 1 then summa else 0 end) as nach
|
77
|
from (select kod_adres, cast(sum(summa * 0.01) as float) as summa, 0 as vid
|
78
|
from kv2021..saldo_t
|
79
|
where kod_postav in (select code from spr..postav where name like '%???%???%')
|
80
|
group by kod_adres
|
81
|
union all
|
82
|
select kod_adres, cast(sum(summa * 0.01) as float) as summa, 1 as vid
|
83
|
from kv2021..nach_a
|
84
|
where kod_postav in (select code from spr..postav where name like '%???%???%') and mes in (@mes - 2, @mes - 1)
|
85
|
group by kod_adres) a
|
86
|
group by kod_adres) a) u
|
87
|
inner join spr..vAdres a on a.code = u.kod_adres
|
88
|
Inner Join @contacts cnts On cnts.code=a.code
|
89
|
Where pos>0
|
90
|
group by a.region, cnts.phone, cnts.phone_kassa
|
91
|
order by a.region
|
92
|
|
93
|
|