Skip to content

Commit 6b9eefd

Browse files
authored
Create A_SP_FC_REG.sql
1 parent b01e1b6 commit 6b9eefd

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed

05.test/A_SP_FC_REG.sql

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
2+
/*/*/*
3+
4+
Name: TEST SQL FOR A_SP_FC_REG
5+
Author: PLANSIS
6+
Written: 2023-08-23
7+
Purpose: A linear (ARIMA) regression model automation. Transforms input timeserie (value1 per day) into output timeserie based on time lagged categories, day of week and regression parameters from file_source table
8+
Comments: Timelag is organized in time categories : 0 - today, 1 - yesterday, 2 - two days ago, 3- 3 to 6 days ago, 4 - 7 - 2 weeks ago
9+
Test sql to be wrapped into EXEC of SQL string , the commented parameters are to be passed as string
10+
Affected table(s): [A_FACT_DAY]
11+
Parameter(s): @param1 - description and usage
12+
@param2 - description and usage
13+
Edit History:
14+
15+
*/*/*/
16+
17+
DECLARE @date_min date = '2023-01-01' -- compute date from
18+
DECLARE @date_max date = '2023-12-31' -- compute date from
19+
DECLARE @timelag_max int = 30 -- max of time lag to stretch @date_min
20+
-- file_id=417 and A = 639 -- source file filter ; B is a text filter, can be used instead of numeric A
21+
-- forecast_id=3 and activity_id in (664,688,697) -- input time series parameter
22+
-- value1 input column name with data
23+
-- value1 output column name
24+
-- parameters file with file_id 417; it has more parametersets tf. we need to filter on a right on using A field.
25+
-- C is the lag category
26+
-- d1 = 1 means it is monday; d1=d2=...=d6=0 means sunday
27+
-- L is a exp addition param = 1
28+
/*{
29+
"A": "ACT_ID",
30+
"B": "ACT_NAME",
31+
"C": "Parameterset",
32+
"D": "offset",
33+
"E": "lnc",
34+
"F": "d1",
35+
"G": "d2",
36+
"H": "d3",
37+
"I": "d4",
38+
"J": "d5",
39+
"K": "d6",
40+
"L": "formula"
41+
}*/
42+
43+
;
44+
with D as
45+
(select date, sum(value1) value1 from [dbo].[A_FACT_DAY]
46+
where forecast_id=3 and activity_id in (664,688,697) -- input time series parameter
47+
and date between dateadd(day,-@timelag_max,@date_min) and @date_max
48+
group by date
49+
)
50+
, T as (
51+
select date
52+
, case when day_week=1 then 1 else 0 end d1
53+
, case when day_week=2 then 1 else 0 end d2
54+
, case when day_week=3 then 1 else 0 end d3
55+
, case when day_week=4 then 1 else 0 end d4
56+
, case when day_week=5 then 1 else 0 end d5
57+
, case when day_week=6 then 1 else 0 end d6
58+
from [dbo].[A_TIME_DATE]
59+
where date between @date_min and @date_max
60+
),
61+
62+
D_CAT as (
63+
select date
64+
, 0 as cat
65+
, isnull(value1,0) val
66+
from D
67+
union all
68+
select date
69+
, 1 as cat
70+
, sum(isnull(value1,0)) OVER(ORDER BY D.Date
71+
ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING ) as val
72+
from D
73+
union all
74+
select date
75+
, 2 as cat
76+
, sum(isnull(value1,0)) OVER(ORDER BY D.Date
77+
ROWS BETWEEN 2 PRECEDING AND 2 PRECEDING ) as val
78+
from D
79+
union all
80+
select date
81+
, 3 as cat
82+
, sum(isnull(value1,0)) OVER(ORDER BY D.Date
83+
ROWS BETWEEN 6 PRECEDING AND 3 PRECEDING ) as val
84+
from D
85+
union all
86+
select date
87+
, 4 as cat
88+
, sum(isnull(value1,0)) OVER(ORDER BY D.Date
89+
ROWS BETWEEN 13 PRECEDING AND 7 PRECEDING ) as val
90+
from D
91+
)
92+
93+
, P as (
94+
select try_convert (smallint,right(C,1)) cat
95+
, try_convert(int,A) activity_id
96+
, try_convert(real,D) offset
97+
, try_convert(real,E) inc
98+
, try_convert(real,F) d1
99+
, try_convert(real,G) d2
100+
, try_convert(real,H) d3
101+
, try_convert(real,I) d4
102+
, try_convert(real,J) d5
103+
, try_convert(real,K) d6
104+
, try_convert(real,L) f
105+
from [dbo].[A_SOURCE_FILE]
106+
where file_id=417 and A = 639 -- source file filter
107+
)
108+
, D_LOG as (
109+
select date,D_CAT.cat,LOG(val+P.f) val_log, val
110+
from D_CAT inner join P on D_CAT.cat=P.cat
111+
)
112+
113+
select T.date
114+
, sum(EXP(P.offset + D_LOG.val_log*P.inc + T.d1*P.d1+T.d2*P.d2+ T.d3*P.d3+T.d4*P.d4+T.d5*P.d5+T.d6*P.d6)-P.f) value1
115+
from D_LOG inner join T on D_LOG.date=T.date
116+
inner join P on D_LOG.cat=P.cat
117+
group by T.date
118+
119+
120+
121+
--select * from D_CAT where date='2023-08-01';
122+
-- select * from [dbo].[A_SOURCE_FILE] where file_id=417
123+

0 commit comments

Comments
 (0)