-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path28-Неокуп ст-ть с %.sql
66 lines (58 loc) · 2.24 KB
/
28-Неокуп ст-ть с %.sql
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use VBM;
go
/* здесь будут лежать данные дл¤ VIEW */
IF OBJECT_ID('dbo.tmp_UNPAY_COST_WITH','U') IS NOT NULL
DROP TABLE tmp_UNPAY_COST_WITH;
CREATE TABLE tmp_UNPAY_COST_WITH (
ID_PORTFOLIO nvarchar(32) NOT NULL,
PERIOD date NOT NULL,
[SUM] decimal(18,5) NOT NULL)
/* сперва считаем в мес¤цах покупки, добавл¤ем их в tmp_UNPAY_COST_WITH */
insert into tmp_UNPAY_COST_WITH (ID_PORTFOLIO,PERIOD,[SUM])
select
CFP.ID_PORTFOLIO,
CFP.PERIOD,
(
(select P.PURCHASE_PRICE from PORTFOLIOS P where P.ID_PORTFOLIO = CFP.ID_PORTFOLIO) * (-1) - CTF.[SUM] + CFP.[SUM]
) as [SUM]
from GetCOLLECTIONS_FACTandPRED CFP left join GetCOSTS_TOTAL_FACT CTF
on CFP.ID_PORTFOLIO = CTF.ID_PORTFOLIO and CFP.PERIOD = CTF.PERIOD
where CFP.PERIOD = (select I.PURCHASE_DATE from PORTFOLIOS I where I.ID_PORTFOLIO = CFP.ID_PORTFOLIO)
/* теперь считаем все мес¤ца, пока не закончен жизненный цикл портфел¤*/
declare @monthCur date
declare @monthStart date
declare @monthEnd date
declare @sql1 nvarchar(1024)
declare @portfolio nvarchar(32)
declare @nn int
DECLARE CURSOR_P CURSOR FOR
select ID_PORTFOLIO from PORTFOLIOS
FOR READ ONLY
OPEN CURSOR_P
FETCH NEXT FROM CURSOR_P INTO @portfolio
--FETCH CURSOR_P into @portfolio
WHILE @@FETCH_STATUS = 0
BEGIN
--set @portfolio = N'¬ЁЅ'
set @monthStart = (select PURCHASE_DATE from PORTFOLIOS where ID_PORTFOLIO = @portfolio)
set @monthEnd = (select ENDLIFE_DATE from PORTFOLIOS where ID_PORTFOLIO = @portfolio)
set @monthCur = @monthStart
set @nn = 1
while @monthCur < @monthEnd
begin
set @monthCur = dateadd(month,@nn,@monthStart)
insert into tmp_UNPAY_COST_WITH(ID_PORTFOLIO,PERIOD,[SUM])
(
select
@portfolio,
@monthCur,
(select T.[SUM] from tmp_UNPAY_COST_WITH T where T.ID_PORTFOLIO = @portfolio and T.PERIOD = dateadd(month,-1,@monthCur)) +
(select POD.[SUM] from GetPROFIT_OPER_DIA POD where POD.ID_PORTFOLIO = @portfolio and POD.PERIOD = @monthCur) -
isnull((select CPFF.[SUM] from GetCOSTS_PCT_FACT_FULL CPFF where CPFF.ID_PORTFOLIO = @portfolio and CPFF.PERIOD = @monthCur),0)
)
set @nn = @nn + 1
end
FETCH NEXT FROM CURSOR_P INTO @portfolio
END
CLOSE CURSOR_P
DEALLOCATE CURSOR_P