-
Notifications
You must be signed in to change notification settings - Fork 0
/
usp_ci_upload_brandforecast.sql
572 lines (424 loc) · 11.6 KB
/
usp_ci_upload_brandforecast.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
DROP PROCEDURE `usp_ci_upload_brandforecast`
GO
CREATE PROCEDURE `usp_ci_upload_brandforecast`(
strPathFileName varchar(500),
ieyear int,
iePeriod int,
str_cTreeName varchar(300)
)
BEGIN
declare recordcount integer;
declare uniquerecordcount integer;
declare recordcount2 integer;
declare uniquerecordcount2 integer;
if iePeriod = 1
then
set iePeriod = 12 ;
set ieyear = ieyear - 1;
else
set iePeriod = iePeriod - 1;
end if;
create temporary table temp
(
rowid int AUTO_INCREMENT NOT NULL,
BrandName varchar(100) default '',
Forecasted_Value1 varchar(100) default '',
Forecasted_Value2 varchar(100) default '',
Forecasted_Value3 varchar(100) default '',
Forecasted_Value4 varchar(100) default '',
Forecasted_Value5 varchar(100) default '',
Forecasted_Value6 varchar(100) default '',
Forecasted_Value7 varchar(100) default '',
Forecasted_Value8 varchar(100) default '',
Forecasted_Value9 varchar(100) default '',
Forecasted_Value10 varchar(100) default '',
Forecasted_Value11 varchar(100) default '',
Forecasted_Value12 varchar(100) default '',
EYEAR varchar(100) default '',
f_Period int,
strTreeName varchar(300),
PRIMARY KEY (`rowid`)
);
insert into temp (
BrandName,
Forecasted_Value1 ,
Forecasted_Value2 ,
Forecasted_Value3 ,
Forecasted_Value4 ,
Forecasted_Value5 ,
Forecasted_Value6 ,
Forecasted_Value7 ,
Forecasted_Value8 ,
Forecasted_Value9 ,
Forecasted_Value10 ,
Forecasted_Value11 ,
Forecasted_Value12 ,
eyear,
f_Period,
strTreeName
)
select
BrandName,
Forecasted_Value1 ,
Forecasted_Value2 ,
Forecasted_Value3 ,
Forecasted_Value4 ,
Forecasted_Value5 ,
Forecasted_Value6 ,
Forecasted_Value7 ,
Forecasted_Value8 ,
Forecasted_Value9 ,
Forecasted_Value10 ,
Forecasted_Value11 ,
Forecasted_Value12 ,
ieyear,
iePeriod,
str_cTreeName
from tbl_brandforecast_temp;
delete
from temp
where BrandName = 'LOCAL STRUCTURE';
create temporary table temp_val
(
ctreeid varchar(100) default '',
ctreecode varchar(100) default '',
ctreeperiodid varchar(100) default '',
typeid varchar(100) default '',
istopmost varchar(100) default '',
prevnodeid varchar(100) default '',
label varchar(100) default '',
brandname varchar(100) default '');
insert into temp_val
select
e.ctreeid,
e.ctreecode,
b.ctreeperiodid ,
b.typeid,
b.istopmost,
d.prevnodeid,
b.label,
a.brandname
from temp a
inner join ctreenodes b
on a.brandname = b.label
inner join ctreeperiods c
on b.ctreeperiodid = c.ctreeperiodid
inner join ctreedefn d
on b.ctreenodeid = d.ctreenodeid
and c.ctreeperiodid = d.ctreeperiodid
inner join ctrees e
on c.ctreeid = e.ctreeid
and e.ctreecode = str_cTreeName
where a.brandname <> "" or a.brandname is not null;
create temporary table temp_val_2
(
ctreeid varchar(100) default '',
ctreecode varchar(100) default '',
ctreenodeid varchar(100) default '',
ctreeperiodid varchar(100) default '',
typeid varchar(100) default '',
istopmost varchar(100) default '',
prevnodeid varchar(100) default '',
brandid varchar(100) default '',
brandname varchar(100) default '');
insert into temp_val_2
select a.ctreeid,
a.ctreecode,
c.ctreenodeid,
b.ctreeperiodid,
c.typeid,
c.istopmost,
d.prevnodeid,
f.brandid,
f.brandname
from ctrees a
inner join ctreeperiods b
on a.ctreeid = b.ctreeid
inner join ctreenodes c
on b.ctreeperiodid = c.ctreeperiodid
inner join ctreedefn d
on b.ctreeperiodid = d.ctreeperiodid
and c.ctreenodeid = d.ctreenodeid
inner join ctreebranchtypes e
on c.typeid = e.ctreebranchtypeid
inner join brands f
on c.branchid = f.brandid
where a.ctreecode = str_cTreeName
and e.ctreebranchtypename = 'brands';
ALTER TABLE temp_val
ADD BrandID int;
update temp_val a
inner join temp_val_2 b
on a.prevnodeid = b.ctreenodeid
and a.ctreeid = b.ctreeid
and a.ctreeperiodid = b.ctreeperiodid
set a.Brandid = b.Brandid;
ALTER TABLE temp
ADD CtreeID int;
ALTER TABLE temp
ADD BrandID int;
update temp a
inner join temp_val b
on a.BrandName = b.BrandName
set a.Brandid = b.Brandid;
update temp a
inner join temp_val b
on a.BrandName = b.BrandName
set a.Ctreeid = b.ctreeid;
create temporary table temp_validation
( rowid int,
BrandID varchar(100) default '',
BrandName varchar(100) default '',
Forecasted_Value1 varchar(100) default '',
Forecasted_Value2 varchar(100) default '',
Forecasted_Value3 varchar(100) default '',
Forecasted_Value4 varchar(100) default '',
Forecasted_Value5 varchar(100) default '',
Forecasted_Value6 varchar(100) default '',
Forecasted_Value7 varchar(100) default '',
Forecasted_Value8 varchar(100) default '',
Forecasted_Value9 varchar(100) default '',
Forecasted_Value10 varchar(100) default '',
Forecasted_Value11 varchar(100) default '',
Forecasted_Value12 varchar(100) default '',
Ctreeid varchar(100) default '',
EYEAR varchar(100) default '',
int_f_Period varchar(100) default '',
strForecastTree varchar(100) default '');
insert into temp_validation(
rowid,
BrandID,
BrandName,
Forecasted_Value1 ,
Forecasted_Value2 ,
Forecasted_Value3 ,
Forecasted_Value4 ,
Forecasted_Value5 ,
Forecasted_Value6 ,
Forecasted_Value7 ,
Forecasted_Value8 ,
Forecasted_Value9 ,
Forecasted_Value10 ,
Forecasted_Value11 ,
Forecasted_Value12 ,
Ctreeid ,
eyear,
int_f_Period,
strForecastTree)
select
t.rowid,
t.BrandID,
t.BrandName,
t.Forecasted_Value1 ,
t.Forecasted_Value2 ,
t.Forecasted_Value3 ,
t.Forecasted_Value4 ,
t.Forecasted_Value5 ,
t.Forecasted_Value6 ,
t.Forecasted_Value7 ,
t.Forecasted_Value8 ,
t.Forecasted_Value9 ,
t.Forecasted_Value10 ,
t.Forecasted_Value11 ,
t.Forecasted_Value12 ,
t.ctreeId ,
t.eyear,
t.f_period,
t.strTreeName
from temp t;
update temp_validation
set BrandName = REPLACE(BrandName, '"', '');
update temp_validation
set BrandName = REPLACE(BrandName, ',', '');
update temp_validation
set Forecasted_Value1 = REPLACE(Forecasted_Value1, ',', '');
update temp_validation
set Forecasted_Value2 = REPLACE(Forecasted_Value2, ',', '');
update temp_validation
set Forecasted_Value3 = REPLACE(Forecasted_Value3, ',', '');
update temp_validation
set Forecasted_Value4 = REPLACE(Forecasted_Value4, ',', '');
update temp_validation
set Forecasted_Value5 = REPLACE(Forecasted_Value5, ',', '');
update temp_validation
set Forecasted_Value6 = REPLACE(Forecasted_Value6, ',', '');
update temp_validation
set Forecasted_Value7 = REPLACE(Forecasted_Value7, ',', '');
update temp_validation
set Forecasted_Value8 = REPLACE(Forecasted_Value8, ',', '');
update temp_validation
set Forecasted_Value9 = REPLACE(Forecasted_Value9, ',', '');
update temp_validation
set Forecasted_Value10 = REPLACE(Forecasted_Value10, ',', '');
update temp_validation
set Forecasted_Value11 = REPLACE(Forecasted_Value11, ',', '');
update temp_validation
set Forecasted_Value12 = REPLACE(Forecasted_Value12, ',', '');
create temporary table tmpReturn (ireturn1 int default 0,
ireturn2 int default 0,
ireturn3 int default 0,
ireturn4 int default 0,
ireturn5 int default 0,
ireturn6 int default 0,
ireturn7 int default 0,
ireturn8 int default 0,
ireturn9 int default 0,
rowid int,
BrandID varchar(100) default '',
BrandName varchar(100) default '',
Forecasted_Value1 varchar(100) default '',
Forecasted_Value2 varchar(100) default '',
Forecasted_Value3 varchar(100) default '',
Forecasted_Value4 varchar(100) default '',
Forecasted_Value5 varchar(100) default '',
Forecasted_Value6 varchar(100) default '',
Forecasted_Value7 varchar(100) default '',
Forecasted_Value8 varchar(100) default '',
Forecasted_Value9 varchar(100) default '',
Forecasted_Value10 varchar(100) default '',
Forecasted_Value11 varchar(100) default '',
Forecasted_Value12 varchar(100) default '',
Ctreeid varchar(100) default '',
EYEAR varchar(100) default '',
int_f_Period varchar(100) default '',
strForecastTree varchar(100) default '');
if exists(select 1 from temp where BrandName like '%,%'
and Forecasted_Value1 like '%,%'
and Forecasted_Value2 like '%,%'
and Forecasted_Value3 like '%,%'
and Forecasted_Value4 like '%,%'
and Forecasted_Value5 like '%,%'
and Forecasted_Value6 like '%,%'
and Forecasted_Value7 like '%,%'
and Forecasted_Value8 like '%,%'
and Forecasted_Value9 like '%,%'
and Forecasted_Value10 like '%,%'
and Forecasted_Value11 like '%,%'
and Forecasted_Value12 like '%,%') then
insert into tmpReturn (ireturn2)
values (-2);
end if;
if exists(select * from temp_validation where BrandName = '') then
insert into tmpReturn(ireturn3, rowid)
select distinct -3, rowid from temp_validation where BrandName = '';
end if;
select count(1)
into recordcount
from temp_validation
where BrandName IS NOT NULL;
select count(distinct BrandName)
into uniquerecordcount
from temp_validation
where BrandName IS NOT NULL;
if recordcount <> uniquerecordcount then
insert into tmpReturn(ireturn5, BrandName, Brandid, rowid)
select
distinct -5,
BrandName, brandid, rowid
from temp_validation
where brandid is not null
group by brandid
having count(1) > 1;
end if;
if exists(select * from temp_validation
where ctreeid is null and brandname <> '' and brandname is not null)
then
insert into tmpReturn(ireturn6,BrandName, rowid)
select distinct -6, BrandName, rowid from temp_validation
where ctreeid is null and brandname <> '' and brandname is not null;
end if;
select count(1)
into recordcount
from temp_validation
where BrandID IS NOT NULL;
select count(distinct BrandID)
into uniquerecordcount
from temp_validation
where BrandID IS NOT NULL;
select count(1)
into recordcount2
from temp_validation;
if recordcount2 = 0 then
insert into tmpReturn(ireturn7,BrandName, rowid)
select -7,
'',
'';
end if;
if not exists (select * from tmpReturn) then
if exists(select * from brandforecast where eyear = ieyear and eperiod = iePeriod) then
insert into tmpReturn(ireturn9)
values (-9);
else
insert into tmpReturn (ireturn1)
values (1);
end if;
end if;
if exists (select * from tmpReturn) then
delete
from temp_validation
where brandid IN (select brandid from tmpReturn
where ireturn5 = -5);
delete a
from temp_validation a
right join tmpReturn b
on a.rowid = b.rowid;
end if;
delete from tbl_brandforecast_list ;
insert
into tbl_brandforecast_list
(
BrandID ,
BrandName ,
Forecasted_Value1 ,
Forecasted_Value2 ,
Forecasted_Value3 ,
Forecasted_Value4 ,
Forecasted_Value5 ,
Forecasted_Value6 ,
Forecasted_Value7 ,
Forecasted_Value8 ,
Forecasted_Value9 ,
Forecasted_Value10 ,
Forecasted_Value11 ,
Forecasted_Value12 ,
eyear ,
ctreeid,
ePeriod
)
select
tv.BrandID ,
tv.BrandName ,
tv.Forecasted_Value1 ,
tv.Forecasted_Value2 ,
tv.Forecasted_Value3 ,
tv.Forecasted_Value4 ,
tv.Forecasted_Value5 ,
tv.Forecasted_Value6 ,
tv.Forecasted_Value7 ,
tv.Forecasted_Value8 ,
tv.Forecasted_Value9 ,
tv.Forecasted_Value10 ,
tv.Forecasted_Value11 ,
tv.Forecasted_Value12 ,
tv.eyear ,
tv.ctreeid,
tv.int_f_Period
from temp_validation tv
WHERE tv.BrandID IS NOT NULL and tv.ctreeID IS NOT NULL;
delete from brandforecast_tmp ;
insert
into brandforecast_tmp
select ireturn1,
ireturn2,
ireturn3,
ireturn4,
ireturn5,
ireturn6,
ireturn7,
ireturn8,
ireturn9,
rowid,
BrandID,
BrandName
from tmpReturn;
END