DBA Data[Home] [Help]

PACKAGE BODY: APPS.BIM_CAMP_COLLECTION

Source


1 PACKAGE BODY bim_camp_collection AS
2 /*$Header: bimccolb.pls 120.1 2005/06/06 14:52:28 appldev  $*/
3    g_pkg_name    CONSTANT VARCHAR2(40) := 'bim_camp_collection_new_t';
4    g_file_name   CONSTANT VARCHAR2(40) := 'bimccolb.pls';
5    PROCEDURE bim_dates_pop
6    IS
7       CURSOR c_month(l_date IN DATE)
8       IS
9          SELECT start_date
10            FROM gl_periods
11           WHERE period_type = 'Month'
12             AND period_set_name =
13                             fnd_profile.VALUE('AMS_CAMPAIGN_DEFAULT_CALENDER')
14             AND TRUNC(start_date) >= TRUNC(l_date)
15             AND TRUNC(start_date) <= SYSDATE
16 			AND ADJUSTMENT_PERIOD_FLAG ='N';
17       CURSOR c_quarter(l_date IN DATE)
18       IS
19          SELECT start_date,
20                 period_num
21            FROM gl_periods
22           WHERE period_type = 'Quarter'
23             AND period_set_name =
24                             fnd_profile.VALUE('AMS_CAMPAIGN_DEFAULT_CALENDER')
25             AND TRUNC(start_date) >= TRUNC(l_date)
26             AND TRUNC(start_date) <= SYSDATE;
27       CURSOR c_year(l_date IN DATE)
28       IS
29          SELECT start_date,
30                 period_num
31            FROM gl_periods
32           WHERE period_type = 'Year'
33             AND period_set_name =
34                             fnd_profile.VALUE('AMS_CAMPAIGN_DEFAULT_CALENDER')
35             AND (   TRUNC(l_date) BETWEEN start_date AND end_date
36                  OR TRUNC(l_date) < start_date
37                 )
38             AND TRUNC(start_date) <= SYSDATE;
39       l_min_start_date   DATE;
40    BEGIN
41       EXECUTE IMMEDIATE 'TRUNCATE TABLE bim_dates';
42       SELECT MIN(start_date)
43         INTO l_min_start_date
44         FROM bim_rep_history
45        WHERE OBJECT = 'CAMPAIGN';
46       FOR x IN c_month(l_min_start_date)
47       LOOP
48          --BEGIN
49          INSERT INTO bim_dates
50                      (trdate,
51                       creation_date,
52                       last_update_date,
53                       created_by,
54                       last_updated_by,
55                       last_update_login,
56                       security_group_id,
57                       fiscal_month,
58                       fiscal_month_end,
59                       fiscal_month_num,
60                       fiscal_month_start,
61                       fiscal_qtr,
62                       fiscal_qtr_end,
63                       fiscal_qtr_num,
64                       fiscal_qtr_start,
65                       fiscal_year,
66                       fiscal_year_end,
67                       fiscal_year_start,
68                       pre_fiscal_month_end,
69                       pre_fiscal_month_start,
70                       pre_fiscal_qtr_end,
71                       pre_fiscal_qtr_start,
72                       pre_fiscal_year_end,
73                       pre_fiscal_year_start,
74                       period_type
75                      )
76             SELECT x.start_date,
77                    SYSDATE,
78                    SYSDATE,
79                    -1,
80                    -1,
81                    -1,
82                    -1,
83                    bim_set_of_books.get_fiscal_month(x.start_date, 0),
84                    bim_set_of_books.get_fiscal_month_end(x.start_date, 0),
85                    bim_set_of_books.get_fiscal_month_num(x.start_date, 0),
86                    bim_set_of_books.get_fiscal_month_start(x.start_date, 0),
87                    bim_set_of_books.get_fiscal_qtr(x.start_date, 0),
88                    bim_set_of_books.get_fiscal_qtr_end(x.start_date, 0),
89                    bim_set_of_books.get_fiscal_qtr_num(x.start_date, 0),
90                    bim_set_of_books.get_fiscal_qtr_start(x.start_date, 0),
91                    bim_set_of_books.get_fiscal_year(x.start_date, 0),
92                    bim_set_of_books.get_fiscal_year_end(x.start_date, 0),
93                    bim_set_of_books.get_fiscal_year_start(x.start_date, 0),
94                    bim_set_of_books.get_pre_fiscal_month_end(x.start_date, 0),
95                    bim_set_of_books.get_pre_fiscal_month_start(x.start_date, 0),
96                    bim_set_of_books.get_pre_fiscal_qtr_end(x.start_date, 0),
97                    bim_set_of_books.get_pre_fiscal_qtr_start(x.start_date, 0),
98                    bim_set_of_books.get_pre_fiscal_year_end(x.start_date, 0),
99                    bim_set_of_books.get_pre_fiscal_year_start(x.start_date, 0),
100                    'MONTH'
101               FROM DUAL;
102 
103 --commit;
104 
105       /* EXCEPTION
106       WHEN OTHERS THEN
107            DBMS_OUTPUT.PUT_LINE(SQLERRM(SQLCODE));
108            END; */
109       END LOOP;
110 -------------------------------------------------------------------------------------
111       FOR x IN c_quarter(l_min_start_date)
112       LOOP
113          -- BEGIN
114          INSERT INTO bim_dates
115                      (trdate,
116                       creation_date,
117                       last_update_date,
118                       created_by,
119                       last_updated_by,
120                       last_update_login,
121                       security_group_id,
122                       fiscal_month,
123                       fiscal_month_end,
124                       fiscal_month_num,
125                       fiscal_month_start,
126                       fiscal_qtr,
127                       fiscal_qtr_end,
128                       fiscal_qtr_num,
129                       fiscal_qtr_start,
130                       fiscal_year,
131                       fiscal_year_end,
132                       fiscal_year_start,
133                       pre_fiscal_month_end,
134                       pre_fiscal_month_start,
135                       pre_fiscal_qtr_end,
136                       pre_fiscal_qtr_start,
137                       pre_fiscal_year_end,
138                       pre_fiscal_year_start,
139                       period_type
140                      )
141             SELECT x.start_date,
142                    SYSDATE,
143                    SYSDATE,
144                    -1,
145                    -1,
146                    -1,
147                    -1,
148                    bim_set_of_books.get_fiscal_month(x.start_date, 0),
149                    bim_set_of_books.get_fiscal_month_end(x.start_date, 0),
150                    bim_set_of_books.get_fiscal_month_num(x.start_date, 0),
151                    bim_set_of_books.get_fiscal_month_start(x.start_date, 0),
152                    bim_set_of_books.get_fiscal_qtr(x.start_date, 0),
153                    bim_set_of_books.get_fiscal_qtr_end(x.start_date, 0),
154                    bim_set_of_books.get_fiscal_qtr_num(x.start_date, 0),
155                    bim_set_of_books.get_fiscal_qtr_start(x.start_date, 0),
156                    bim_set_of_books.get_fiscal_year(x.start_date, 0),
157                    bim_set_of_books.get_fiscal_year_end(x.start_date, 0),
158                    bim_set_of_books.get_fiscal_year_start(x.start_date, 0),
159                    bim_set_of_books.get_pre_fiscal_month_end(x.start_date, 0),
160                    bim_set_of_books.get_pre_fiscal_month_start(x.start_date, 0),
161                    bim_set_of_books.get_pre_fiscal_qtr_end(x.start_date, 0),
162                    bim_set_of_books.get_pre_fiscal_qtr_start(x.start_date, 0),
163                    bim_set_of_books.get_pre_fiscal_year_end(x.start_date, 0),
164                    bim_set_of_books.get_pre_fiscal_year_start(x.start_date, 0),
165                    'QUARTER'
166               FROM DUAL;
167 
168 --commit;
169 
170       /* EXCEPTION
171        WHEN OTHERS THEN
172             DBMS_OUTPUT.PUT_LINE(SQLERRM(SQLCODE));
173             END; */
174       END LOOP;
175 -------------------------------------------------------------------------------------
176       FOR x IN c_year(l_min_start_date)
177       LOOP
178          -- BEGIN
179          INSERT INTO bim_dates
180                      (trdate,
181                       creation_date,
182                       last_update_date,
183                       created_by,
184                       last_updated_by,
185                       last_update_login,
186                       security_group_id,
187                       fiscal_month,
188                       fiscal_month_end,
189                       fiscal_month_num,
190                       fiscal_month_start,
191                       fiscal_qtr,
192                       fiscal_qtr_end,
193                       fiscal_qtr_num,
194                       fiscal_qtr_start,
195                       fiscal_year,
196                       fiscal_year_end,
197                       fiscal_year_start,
198                       pre_fiscal_month_end,
199                       pre_fiscal_month_start,
200                       pre_fiscal_qtr_end,
201                       pre_fiscal_qtr_start,
202                       pre_fiscal_year_end,
203                       pre_fiscal_year_start,
204                       period_type
205                      )
206             SELECT x.start_date,
207                    SYSDATE,
208                    SYSDATE,
209                    -1,
210                    -1,
211                    -1,
212                    -1,
213                    bim_set_of_books.get_fiscal_month(x.start_date, 0),
214                    bim_set_of_books.get_fiscal_month_end(x.start_date, 0),
215                    bim_set_of_books.get_fiscal_month_num(x.start_date, 0),
216                    bim_set_of_books.get_fiscal_month_start(x.start_date, 0),
217                    bim_set_of_books.get_fiscal_qtr(x.start_date, 0),
218                    bim_set_of_books.get_fiscal_qtr_end(x.start_date, 0),
219                    bim_set_of_books.get_fiscal_qtr_num(x.start_date, 0),
220                    bim_set_of_books.get_fiscal_qtr_start(x.start_date, 0),
221                    bim_set_of_books.get_fiscal_year(x.start_date, 0),
222                    bim_set_of_books.get_fiscal_year_end(x.start_date, 0),
223                    bim_set_of_books.get_fiscal_year_start(x.start_date, 0),
224                    bim_set_of_books.get_pre_fiscal_month_end(x.start_date, 0),
225                    bim_set_of_books.get_pre_fiscal_month_start(x.start_date, 0),
226                    bim_set_of_books.get_pre_fiscal_qtr_end(x.start_date, 0),
227                    bim_set_of_books.get_pre_fiscal_qtr_start(x.start_date, 0),
228                    bim_set_of_books.get_pre_fiscal_year_end(x.start_date, 0),
229                    bim_set_of_books.get_pre_fiscal_year_start(x.start_date, 0),
230                    'YEAR'
231               FROM DUAL;
232 --commit;
233       /* EXCEPTION
234        WHEN OTHERS THEN
235             DBMS_OUTPUT.PUT_LINE(SQLERRM(SQLCODE));
236             END; */
237       END LOOP;
238 -------------------------------------------------------------------------------------
239    /* EXCEPTION
240    WHEN OTHERS THEN
241             DBMS_OUTPUT.PUT_LINE(SQLERRM(SQLCODE)); */
242    END bim_dates_pop;
243 -----------------------------------------------------------------------------------------
244    PROCEDURE get_increment_mqy_data
245    IS
246       l_org_id                       NUMBER;
247       l_fiscal_month                 VARCHAR2(10);
248       l_fiscal_month_end             DATE;
249       l_fiscal_month_start           DATE;
250       l_pre_fiscal_month_end         DATE;
251       l_pre_fiscal_month_start       DATE;
252       l_pre_pre_fiscal_month_start   DATE;
253       l_fiscal_qtr                   VARCHAR2(10);
254       l_fiscal_qtr_end               DATE;
255       l_fiscal_qtr_start             DATE;
256       l_pre_fiscal_qtr_end           DATE;
257       l_pre_fiscal_qtr_start         DATE;
258       l_fiscal_roll_year_start       DATE;
259       l_fiscal_year                  VARCHAR2(10);
260       l_fiscal_year_end              DATE;
261       l_fiscal_year_start            DATE;
262       l_pre_fiscal_year_end          DATE;
263       l_pre_fiscal_year_start        DATE;
264       --l_pre_fiscal_roll_year_end           DATE;
265       l_pre_fiscal_roll_year_start   DATE;
266       l_min_start_date               DATE;
267       l_sysdate  date := trunc(sysdate);--'02-SEP-2000';
268       sysdate_1  date := TRUNC(SYSDATE- 1);
269       CURSOR chk_history_data
270       IS
271          SELECT MIN(start_date)
272            FROM bim_rep_history
273           WHERE OBJECT = 'CAMPAIGN';
274    BEGIN
275       OPEN chk_history_data;
276       FETCH chk_history_data INTO l_min_start_date;
277       CLOSE chk_history_data;
278       l_org_id := 0;
279       l_fiscal_month := bim_set_of_books.get_fiscal_month(l_SYSDATE - 1, l_org_id);
280       l_fiscal_month_end := bim_set_of_books.get_fiscal_month_end(l_SYSDATE - 1, l_org_id);
281       l_fiscal_month_start := bim_set_of_books.get_fiscal_month_start(l_SYSDATE - 1, l_org_id);
282       l_pre_fiscal_month_end := bim_set_of_books.get_fiscal_month_end(l_fiscal_month_start - 1,l_org_id);
283       l_pre_fiscal_month_start := bim_set_of_books.get_pre_fiscal_month_start(l_SYSDATE - 1, l_org_id);
284       l_fiscal_qtr := bim_set_of_books.get_fiscal_qtr(l_SYSDATE - 1, l_org_id);
285       l_fiscal_qtr_end := bim_set_of_books.get_fiscal_qtr_end(l_SYSDATE - 1, l_org_id);
286       l_fiscal_qtr_start := bim_set_of_books.get_fiscal_qtr_start(l_SYSDATE - 1, l_org_id);
287       l_pre_fiscal_qtr_end := bim_set_of_books.get_fiscal_qtr_end(l_fiscal_qtr_start - 1, l_org_id);
288       l_pre_fiscal_qtr_start := bim_set_of_books.get_pre_fiscal_qtr_start(l_SYSDATE - 1, l_org_id);
289       l_fiscal_roll_year_start := bim_set_of_books.get_fiscal_roll_year_start(l_SYSDATE - 1, l_org_id);
290       l_fiscal_year := bim_set_of_books.get_fiscal_year(l_SYSDATE - 1, l_org_id);
291       l_fiscal_year_end := bim_set_of_books.get_fiscal_year_end(l_SYSDATE - 1, l_org_id);
292       l_fiscal_year_start := bim_set_of_books.get_fiscal_year_start(l_SYSDATE - 1, l_org_id);
293       l_pre_fiscal_roll_year_start := bim_set_of_books.get_pre_fiscal_roll_year_start(l_SYSDATE - 1,l_org_id);
294   --   EXECUTE IMMEDIATE 'TRUNCATE TABLE bim_r_camp_collection';
295 /* START OF THE INCREMENT DATA INSERT */
296       ams_utility_pvt.write_conc_log('BIM_CAMP_COLLECTION: INCREMENT INSERT START');
297       INSERT INTO  /*+ append parallel(i,5) */
298            bim_r_camp_collection i
299                   (campaign_area1,
300                    campaign_area2,
301                    schedule_area1,
302                    schedule_area2,
303                    campaign_country,
304                    schedule_country,
305                    business_unit_id,
306                    resource_id,
307                    campaign_type,
308                    schedule_activity_type,
309                    campaign_status,
310                    schedule_status,
311                    aggregate_by,
312                    display_type,
313                    report_type,
314                    YEAR,
315                    qtr,
316                    MONTH,
317                    current_count_value,
318                    current_started_value,
319                    current_ended_value,
320                    previous_count_value,
321                    previous_started_value,
322                    previous_ended_value
323                   )
324          SELECT  /* +INDEX_FFS(R,BIM_DBI_U2) */
325 	         'N' campaign_area1,
326                   'N' campaign_area2,
327                   'N' schedule_area1,
328                   schedule_area2,
329                   'N' campaign_country,
330                   schedule_country,
331                   business_unit_id,
332                    resource_id,
333                   'N' campaign_type,
334                   'N' schedule_activity_type,
335                   'N' campaign_status,
336                   'N' schedule_status,
337                   aggregate_by,
338                   display_type,
339                   'BIN' report_type,
340                   'N' YEAR,
341                   'N' qtr,
342                   'N' MONTH,
343                   SUM(curr_count_value) current_count_value,
344                   SUM(curr_started_value) current_started_value,
345                   SUM(curr_ended_value) current_ended_value,
346                   SUM(prev_count_value) previous_count_value,
347                   SUM(prev_started_value) previous_started_value,
348                   SUM(prev_ended_value) previous_ended_value
349              FROM (SELECT  /*+ use_hash(A)    use_hash(B) use_hash(C)  */
350 	                a.campaign_id campaign_id,
351 	                   c.area2_code schedule_area2,
352                             a.country_id schedule_country,
353                             NVL(b.business_unit_id, 0) business_unit_id,
354                            'INCREMENT' aggregate_by,
355                             'DAY' display_type,
356                             SUM(DECODE(TRUNC(start_date_time),
357                                        TRUNC(SYSDATE - 1), 0,
358                                        DECODE(LEAST(TRUNC(start_date_time),
359                                                     TRUNC(SYSDATE - 1)),
360                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
361                                                                                       TRUNC(  SYSDATE
362                                                                                             - 1)),
363                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
364                                                                              0
365                                                                             ),
366                                               0
367                                              )
368                                       )) curr_count_value,
369                             SUM(DECODE(TRUNC(start_date_time),
370                                        TRUNC(SYSDATE - 1), 1,
371                                        0
372                                       )) curr_started_value,
373                             SUM(DECODE(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
374                                        TRUNC(SYSDATE - 1), 1,
375                                        0
376                                       )) curr_ended_value,
377                             SUM(DECODE(TRUNC(start_date_time),
378                                        TRUNC(SYSDATE - 2), 0,
379                                        DECODE(LEAST(TRUNC(start_date_time),
380                                                     TRUNC(SYSDATE - 2)),
381                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
382                                                                                       TRUNC(  SYSDATE
383                                                                                             - 2)),
384                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
385                                                                              0
386                                                                             ),
387                                               0
388                                              )
389                                       )) prev_count_value,
390                             SUM(DECODE(TRUNC(start_date_time),
391                                        TRUNC(SYSDATE - 2), 1,
392                                        0
393                                       )) prev_started_value,
394                             SUM(DECODE(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
395                                        TRUNC(SYSDATE - 2), 1,
396                                        0
397                                       )) prev_ended_value
398                        FROM ams_campaign_schedules_b a,
399                             jtf_loc_hierarchies_b c,
400                             ams_campaigns_all_b b
401                           WHERE a.status_code IN
402                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
403                         AND trunc(a.start_date_time) >= l_min_start_date
404 			and trunc(a.start_date_time) <= sysdate_1
405                         AND a.country_id = c.location_hierarchy_id
406                         AND a.campaign_id = b.campaign_id
407                       GROUP BY a.campaign_id,c.area2_code,
408                             a.country_id,
409                             NVL(b.business_unit_id, 0)
410 ------------------------------------------------------------------------------------------------
411                    UNION ALL
412 ------------------------------------------------------------------------------------------------
413                    SELECT  /*+ use_hash(A)    use_hash(B) use_hash(C)  */
414 				a.campaign_id campaign_id,
415 				c.area2_code schedule_area2,
416                             a.country_id schedule_country,
417                             NVL(b.business_unit_id, 0) business_unit_id,
418                             'INCREMENT' aggregate_by,
419                             'WEEK' display_type,
420                             SUM(DECODE(TRUNC(start_date_time),
421                                        TRUNC(SYSDATE - 8), 0,
422                                        DECODE(LEAST(TRUNC(start_date_time),
423                                                     TRUNC(SYSDATE - 8)),
424                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
425                                                                                       TRUNC(  SYSDATE
426                                                                                             - 8)),
427                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
428                                                                              0
429                                                                             ),
430                                               0
431                                              )
432                                       )) curr_count_value,
433                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
434                                                 TRUNC(SYSDATE - 8)),
435                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
436                                                                             TRUNC(  SYSDATE
437                                                                                   - 1)),
438                                                                       TRUNC(start_date_time), 1,
439                                                                       0
440                                                                      ),
441                                        0
442                                       )) curr_started_value,
443                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
444                                                 TRUNC(SYSDATE - 8)),
445                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
446                                                                             TRUNC(  SYSDATE
447                                                                                   - 1)),
448                                                                       trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
449                                                                       0
450                                                                      ),
451                                        0
452                                       )) curr_ended_value,
453                             SUM(DECODE(TRUNC(start_date_time),
454                                        TRUNC(SYSDATE - 15), 0,
455                                        DECODE(LEAST(TRUNC(start_date_time),
456                                                     TRUNC(SYSDATE - 15)),
457                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
458                                                                                       TRUNC(  SYSDATE
459                                                                                             - 15)),
460                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
461                                                                              0
462                                                                             ),
463                                               0
464                                              )
465                                       )) prev_count_value,
466                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
467                                                 TRUNC(SYSDATE - 15)),
468                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
469                                                                             TRUNC(  SYSDATE
470                                                                                   - 8)),
471                                                                       TRUNC(start_date_time), 1,
472                                                                       0
473                                                                      ),
474                                        0
475                                       )) prev_started_value,
476                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
477                                                 TRUNC(SYSDATE - 15)),
478                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
479                                                                           TRUNC(  SYSDATE
480                                                                                 - 8)),
481                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
482                                                                     0
483                                                                    ),
484                                        0
485                                       )) prev_ended_value
486                        FROM ams_campaign_schedules_b a,
487                             jtf_loc_hierarchies_b c,
488                             ams_campaigns_all_b b
489                       WHERE a.status_code IN
490                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
491                         AND trunc(a.start_date_time) >= l_min_start_date
492 			and trunc(a.start_date_time) <= sysdate_1
493                         AND a.country_id = c.location_hierarchy_id
494                         AND a.campaign_id = b.campaign_id
495 
496                    GROUP BY a.campaign_id ,c.area2_code,
497                             a.country_id,
498                             NVL(b.business_unit_id, 0)
499 ------------------------------------------------------------------------------------------------
500                    UNION ALL
501 ------------------------------------------------------------------------------------------------
502                    SELECT  /*+ use_hash(A)    use_hash(B) use_hash(C)  */
503 				a.campaign_id campaign_id,
504 				c.area2_code schedule_area2,
505                             a.country_id schedule_country,
506                             NVL(b.business_unit_id, 0) business_unit_id,
507                            'INCREMENT' aggregate_by,
508                             'MONTH' display_type,
509                             SUM(DECODE(TRUNC(start_date_time),
510                                        l_fiscal_month_start, 0,
511                                        DECODE(LEAST(TRUNC(start_date_time),
512                                                     l_fiscal_month_start),
513                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
514                                                                                       l_fiscal_month_start),
515                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
516                                                                              0
517                                                                             ),
518                                               0
519                                              )
520                                       )) curr_count_value,
521                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
522                                                 l_fiscal_month_start),
523                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
524                                                                               SYSDATE
525                                                                             - 1),
526                                                                       TRUNC(start_date_time), 1,
527                                                                       0
528                                                                      ),
529                                        0
530                                       )) curr_started_value,
531                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
532                                                 l_fiscal_month_start),
533                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
534                                                                             SYSDATE
535                                                                           - 1),
536                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
537                                                                     0
538                                                                    ),
539                                        0
540                                       )) curr_ended_value,
541                             SUM(DECODE(TRUNC(start_date_time),
542                                        l_pre_fiscal_month_start, 0,
543                                        DECODE(LEAST(TRUNC(start_date_time),
544                                                     l_pre_fiscal_month_start),
545                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
546                                                                                       l_pre_fiscal_month_start),
547                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
548                                                                              0
549                                                                             ),
550                                               0
551                                              )
552                                       )) prev_count_value,
553                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
554                                                 l_pre_fiscal_month_start),
555                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
556                                                                             l_pre_fiscal_month_end),
557                                                                       TRUNC(start_date_time), 1,
558                                                                       0
559                                                                      ),
560                                        0
561                                       )) prev_started_value,
562                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
563                                                 l_pre_fiscal_month_start),
564                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
565                                                                           l_pre_fiscal_month_end),
566                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
567                                                                     0
568                                                                    ),
569                                        0
570                                       )) prev_ended_value
571                        FROM ams_campaign_schedules_b a,
572                             jtf_loc_hierarchies_b c,
573                             ams_campaigns_all_b b
574                       WHERE a.status_code IN
575                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
576                         AND trunc(a.start_date_time) >= l_min_start_date
577 			and trunc(a.start_date_time) <= sysdate_1
578                         AND a.country_id = c.location_hierarchy_id
579                         AND a.campaign_id = b.campaign_id
580 
581                      GROUP BY
582 			a.campaign_id ,c.area2_code,
583                             a.country_id,
584                             NVL(b.business_unit_id, 0)
585 ------------------------------------------------------------------------------------------------
586                    UNION ALL
587 ------------------------------------------------------------------------------------------------
588                    SELECT  /*+ use_hash(A)    use_hash(B) use_hash(C)  */
589 			a.campaign_id campaign_id,
590 			   c.area2_code schedule_area2,
591                             a.country_id schedule_country,
592                             NVL(b.business_unit_id, 0) business_unit_id,
593                             'INCREMENT' aggregate_by,
594                             'QUARTER' display_type,
595                             SUM(DECODE(TRUNC(start_date_time),
596                                        l_fiscal_qtr_start, 0,
597                                        DECODE(LEAST(TRUNC(start_date_time),
598                                                     l_fiscal_qtr_start),
599                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
600                                                                                       l_fiscal_qtr_start),
601                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
602                                                                              0
603                                                                             ),
604                                               0
605                                              )
606                                       )) curr_count_value,
607                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
608                                                 l_fiscal_qtr_start),
609                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
610                                                                               SYSDATE
611                                                                             - 1),
612                                                                       TRUNC(start_date_time), 1,
613                                                                       0
614                                                                      ),
615                                        0
616                                       )) curr_started_value,
617                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
618                                                 l_fiscal_qtr_start),
619                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
620                                                                             SYSDATE
621                                                                           - 1),
622                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
623                                                                     0
624                                                                    ),
625                                        0
626                                       )) curr_ended_value,
627                             SUM(DECODE(TRUNC(start_date_time),
628                                        l_pre_fiscal_qtr_start, 0,
629                                        DECODE(LEAST(TRUNC(start_date_time),
630                                                     l_pre_fiscal_qtr_start),
631                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
632                                                                                       l_pre_fiscal_qtr_start),
633                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
634                                                                              0
635                                                                             ),
636                                               0
637                                              )
638                                       )) prev_count_value,
639                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
640                                                 l_pre_fiscal_qtr_start),
641                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
642                                                                             l_pre_fiscal_qtr_end),
643                                                                       TRUNC(start_date_time), 1,
644                                                                       0
645                                                                      ),
646                                        0
647                                       )) prev_started_value,
648                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
649                                                 l_pre_fiscal_qtr_start),
650                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
651                                                                           l_pre_fiscal_qtr_end),
652                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
653                                                                     0
654                                                                    ),
655                                        0
656                                       )) prev_ended_value
657                        FROM ams_campaign_schedules_b a,
658                             jtf_loc_hierarchies_b c,
659                             ams_campaigns_all_b b
660                       WHERE a.status_code IN
661                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
662                         AND trunc(a.start_date_time) >= l_min_start_date
663 			and trunc(a.start_date_time) <= sysdate_1
664                         AND a.country_id = c.location_hierarchy_id
665                         AND a.campaign_id = b.campaign_id
666                        GROUP BY a.campaign_id,c.area2_code,
667                             a.country_id,
668                             NVL(b.business_unit_id, 0)
669 ------------------------------------------------------------------------------------------------
670                    UNION ALL
671 ------------------------------------------------------------------------------------------------
672                    SELECT  /*+ use_hash(A)    use_hash(B) use_hash(C)  */
673 			a.campaign_id campaign_id,
674 			c.area2_code schedule_area2,
675                             a.country_id schedule_country,
676                             NVL(b.business_unit_id, 0) business_unit_id,
677                             'INCREMENT' aggregate_by,
678                             'YEAR' display_type,
679                             SUM(DECODE(TRUNC(start_date_time),
680                                        l_fiscal_roll_year_start, 0,
681                                        DECODE(LEAST(TRUNC(start_date_time),
682                                                     l_fiscal_roll_year_start),
683                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
684                                                                                       l_fiscal_roll_year_start),
685                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
686                                                                              0
687                                                                             ),
688                                               0
689                                              )
690                                       )) curr_count_value,
691                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
692                                                 l_fiscal_roll_year_start),
693                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
694                                                                               SYSDATE
695                                                                             - 1),
696                                                                       TRUNC(start_date_time), 1,
697                                                                       0
698                                                                      ),
699                                        0
700                                       )) curr_started_value,
701                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
702                                                 l_fiscal_roll_year_start),
703                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
704                                                                             SYSDATE
705                                                                           - 1),
706                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
707                                                                     0
708                                                                    ),
709                                        0
710                                       )) curr_ended_value,
711                             SUM(DECODE(TRUNC(start_date_time),
712                                        l_pre_fiscal_roll_year_start, 0,
713                                        DECODE(LEAST(TRUNC(start_date_time),
714                                                     l_pre_fiscal_roll_year_start),
715                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
716                                                                                       l_pre_fiscal_roll_year_start),
717                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
718                                                                              0
719                                                                             ),
720                                               0
721                                              )
722                                       )) prev_count_value,
723                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
724                                                 l_pre_fiscal_roll_year_start),
725                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
726                                                                               l_fiscal_roll_year_start
727                                                                             - 1),
728                                                                       TRUNC(start_date_time), 1,
729                                                                       0
730                                                                      ),
731                                        0
732                                       )) prev_started_value,
733                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
734                                                 l_pre_fiscal_roll_year_start),
735                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
736                                                                             l_fiscal_roll_year_start
737                                                                           - 1),
738                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
739                                                                     0
740                                                                    ),
741                                        0
742                                       )) prev_ended_value
743                        FROM ams_campaign_schedules_b a,
744                             jtf_loc_hierarchies_b c,
745                             ams_campaigns_all_b b
746                       WHERE a.status_code IN
747                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
748                         AND trunc(a.start_date_time) >= l_min_start_date
749 			and trunc(a.start_date_time) <= sysdate_1
750                         AND a.country_id = c.location_hierarchy_id
751                         AND a.campaign_id = b.campaign_id
752 			GROUP BY
753 			a.campaign_id ,c.area2_code,
754                             a.country_id,
755                             NVL(b.business_unit_id, 0)),ams_act_access_denorm R
756 			    where campaign_id=r.object_id
757 			    and r.object_type='CAMP'
758          GROUP BY schedule_area2,
759                   schedule_country,
760                   business_unit_id,
761                   resource_id,
762                   aggregate_by,
763                   display_type
764 
765 				  ---------- for admin
766 				  union all
767 				  -------------
768 
769 	SELECT  /* +INDEX_FFS(R,BIM_DBI_U2) */
770 	         'N' campaign_area1,
771                   'N' campaign_area2,
772                   'N' schedule_area1,
773                   schedule_area2,
774                   'N' campaign_country,
775                   schedule_country,
776                   business_unit_id,
777                    -1 resource_id,
778                   'N' campaign_type,
779                   'N' schedule_activity_type,
780                   'N' campaign_status,
781                   'N' schedule_status,
782                   aggregate_by,
783                   display_type,
784                   'BIN' report_type,
785                   'N' YEAR,
786                   'N' qtr,
787                   'N' MONTH,
788                   SUM(curr_count_value) current_count_value,
789                   SUM(curr_started_value) current_started_value,
790                   SUM(curr_ended_value) current_ended_value,
791                   SUM(prev_count_value) previous_count_value,
792                   SUM(prev_started_value) previous_started_value,
793                   SUM(prev_ended_value) previous_ended_value
794              FROM (SELECT  /*+ use_hash(A)    use_hash(B) use_hash(C)  */
795 	                a.campaign_id campaign_id,
796 	                   c.area2_code schedule_area2,
797                             a.country_id schedule_country,
798                             NVL(b.business_unit_id, 0) business_unit_id,
799                            'INCREMENT' aggregate_by,
800                             'DAY' display_type,
801                             SUM(DECODE(TRUNC(start_date_time),
802                                        TRUNC(SYSDATE - 1), 0,
803                                        DECODE(LEAST(TRUNC(start_date_time),
804                                                     TRUNC(SYSDATE - 1)),
805                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
806                                                                                       TRUNC(  SYSDATE
807                                                                                             - 1)),
808                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
809                                                                              0
810                                                                             ),
811                                               0
812                                              )
813                                       )) curr_count_value,
814                             SUM(DECODE(TRUNC(start_date_time),
815                                        TRUNC(SYSDATE - 1), 1,
816                                        0
817                                       )) curr_started_value,
818                             SUM(DECODE(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
819                                        TRUNC(SYSDATE - 1), 1,
820                                        0
821                                       )) curr_ended_value,
822                             SUM(DECODE(TRUNC(start_date_time),
823                                        TRUNC(SYSDATE - 2), 0,
824                                        DECODE(LEAST(TRUNC(start_date_time),
825                                                     TRUNC(SYSDATE - 2)),
826                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
827                                                                                       TRUNC(  SYSDATE
828                                                                                             - 2)),
829                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
830                                                                              0
831                                                                             ),
832                                               0
833                                              )
834                                       )) prev_count_value,
835                             SUM(DECODE(TRUNC(start_date_time),
836                                        TRUNC(SYSDATE - 2), 1,
837                                        0
838                                       )) prev_started_value,
839                             SUM(DECODE(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
840                                        TRUNC(SYSDATE - 2), 1,
841                                        0
842                                       )) prev_ended_value
843                        FROM ams_campaign_schedules_b a,
844                             jtf_loc_hierarchies_b c,
845                             ams_campaigns_all_b b
846                           WHERE a.status_code IN
847                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
848                         AND trunc(a.start_date_time) >= l_min_start_date
849 			and trunc(a.start_date_time) <= sysdate_1
850                         AND a.country_id = c.location_hierarchy_id
851                         AND a.campaign_id = b.campaign_id
852                       GROUP BY a.campaign_id,c.area2_code,
853                             a.country_id,
854                             NVL(b.business_unit_id, 0)
855 ------------------------------------------------------------------------------------------------
856                    UNION ALL
857 ------------------------------------------------------------------------------------------------
858                    SELECT  /*+ use_hash(A)    use_hash(B) use_hash(C)  */
859 				a.campaign_id campaign_id,
860 				c.area2_code schedule_area2,
861                             a.country_id schedule_country,
862                             NVL(b.business_unit_id, 0) business_unit_id,
863                             'INCREMENT' aggregate_by,
864                             'WEEK' display_type,
865                             SUM(DECODE(TRUNC(start_date_time),
866                                        TRUNC(SYSDATE - 8), 0,
867                                        DECODE(LEAST(TRUNC(start_date_time),
868                                                     TRUNC(SYSDATE - 8)),
869                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
870                                                                                       TRUNC(  SYSDATE
871                                                                                             - 8)),
872                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
873                                                                              0
874                                                                             ),
875                                               0
876                                              )
877                                       )) curr_count_value,
878                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
879                                                 TRUNC(SYSDATE - 8)),
880                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
881                                                                             TRUNC(  SYSDATE
882                                                                                   - 1)),
883                                                                       TRUNC(start_date_time), 1,
884                                                                       0
885                                                                      ),
886                                        0
887                                       )) curr_started_value,
888                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
889                                                 TRUNC(SYSDATE - 8)),
890                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
891                                                                             TRUNC(  SYSDATE
892                                                                                   - 1)),
893                                                                       trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
894                                                                       0
895                                                                      ),
896                                        0
897                                       )) curr_ended_value,
898                             SUM(DECODE(TRUNC(start_date_time),
899                                        TRUNC(SYSDATE - 15), 0,
900                                        DECODE(LEAST(TRUNC(start_date_time),
901                                                     TRUNC(SYSDATE - 15)),
902                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
903                                                                                       TRUNC(  SYSDATE
904                                                                                             - 15)),
905                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
906                                                                              0
907                                                                             ),
908                                               0
909                                              )
910                                       )) prev_count_value,
911                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
912                                                 TRUNC(SYSDATE - 15)),
913                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
914                                                                             TRUNC(  SYSDATE
915                                                                                   - 8)),
916                                                                       TRUNC(start_date_time), 1,
917                                                                       0
918                                                                      ),
919                                        0
920                                       )) prev_started_value,
921                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
922                                                 TRUNC(SYSDATE - 15)),
923                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
924                                                                           TRUNC(  SYSDATE
925                                                                                 - 8)),
926                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
927                                                                     0
928                                                                    ),
929                                        0
930                                       )) prev_ended_value
931                        FROM ams_campaign_schedules_b a,
932                             jtf_loc_hierarchies_b c,
933                             ams_campaigns_all_b b
934                       WHERE a.status_code IN
935                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
936                         AND trunc(a.start_date_time) >= l_min_start_date
937 			and trunc(a.start_date_time) <= sysdate_1
938                         AND a.country_id = c.location_hierarchy_id
939                         AND a.campaign_id = b.campaign_id
940                    GROUP BY a.campaign_id ,c.area2_code,
941                             a.country_id,
942                             NVL(b.business_unit_id, 0)
943 ------------------------------------------------------------------------------------------------
944                    UNION ALL
945 ------------------------------------------------------------------------------------------------
946                    SELECT  /*+ use_hash(A)    use_hash(B) use_hash(C)  */
947 				a.campaign_id campaign_id,
948 				c.area2_code schedule_area2,
949                             a.country_id schedule_country,
950                             NVL(b.business_unit_id, 0) business_unit_id,
951                            'INCREMENT' aggregate_by,
952                             'MONTH' display_type,
953                             SUM(DECODE(TRUNC(start_date_time),
954                                        l_fiscal_month_start, 0,
955                                        DECODE(LEAST(TRUNC(start_date_time),
956                                                     l_fiscal_month_start),
957                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
958                                                                                       l_fiscal_month_start),
959                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
960                                                                              0
961                                                                             ),
962                                               0
963                                              )
964                                       )) curr_count_value,
965                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
966                                                 l_fiscal_month_start),
967                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
968                                                                               SYSDATE
969                                                                             - 1),
970                                                                       TRUNC(start_date_time), 1,
971                                                                       0
972                                                                      ),
973                                        0
974                                       )) curr_started_value,
975                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
976                                                 l_fiscal_month_start),
977                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
978                                                                             SYSDATE
979                                                                           - 1),
980                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
981                                                                     0
982                                                                    ),
983                                        0
984                                       )) curr_ended_value,
985                             SUM(DECODE(TRUNC(start_date_time),
986                                        l_pre_fiscal_month_start, 0,
987                                        DECODE(LEAST(TRUNC(start_date_time),
988                                                     l_pre_fiscal_month_start),
989                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
990                                                                                       l_pre_fiscal_month_start),
991                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
992                                                                              0
993                                                                             ),
994                                               0
995                                              )
996                                       )) prev_count_value,
997                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
998                                                 l_pre_fiscal_month_start),
999                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
1000                                                                             l_pre_fiscal_month_end),
1001                                                                       TRUNC(start_date_time), 1,
1002                                                                       0
1003                                                                      ),
1004                                        0
1005                                       )) prev_started_value,
1006                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
1007                                                 l_pre_fiscal_month_start),
1008                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
1009                                                                           l_pre_fiscal_month_end),
1010                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
1011                                                                     0
1012                                                                    ),
1013                                        0
1014                                       )) prev_ended_value
1015                        FROM ams_campaign_schedules_b a,
1016                             jtf_loc_hierarchies_b c,
1017                             ams_campaigns_all_b b
1018                       WHERE a.status_code IN
1019                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
1020                         AND trunc(a.start_date_time) >= l_min_start_date
1021 			and trunc(a.start_date_time) <= sysdate_1
1022                         AND a.country_id = c.location_hierarchy_id
1023                         AND a.campaign_id = b.campaign_id
1024                      GROUP BY
1025 			a.campaign_id ,c.area2_code,
1026                             a.country_id,
1027                             NVL(b.business_unit_id, 0)
1028 ------------------------------------------------------------------------------------------------
1029                    UNION ALL
1030 ------------------------------------------------------------------------------------------------
1031                    SELECT  /*+ use_hash(A)    use_hash(B) use_hash(C)  */
1032 			a.campaign_id campaign_id,
1033 			   c.area2_code schedule_area2,
1034                             a.country_id schedule_country,
1035                             NVL(b.business_unit_id, 0) business_unit_id,
1036                             'INCREMENT' aggregate_by,
1037                             'QUARTER' display_type,
1038                             SUM(DECODE(TRUNC(start_date_time),
1039                                        l_fiscal_qtr_start, 0,
1040                                        DECODE(LEAST(TRUNC(start_date_time),
1041                                                     l_fiscal_qtr_start),
1042                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
1043                                                                                       l_fiscal_qtr_start),
1044                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
1045                                                                              0
1046                                                                             ),
1047                                               0
1048                                              )
1049                                       )) curr_count_value,
1050                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
1051                                                 l_fiscal_qtr_start),
1052                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
1053                                                                               SYSDATE
1054                                                                             - 1),
1055                                                                       TRUNC(start_date_time), 1,
1056                                                                       0
1057                                                                      ),
1058                                        0
1059                                       )) curr_started_value,
1060                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
1061                                                 l_fiscal_qtr_start),
1062                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
1063                                                                             SYSDATE
1064                                                                           - 1),
1065                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
1066                                                                     0
1067                                                                    ),
1068                                        0
1069                                       )) curr_ended_value,
1070                             SUM(DECODE(TRUNC(start_date_time),
1071                                        l_pre_fiscal_qtr_start, 0,
1072                                        DECODE(LEAST(TRUNC(start_date_time),
1073                                                     l_pre_fiscal_qtr_start),
1074                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
1075                                                                                       l_pre_fiscal_qtr_start),
1076                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
1077                                                                              0
1078                                                                             ),
1079                                               0
1080                                              )
1081                                       )) prev_count_value,
1082                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
1083                                                 l_pre_fiscal_qtr_start),
1084                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
1085                                                                             l_pre_fiscal_qtr_end),
1086                                                                       TRUNC(start_date_time), 1,
1087                                                                       0
1088                                                                      ),
1089                                        0
1090                                       )) prev_started_value,
1091                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
1092                                                 l_pre_fiscal_qtr_start),
1093                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
1094                                                                           l_pre_fiscal_qtr_end),
1095                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
1096                                                                     0
1097                                                                    ),
1098                                        0
1099                                       )) prev_ended_value
1100                        FROM ams_campaign_schedules_b a,
1101                             jtf_loc_hierarchies_b c,
1102                             ams_campaigns_all_b b
1103                       WHERE a.status_code IN
1104                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
1105                         AND trunc(a.start_date_time) >= l_min_start_date
1106 			and trunc(a.start_date_time) <= sysdate_1
1107                         AND a.country_id = c.location_hierarchy_id
1108                         AND a.campaign_id = b.campaign_id
1109                        GROUP BY a.campaign_id,c.area2_code,
1110                             a.country_id,
1111                             NVL(b.business_unit_id, 0)
1112 ------------------------------------------------------------------------------------------------
1113                    UNION ALL
1114 ------------------------------------------------------------------------------------------------
1115                    SELECT  /*+ use_hash(A)    use_hash(B) use_hash(C)  */
1116 			a.campaign_id campaign_id,
1117 			c.area2_code schedule_area2,
1118                             a.country_id schedule_country,
1119                             NVL(b.business_unit_id, 0) business_unit_id,
1120                             'INCREMENT' aggregate_by,
1121                             'YEAR' display_type,
1122                             SUM(DECODE(TRUNC(start_date_time),
1123                                        l_fiscal_roll_year_start, 0,
1124                                        DECODE(LEAST(TRUNC(start_date_time),
1125                                                     l_fiscal_roll_year_start),
1126                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
1127                                                                                       l_fiscal_roll_year_start),
1128                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
1129                                                                              0
1130                                                                             ),
1131                                               0
1132                                              )
1133                                       )) curr_count_value,
1134                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
1135                                                 l_fiscal_roll_year_start),
1136                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
1137                                                                               SYSDATE
1138                                                                             - 1),
1139                                                                       TRUNC(start_date_time), 1,
1140                                                                       0
1141                                                                      ),
1142                                        0
1143                                       )) curr_started_value,
1144                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
1145                                                 l_fiscal_roll_year_start),
1146                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
1147                                                                             SYSDATE
1148                                                                           - 1),
1149                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
1150                                                                     0
1151                                                                    ),
1152                                        0
1153                                       )) curr_ended_value,
1154                             SUM(DECODE(TRUNC(start_date_time),
1155                                        l_pre_fiscal_roll_year_start, 0,
1156                                        DECODE(LEAST(TRUNC(start_date_time),
1157                                                     l_pre_fiscal_roll_year_start),
1158                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
1159                                                                                       l_pre_fiscal_roll_year_start),
1160                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
1161                                                                              0
1162                                                                             ),
1163                                               0
1164                                              )
1165                                       )) prev_count_value,
1166                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
1167                                                 l_pre_fiscal_roll_year_start),
1168                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
1169                                                                               l_fiscal_roll_year_start
1170                                                                             - 1),
1171                                                                       TRUNC(start_date_time), 1,
1172                                                                       0
1173                                                                      ),
1174                                        0
1175                                       )) prev_started_value,
1176                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
1177                                                 l_pre_fiscal_roll_year_start),
1178                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
1179                                                                             l_fiscal_roll_year_start
1180                                                                           - 1),
1181                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
1182                                                                     0
1183                                                                    ),
1184                                        0
1185                                       )) prev_ended_value
1186                        FROM ams_campaign_schedules_b a,
1187                             jtf_loc_hierarchies_b c,
1188                             ams_campaigns_all_b b
1189                       WHERE a.status_code IN
1190                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
1191                         AND trunc(a.start_date_time) >= l_min_start_date
1192 			and trunc(a.start_date_time) <= sysdate_1
1193                         AND a.country_id = c.location_hierarchy_id
1194                         AND a.campaign_id = b.campaign_id
1195 			GROUP BY
1196 			a.campaign_id ,c.area2_code,
1197                             a.country_id,
1198                             NVL(b.business_unit_id, 0))
1199          GROUP BY schedule_area2,
1200                   schedule_country,
1201                   business_unit_id,
1202                   aggregate_by,
1203                   display_type;
1204 
1205 	---------union all admin end
1206 
1207 --commit;
1208 
1209 /* END OF THE INCREMENT DATA INSERT */
1210       ams_utility_pvt.write_conc_log('BIM_CAMP_COLLECTION: INCREMENT INSERT END');
1211 /* START OF THE MONTH, QUARTER ,YEAR DATA INSERT */
1212       ams_utility_pvt.write_conc_log('BIM_CAMP_COLLECTION: Q/M/Y INSERT START');
1213       INSERT INTO  /*+ append parallel(i,5) */
1214            bim_r_camp_collection  i
1215                   (schedule_area2,
1216                    schedule_country,
1217                    business_unit_id,
1218                    resource_id,
1219                    aggregate_by,
1220                    display_type,
1221                    YEAR,
1222                    qtr,
1223                    MONTH,
1224                    current_count_value,
1225                    previous_count_value,
1226                    current_started_value,
1227                    previous_started_value,
1228                    current_ended_value,
1229                    previous_ended_value,
1230                    report_type
1231                   )
1232          SELECT  /* +INDEX_FFS(R,BIM_DBI_U2) */
1233 	          schedule_area2,
1234                   schedule_country,
1235                   business_unit_id,
1236                   resource_id,
1237                   aggregate_by,
1238                   display_type,
1239                   fiscal_year,
1240                   fiscal_qtr,
1241                   fiscal_month,
1242                   SUM(curr_count) curr_count,
1243                   SUM(pre_count) pre_count,
1244                   SUM(curr_started) curr_started,
1245                   SUM(pre_started) pre_started,
1246                   SUM(curr_ended) curr_ended,
1247                   SUM(pre_ended) pre_ended,
1248                   'BIN' report_type
1249              FROM (
1250 -- FOR THE PERIOD TYPE MONTH
1251                    SELECT    /*+ use_hash(a)    use_hash(b)  use_hash(c) use_hash(d)  */
1252 		            a.campaign_id campaign_id,
1253                              c.area2_code schedule_area2,
1254                              a.country_id schedule_country,
1255                              NVL(d.business_unit_id, 0) business_unit_id,
1256                              'MONTH' aggregate_by,
1257                              'Z' display_type,
1258                              b.fiscal_year fiscal_year,
1259                              b.fiscal_qtr fiscal_qtr,
1260                              b.fiscal_month fiscal_month,
1261                              COUNT(a.schedule_id) curr_count,
1262                              0 pre_count,
1263                              0 curr_started,
1264                              0 pre_started,
1265                              0 curr_ended,
1266                              0 pre_ended
1267                        FROM ams_campaign_schedules_b a,
1268                             bim_dates b,
1269                             jtf_loc_hierarchies_b c,
1270                             ams_campaigns_all_b d
1271                       WHERE a.status_code IN
1272                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
1273                         AND trunc(a.start_date_time) < b.fiscal_month_start
1274                         AND a.country_id = c.location_hierarchy_id
1275                         AND a.campaign_id = d.campaign_id
1276                         AND b.period_type = 'MONTH'
1277                         AND trunc(a.start_date_time) >= l_min_start_date
1278                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date)) >=
1279                                                           b.fiscal_month_start
1280                    GROUP BY a.campaign_id ,c.area2_code,
1281                             a.country_id,
1282                             NVL(d.business_unit_id, 0),
1283 
1284                             b.fiscal_year,
1285                             b.fiscal_qtr,
1286                             b.fiscal_month
1287 ------------------------------------------------------------------------------------------
1288                    UNION ALL
1289 ------------------------------------------------------------------------------------------
1290                    SELECT    /*+ use_hash(a)    use_hash(b)  use_hash(c) use_hash(d) */
1291                              a.campaign_id campaign_id,
1292 			     c.area2_code schedule_area2,
1293                              a.country_id schedule_country,
1294                              NVL(d.business_unit_id, 0) business_unit_id,
1295                              'MONTH' aggregate_by,
1296                              'Z' display_type,
1297                              b.fiscal_year fiscal_year,
1298                              b.fiscal_qtr fiscal_qtr,
1299                              b.fiscal_month fiscal_month,
1300                              0 curr_count,
1301                              COUNT(a.schedule_id) pre_count,
1302                              0 curr_started,
1303                              0 pre_started,
1304                              0 curr_ended,
1305                              0 pre_ended
1306                        FROM ams_campaign_schedules_b a,
1307                             bim_dates b,
1308                             jtf_loc_hierarchies_b c,
1309                             ams_campaigns_all_b d
1310                       WHERE a.status_code IN
1311                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
1312                         AND trunc(a.start_date_time) < b.pre_fiscal_month_start
1313                         AND a.country_id = c.location_hierarchy_id
1314                         AND a.campaign_id = d.campaign_id
1315                         AND trunc(a.start_date_time) >= l_min_start_date
1316                         AND b.period_type = 'MONTH'
1317                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date)) >=
1318                                                       b.pre_fiscal_month_start
1319                    GROUP BY a.campaign_id,c.area2_code,
1320                             a.country_id,
1321                             NVL(d.business_unit_id, 0),
1322                             b.fiscal_year,
1323                             b.fiscal_qtr,
1324                             b.fiscal_month
1325 ------------------------------------------------------------------------------------------
1326                    UNION ALL
1327 ------------------------------------------------------------------------------------------
1328                    SELECT    /*+ use_hash(a)    use_hash(b)  use_hash(c) use_hash(d) */
1329                              a.campaign_id campaign_id,
1330 			     c.area2_code schedule_area2,
1331                              a.country_id schedule_country,
1332                              NVL(d.business_unit_id, 0) business_unit_id,
1333                              'MONTH' aggregate_by,
1334                              'Z' display_type,
1335                              b.fiscal_year fiscal_year,
1336                              b.fiscal_qtr fiscal_qtr,
1337                              b.fiscal_month fiscal_month,
1338                              0 curr_count,
1339                              0 pre_count,
1340                              COUNT(schedule_id) curr_started,
1341                              0 pre_started,
1342                              0 curr_ended,
1343                              0 pre_ended
1344                        FROM ams_campaign_schedules_b a,
1345                             bim_dates b,
1346                             jtf_loc_hierarchies_b c,
1347                             ams_campaigns_all_b d
1348                       WHERE a.status_code IN
1349                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
1350                         AND trunc(a.start_date_time) BETWEEN b.fiscal_month_start
1351                                                   AND b.fiscal_month_end
1352                         AND a.country_id = c.location_hierarchy_id
1353                         AND a.campaign_id = d.campaign_id
1354                         AND trunc(a.start_date_time) >= l_min_start_date
1355                         AND b.period_type = 'MONTH'
1356                    GROUP BY a.campaign_id ,
1357 		          c.area2_code,
1358                             a.country_id,
1359                             NVL(d.business_unit_id, 0),
1360                             b.fiscal_year,
1361                             b.fiscal_qtr,
1362                             b.fiscal_month
1363 ------------------------------------------------------------------------------------------
1364                    UNION ALL
1365 ------------------------------------------------------------------------------------------
1366                    SELECT    /*+ use_hash(a)    use_hash(b)  use_hash(c) use_hash(d) */
1367                              a.campaign_id campaign_id,
1368 			     c.area2_code schedule_area2,
1369                              a.country_id schedule_country,
1370                              NVL(d.business_unit_id, 0) business_unit_id,
1371                              'MONTH' aggregate_by,
1372                              'Z' display_type,
1373                              b.fiscal_year fiscal_year,
1374                              b.fiscal_qtr fiscal_qtr,
1375                              b.fiscal_month fiscal_month,
1376                              0 curr_count,
1377                              0 pre_count,
1378                              0 curr_started,
1379                              COUNT(schedule_id) pre_started,
1380                              0 curr_ended,
1381                              0 pre_ended
1382                        FROM ams_campaign_schedules_b a,
1383                             bim_dates b,
1384                             jtf_loc_hierarchies_b c,
1385                             ams_campaigns_all_b d
1386                       WHERE a.status_code IN
1387                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
1388                         AND trunc(a.start_date_time) BETWEEN b.pre_fiscal_month_start
1389                                                   AND(b.fiscal_month_start - 1
1390                                                      )
1391                         AND a.country_id = c.location_hierarchy_id
1392                         AND a.campaign_id = d.campaign_id
1393                         AND trunc(a.start_date_time) >= l_min_start_date
1394                         AND b.period_type = 'MONTH'
1395                    GROUP BY a.campaign_id ,c.area2_code,
1396                             a.country_id,
1397                             NVL(d.business_unit_id, 0),
1398                             b.fiscal_year,
1399                             b.fiscal_qtr,
1400                             b.fiscal_month
1401 ------------------------------------------------------------------------------------------
1402                    UNION ALL
1403 ------------------------------------------------------------------------------------------
1404                    SELECT    /*+ use_hash(a)    use_hash(b)  use_hash(c) use_hash(d) */
1405                              a.campaign_id campaign_id,
1406 			     c.area2_code schedule_area2,
1407                              a.country_id schedule_country,
1408                              NVL(d.business_unit_id, 0) business_unit_id,
1409                              'MONTH' aggregate_by,
1410                              'Z' display_type,
1411                              b.fiscal_year fiscal_year,
1412                              b.fiscal_qtr fiscal_qtr,
1413                              b.fiscal_month fiscal_month,
1414                              0 curr_count,
1415                              0 pre_count,
1416                              0 curr_started,
1417                              0 pre_started,
1418                              COUNT(schedule_id) curr_ended,
1419                              0 pre_ended
1420                        FROM ams_campaign_schedules_b a,
1421                             bim_dates b,
1422                             jtf_loc_hierarchies_b c,
1423                             ams_campaigns_all_b d
1424                       WHERE a.status_code IN
1425                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
1426                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date))
1427                                BETWEEN b.fiscal_month_start
1428                                    AND b.fiscal_month_end
1429                         AND a.country_id = c.location_hierarchy_id
1430                         AND a.campaign_id = d.campaign_id
1431                         AND trunc(a.start_date_time) >= l_min_start_date
1432                         AND b.period_type = 'MONTH'
1433                    GROUP BY a.campaign_id,
1434 		            c.area2_code,
1435                             a.country_id,
1436                             NVL(d.business_unit_id, 0),
1437                             b.fiscal_year,
1438                             b.fiscal_qtr,
1439                             b.fiscal_month
1440 ------------------------------------------------------------------------------------------
1441                    UNION ALL
1442 ------------------------------------------------------------------------------------------
1443                    SELECT    /*+ use_hash(a)   use_hash(b)  use_hash(c) use_hash(d) */
1444                              a.campaign_id campaign_id,
1445 			     c.area2_code schedule_area2,
1446                              a.country_id schedule_country,
1447                              NVL(d.business_unit_id, 0) business_unit_id,
1448                              'MONTH' aggregate_by,
1449                              'Z' display_type,
1450                              b.fiscal_year fiscal_year,
1451                              b.fiscal_qtr fiscal_qtr,
1452                              b.fiscal_month fiscal_month,
1453                              0 curr_count,
1454                              0 pre_count,
1455                              0 curr_started,
1456                              0 pre_started,
1457                              0 curr_ended,
1458                              COUNT(schedule_id) pre_ended
1459                        FROM ams_campaign_schedules_b a,
1460                             bim_dates b,
1461                             jtf_loc_hierarchies_b c,
1462                             ams_campaigns_all_b d
1463                       WHERE a.status_code IN
1464                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
1465                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date))
1466                                BETWEEN b.pre_fiscal_month_start
1467                                    AND(b.fiscal_month_start - 1)
1468                         AND a.country_id = c.location_hierarchy_id
1469                         AND a.campaign_id = d.campaign_id
1470                         AND trunc(a.start_date_time) >= l_min_start_date
1471                         AND b.period_type = 'MONTH'
1472                    GROUP BY a.campaign_id,
1473 		            c.area2_code,
1474                             a.country_id,
1475                             NVL(d.business_unit_id, 0),
1476                             b.fiscal_year,
1477                             b.fiscal_qtr,
1478                             b.fiscal_month),ams_act_access_denorm R
1479 			    where campaign_id=r.object_id
1480 			    and r.object_type='CAMP'
1481          GROUP BY schedule_area2,
1482                   schedule_country,
1483                   business_unit_id,
1484                   resource_id,
1485                   aggregate_by,
1486                   display_type,
1487                   fiscal_year,
1488                   fiscal_qtr,
1489                   fiscal_month
1490 
1491 		  ---------- for admin
1492 				  union all
1493 				  -------------
1494 
1495 				SELECT
1496 	          schedule_area2,
1497                   schedule_country,
1498                   business_unit_id,
1499                   -1 resource_id,
1500                   aggregate_by,
1501                   display_type,
1502                   fiscal_year,
1503                   fiscal_qtr,
1504                   fiscal_month,
1505                   SUM(curr_count) curr_count,
1506                   SUM(pre_count) pre_count,
1507                   SUM(curr_started) curr_started,
1508                   SUM(pre_started) pre_started,
1509                   SUM(curr_ended) curr_ended,
1510                   SUM(pre_ended) pre_ended,
1511                   'BIN' report_type
1512              FROM (
1513 -- FOR THE PERIOD TYPE MONTH
1514                    SELECT    /*+ use_hash(a)    use_hash(b)  use_hash(c) use_hash(d)  */
1515 		            a.campaign_id campaign_id,
1516                              c.area2_code schedule_area2,
1517                              a.country_id schedule_country,
1518                              NVL(d.business_unit_id, 0) business_unit_id,
1519                              'MONTH' aggregate_by,
1520                              'Z' display_type,
1521                              b.fiscal_year fiscal_year,
1522                              b.fiscal_qtr fiscal_qtr,
1523                              b.fiscal_month fiscal_month,
1524                              COUNT(a.schedule_id) curr_count,
1525                              0 pre_count,
1526                              0 curr_started,
1527                              0 pre_started,
1528                              0 curr_ended,
1529                              0 pre_ended
1530                        FROM ams_campaign_schedules_b a,
1531                             bim_dates b,
1532                             jtf_loc_hierarchies_b c,
1533                             ams_campaigns_all_b d
1534                       WHERE a.status_code IN
1535                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
1536                         AND trunc(a.start_date_time) < b.fiscal_month_start
1537                         AND a.country_id = c.location_hierarchy_id
1538                         AND a.campaign_id = d.campaign_id
1539                         AND b.period_type = 'MONTH'
1540                         AND trunc(a.start_date_time) >= l_min_start_date
1541                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date)) >=
1542                                                           b.fiscal_month_start
1543                    GROUP BY a.campaign_id ,c.area2_code,
1544                             a.country_id,
1545                             NVL(d.business_unit_id, 0),
1546 
1547                             b.fiscal_year,
1548                             b.fiscal_qtr,
1549                             b.fiscal_month
1550 ------------------------------------------------------------------------------------------
1551                    UNION ALL
1552 ------------------------------------------------------------------------------------------
1553                    SELECT    /*+ use_hash(a)    use_hash(b)  use_hash(c) use_hash(d) */
1554                              a.campaign_id campaign_id,
1555 			     c.area2_code schedule_area2,
1556                              a.country_id schedule_country,
1557                              NVL(d.business_unit_id, 0) business_unit_id,
1558                              'MONTH' aggregate_by,
1559                              'Z' display_type,
1560                              b.fiscal_year fiscal_year,
1561                              b.fiscal_qtr fiscal_qtr,
1562                              b.fiscal_month fiscal_month,
1563                              0 curr_count,
1564                              COUNT(a.schedule_id) pre_count,
1565                              0 curr_started,
1566                              0 pre_started,
1567                              0 curr_ended,
1568                              0 pre_ended
1569                        FROM ams_campaign_schedules_b a,
1570                             bim_dates b,
1571                             jtf_loc_hierarchies_b c,
1572                             ams_campaigns_all_b d
1573                       WHERE a.status_code IN
1574                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
1575                         AND trunc(a.start_date_time) < b.pre_fiscal_month_start
1576                         AND a.country_id = c.location_hierarchy_id
1577                         AND a.campaign_id = d.campaign_id
1578                         AND trunc(a.start_date_time) >= l_min_start_date
1579                         AND b.period_type = 'MONTH'
1580                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date)) >=
1581                                                       b.pre_fiscal_month_start
1582                    GROUP BY a.campaign_id,c.area2_code,
1583                             a.country_id,
1584                             NVL(d.business_unit_id, 0),
1585                             b.fiscal_year,
1586                             b.fiscal_qtr,
1587                             b.fiscal_month
1588 ------------------------------------------------------------------------------------------
1589                    UNION ALL
1590 ------------------------------------------------------------------------------------------
1591                    SELECT    /*+ use_hash(a)    use_hash(b)  use_hash(c) use_hash(d) */
1592                              a.campaign_id campaign_id,
1593 			     c.area2_code schedule_area2,
1594                              a.country_id schedule_country,
1595                              NVL(d.business_unit_id, 0) business_unit_id,
1596                              'MONTH' aggregate_by,
1597                              'Z' display_type,
1598                              b.fiscal_year fiscal_year,
1599                              b.fiscal_qtr fiscal_qtr,
1600                              b.fiscal_month fiscal_month,
1601                              0 curr_count,
1602                              0 pre_count,
1603                              COUNT(schedule_id) curr_started,
1604                              0 pre_started,
1605                              0 curr_ended,
1606                              0 pre_ended
1607                        FROM ams_campaign_schedules_b a,
1608                             bim_dates b,
1609                             jtf_loc_hierarchies_b c,
1610                             ams_campaigns_all_b d
1611                       WHERE a.status_code IN
1612                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
1613                         AND trunc(a.start_date_time) BETWEEN b.fiscal_month_start
1614                                                   AND b.fiscal_month_end
1615                         AND a.country_id = c.location_hierarchy_id
1616                         AND a.campaign_id = d.campaign_id
1617                         AND trunc(a.start_date_time) >= l_min_start_date
1618                         AND b.period_type = 'MONTH'
1619                    GROUP BY a.campaign_id ,
1620 		          c.area2_code,
1621                             a.country_id,
1622                             NVL(d.business_unit_id, 0),
1623                             b.fiscal_year,
1624                             b.fiscal_qtr,
1625                             b.fiscal_month
1626 ------------------------------------------------------------------------------------------
1627                    UNION ALL
1628 ------------------------------------------------------------------------------------------
1629                    SELECT    /*+ use_hash(a)    use_hash(b)  use_hash(c) use_hash(d) */
1630                              a.campaign_id campaign_id,
1631 			     c.area2_code schedule_area2,
1632                              a.country_id schedule_country,
1633                              NVL(d.business_unit_id, 0) business_unit_id,
1634                              'MONTH' aggregate_by,
1635                              'Z' display_type,
1636                              b.fiscal_year fiscal_year,
1637                              b.fiscal_qtr fiscal_qtr,
1638                              b.fiscal_month fiscal_month,
1639                              0 curr_count,
1640                              0 pre_count,
1641                              0 curr_started,
1642                              COUNT(schedule_id) pre_started,
1643                              0 curr_ended,
1644                              0 pre_ended
1645                        FROM ams_campaign_schedules_b a,
1646                             bim_dates b,
1647                             jtf_loc_hierarchies_b c,
1648                             ams_campaigns_all_b d
1649                       WHERE a.status_code IN
1650                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
1651                         AND trunc(a.start_date_time) BETWEEN b.pre_fiscal_month_start
1652                                                   AND(b.fiscal_month_start - 1
1653                                                      )
1654                         AND a.country_id = c.location_hierarchy_id
1655                         AND a.campaign_id = d.campaign_id
1656                         AND trunc(a.start_date_time) >= l_min_start_date
1657                         AND b.period_type = 'MONTH'
1658                    GROUP BY a.campaign_id ,c.area2_code,
1659                             a.country_id,
1660                             NVL(d.business_unit_id, 0),
1661                             b.fiscal_year,
1662                             b.fiscal_qtr,
1663                             b.fiscal_month
1664 ------------------------------------------------------------------------------------------
1665                    UNION ALL
1666 ------------------------------------------------------------------------------------------
1667                    SELECT    /*+ use_hash(a)    use_hash(b)  use_hash(c) use_hash(d) */
1668                              a.campaign_id campaign_id,
1669 			     c.area2_code schedule_area2,
1670                              a.country_id schedule_country,
1671                              NVL(d.business_unit_id, 0) business_unit_id,
1672                              'MONTH' aggregate_by,
1673                              'Z' display_type,
1674                              b.fiscal_year fiscal_year,
1675                              b.fiscal_qtr fiscal_qtr,
1676                              b.fiscal_month fiscal_month,
1677                              0 curr_count,
1678                              0 pre_count,
1679                              0 curr_started,
1680                              0 pre_started,
1681                              COUNT(schedule_id) curr_ended,
1682                              0 pre_ended
1683                        FROM ams_campaign_schedules_b a,
1684                             bim_dates b,
1685                             jtf_loc_hierarchies_b c,
1686                             ams_campaigns_all_b d
1687                       WHERE a.status_code IN
1688                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
1689                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date))
1690                                BETWEEN b.fiscal_month_start
1691                                    AND b.fiscal_month_end
1692                         AND a.country_id = c.location_hierarchy_id
1693                         AND a.campaign_id = d.campaign_id
1694                         AND trunc(a.start_date_time) >= l_min_start_date
1695                         AND b.period_type = 'MONTH'
1696                    GROUP BY a.campaign_id,
1697 		            c.area2_code,
1698                             a.country_id,
1699                             NVL(d.business_unit_id, 0),
1700                             b.fiscal_year,
1701                             b.fiscal_qtr,
1702                             b.fiscal_month
1703 ------------------------------------------------------------------------------------------
1704                    UNION ALL
1705 ------------------------------------------------------------------------------------------
1706                    SELECT    /*+ use_hash(a)   use_hash(b)  use_hash(c) use_hash(d) */
1707                              a.campaign_id campaign_id,
1708 			     c.area2_code schedule_area2,
1709                              a.country_id schedule_country,
1710                              NVL(d.business_unit_id, 0) business_unit_id,
1711                              'MONTH' aggregate_by,
1712                              'Z' display_type,
1713                              b.fiscal_year fiscal_year,
1714                              b.fiscal_qtr fiscal_qtr,
1715                              b.fiscal_month fiscal_month,
1716                              0 curr_count,
1717                              0 pre_count,
1718                              0 curr_started,
1719                              0 pre_started,
1720                              0 curr_ended,
1721                              COUNT(schedule_id) pre_ended
1722                        FROM ams_campaign_schedules_b a,
1723                             bim_dates b,
1724                             jtf_loc_hierarchies_b c,
1725                             ams_campaigns_all_b d
1726                       WHERE a.status_code IN
1727                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
1728                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date))
1729                                BETWEEN b.pre_fiscal_month_start
1730                                    AND(b.fiscal_month_start - 1)
1731                         AND a.country_id = c.location_hierarchy_id
1732                         AND a.campaign_id = d.campaign_id
1733                         AND trunc(a.start_date_time) >= l_min_start_date
1734                         AND b.period_type = 'MONTH'
1735                    GROUP BY a.campaign_id,
1736 		            c.area2_code,
1737                             a.country_id,
1738                             NVL(d.business_unit_id, 0),
1739                             b.fiscal_year,
1740                             b.fiscal_qtr,
1741                             b.fiscal_month)
1742          GROUP BY schedule_area2,
1743                   schedule_country,
1744                   business_unit_id,
1745                   aggregate_by,
1746                   display_type,
1747                   fiscal_year,
1748                   fiscal_qtr,
1749                   fiscal_month;
1750 
1751 ---------union all admin end
1752 --commit;
1753 -- FOR THE PERIOD TYPE QTR
1754       INSERT INTO  /*+ append parallel(i,5) */
1755                  bim_r_camp_collection i
1756                   (schedule_area2,
1757                    schedule_country,
1758                    business_unit_id,
1759                    resource_id,
1760                    aggregate_by,
1761                    display_type,
1762                    YEAR,
1763                    qtr,
1764                    MONTH,
1765                    current_count_value,
1766                    previous_count_value,
1767                    current_started_value,
1768                    previous_started_value,
1769                    current_ended_value,
1770                    previous_ended_value,
1771                    report_type
1772                   )
1773          SELECT  /* +INDEX_FFS(R,BIM_DBI_U2) */ schedule_area2,
1774                   schedule_country,
1775                   business_unit_id,
1776                   resource_id,
1777                   aggregate_by,
1778                   display_type,
1779                   fiscal_year,
1780                   fiscal_qtr,
1781                   fiscal_month,
1782                   SUM(curr_count) curr_count,
1783                   SUM(pre_count) pre_count,
1784                   SUM(curr_started) curr_started,
1785                   SUM(pre_started) pre_started,
1786                   SUM(curr_ended) curr_ended,
1787                   SUM(pre_ended) pre_ended,
1788                   'BIN' report_type
1789              FROM (SELECT    /*+ use_hash(a)   use_hash(b)  use_hash(c) use_hash(d) */
1790 	                     a.campaign_id campaign_id,
1791                              c.area2_code schedule_area2,
1792                              a.country_id schedule_country,
1793                              NVL(d.business_unit_id, 0) business_unit_id,
1794                              'QTR' aggregate_by,
1795                              'Z' display_type,
1796                              b.fiscal_year fiscal_year,
1797                              b.fiscal_qtr fiscal_qtr,
1798                              'N' fiscal_month,
1799                              COUNT(a.schedule_id) curr_count,
1800                              0 pre_count,
1801                              0 curr_started,
1802                              0 pre_started,
1803                              0 curr_ended,
1804                              0 pre_ended
1805                        FROM ams_campaign_schedules_b a,
1806                             bim_dates b,
1807                             jtf_loc_hierarchies_b c,
1808                             ams_campaigns_all_b d
1809                       WHERE a.status_code IN
1810                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
1811                         AND trunc(a.start_date_time) < b.fiscal_qtr_start
1812                         AND a.country_id = c.location_hierarchy_id
1813                         AND a.campaign_id = d.campaign_id
1814                         AND trunc(a.start_date_time) >= l_min_start_date
1815                         AND b.period_type = 'QUARTER'
1816                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date)) >=
1817                                                             b.fiscal_qtr_start
1818                    GROUP BY a.campaign_id,
1819 		            c.area2_code,
1820                             a.country_id,
1821                             NVL(d.business_unit_id, 0),
1822                             b.fiscal_year,
1823                             b.fiscal_qtr,
1824                             'N'
1825 ------------------------------------------------------------------------------------------
1826                    UNION ALL
1827 ------------------------------------------------------------------------------------------
1828                    SELECT    /*+ use_hash(a)   use_hash(b)  use_hash(c) use_hash(d) */
1829 		             a.campaign_id campaign_id,
1830                              c.area2_code schedule_area2,
1831                              a.country_id schedule_country,
1832                              NVL(d.business_unit_id, 0) business_unit_id,
1833                              'QTR' aggregate_by,
1834                              'Z' display_type,
1835                              b.fiscal_year fiscal_year,
1836                              b.fiscal_qtr fiscal_qtr,
1837                              'N' fiscal_month,
1838                              0 curr_count,
1839                              COUNT(a.schedule_id) pre_count,
1840                              0 curr_started,
1841                              0 pre_started,
1842                              0 curr_ended,
1843                              0 pre_ended
1844                        FROM ams_campaign_schedules_b a,
1845                             bim_dates b,
1846                             jtf_loc_hierarchies_b c,
1847                             ams_campaigns_all_b d
1848                       WHERE a.status_code IN
1849                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
1850                         AND trunc(a.start_date_time) < b.pre_fiscal_qtr_start
1851                         AND a.country_id = c.location_hierarchy_id
1852                         AND a.campaign_id = d.campaign_id
1853                         AND trunc(a.start_date_time) >= l_min_start_date
1854                         AND b.period_type = 'QUARTER'
1855                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date)) >=
1856                                                         b.pre_fiscal_qtr_start
1857                    GROUP BY a.campaign_id ,c.area2_code,
1858                             a.country_id,
1859                             NVL(d.business_unit_id, 0),
1860                             b.fiscal_year,
1861                             b.fiscal_qtr,
1862                             'N'
1863 ------------------------------------------------------------------------------------------
1864                    UNION ALL
1865 ------------------------------------------------------------------------------------------
1866                    SELECT    /*+ use_hash(a)   use_hash(b)  use_hash(c) use_hash(d) */
1867 		             a.campaign_id campaign_id,
1868                              c.area2_code schedule_area2,
1869                              a.country_id schedule_country,
1870                              NVL(d.business_unit_id, 0) business_unit_id,
1871                              'QTR' aggregate_by,
1872                              'Z' display_type,
1873                              b.fiscal_year fiscal_year,
1874                              b.fiscal_qtr fiscal_qtr,
1875                              'N' fiscal_month,
1876                              0 curr_count,
1877                              0 pre_count,
1878                              COUNT(schedule_id) curr_started,
1879                              0 pre_started,
1880                              0 curr_ended,
1881                              0 pre_ended
1882                        FROM ams_campaign_schedules_b a,
1883                             bim_dates b,
1884                             jtf_loc_hierarchies_b c,
1885                             ams_campaigns_all_b d
1886                       WHERE a.status_code IN
1887                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
1888                         AND trunc(a.start_date_time) BETWEEN b.fiscal_qtr_start
1889                                                   AND b.fiscal_qtr_end
1890                         AND a.country_id = c.location_hierarchy_id
1891                         AND a.campaign_id = d.campaign_id
1892                         AND trunc(a.start_date_time) >= l_min_start_date
1893                         AND b.period_type = 'QUARTER'
1894                    GROUP BY a.campaign_id ,
1895 		            c.area2_code,
1896                             a.country_id,
1897                             NVL(d.business_unit_id, 0),
1898                             b.fiscal_year,
1899                             b.fiscal_qtr,
1900                             'N'
1901 ------------------------------------------------------------------------------------------
1902                    UNION ALL
1903 ------------------------------------------------------------------------------------------
1904                    SELECT    /*+ use_hash(a)   use_hash(b)  use_hash(c) use_hash(d) */
1905 		             a.campaign_id campaign_id,
1906                              c.area2_code schedule_area2,
1907                              a.country_id schedule_country,
1908                              NVL(d.business_unit_id, 0) business_unit_id,
1909                              'QTR' aggregate_by,
1910                              'Z' display_type,
1911                              b.fiscal_year fiscal_year,
1912                              b.fiscal_qtr fiscal_qtr,
1913                              'N' fiscal_month,
1914                              0 curr_count,
1915                              0 pre_count,
1916                              0 curr_started,
1917                              COUNT(schedule_id) pre_started,
1918                              0 curr_ended,
1919                              0 pre_ended
1920                        FROM ams_campaign_schedules_b a,
1921                             bim_dates b,
1922                             jtf_loc_hierarchies_b c,
1923                             ams_campaigns_all_b d
1924                       WHERE a.status_code IN
1925                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
1926                         AND trunc(a.start_date_time) BETWEEN b.pre_fiscal_qtr_start
1927                                                   AND(b.fiscal_qtr_start - 1)
1928                         AND a.country_id = c.location_hierarchy_id
1929                         AND a.campaign_id = d.campaign_id
1930                         AND trunc(a.start_date_time) >= l_min_start_date
1931                         AND b.period_type = 'QUARTER'
1932                    GROUP BY a.campaign_id,
1933 		            c.area2_code,
1934                             a.country_id,
1935                             NVL(d.business_unit_id, 0),
1936                             b.fiscal_year,
1937                             b.fiscal_qtr,
1938                             'N'
1939 ------------------------------------------------------------------------------------------
1940                    UNION ALL
1941 ------------------------------------------------------------------------------------------
1942                    SELECT    /*+ use_hash(a)   use_hash(b)  use_hash(c) use_hash(d) */
1943 		             a.campaign_id campaign_id,
1944                              c.area2_code schedule_area2,
1945                              a.country_id schedule_country,
1946                              NVL(d.business_unit_id, 0) business_unit_id,
1947                              'QTR' aggregate_by,
1948                              'Z' display_type,
1949                              b.fiscal_year fiscal_year,
1950                              b.fiscal_qtr fiscal_qtr,
1951                              'N' fiscal_month,
1952                              0 curr_count,
1953                              0 pre_count,
1954                              0 curr_started,
1955                              0 pre_started,
1956                              COUNT(schedule_id) curr_ended,
1957                              0 pre_ended
1958                        FROM ams_campaign_schedules_b a,
1959                             bim_dates b,
1960                             jtf_loc_hierarchies_b c,
1961                             ams_campaigns_all_b d
1962                       WHERE a.status_code IN
1963                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
1964                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date))
1965                                BETWEEN b.fiscal_qtr_start
1966                                    AND b.fiscal_qtr_end
1967                         AND a.country_id = c.location_hierarchy_id
1968                         AND a.campaign_id = d.campaign_id
1969                         AND trunc(a.start_date_time) >= l_min_start_date
1970                         AND b.period_type = 'QUARTER'
1971                    GROUP BY a.campaign_id,
1972 		            c.area2_code,
1973                             a.country_id,
1974                             NVL(d.business_unit_id, 0),
1975                             b.fiscal_year,
1976                             b.fiscal_qtr,
1977                             'N'
1978 ------------------------------------------------------------------------------------------
1979                    UNION ALL
1980 ------------------------------------------------------------------------------------------
1981                    SELECT    /*+ use_hash(a)    use_hash(b)  use_hash(c) use_hash(d) */
1982                              a.campaign_id campaign_id,
1983 			     c.area2_code schedule_area2,
1984                              a.country_id schedule_country,
1985                              NVL(d.business_unit_id, 0) business_unit_id,
1986                              'QTR' aggregate_by,
1987                              'Z' display_type,
1988                              b.fiscal_year fiscal_year,
1989                              b.fiscal_qtr fiscal_qtr,
1990                              'N' fiscal_month,
1991                              0 curr_count,
1992                              0 pre_count,
1993                              0 curr_started,
1994                              0 pre_started,
1995                              0 curr_ended,
1996                              COUNT(schedule_id) pre_ended
1997                        FROM ams_campaign_schedules_b a,
1998                             bim_dates b,
1999                             jtf_loc_hierarchies_b c,
2000                             ams_campaigns_all_b d
2001                       WHERE a.status_code IN
2002                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
2003                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date))
2004                                BETWEEN b.pre_fiscal_qtr_start
2005                                    AND(b.fiscal_qtr_start - 1)
2006                         AND a.country_id = c.location_hierarchy_id
2007                         AND a.campaign_id = d.campaign_id
2008                         AND trunc(a.start_date_time) >= l_min_start_date
2009                         AND b.period_type = 'QUARTER'
2010                    GROUP BY a.campaign_id,
2011 		            c.area2_code,
2012                             a.country_id,
2013                             NVL(d.business_unit_id, 0),
2014                             b.fiscal_year,
2015                             b.fiscal_qtr,
2016                             'N'),ams_act_access_denorm R
2017 			    where campaign_id=r.object_id
2018 			    and r.object_type='CAMP'
2019          GROUP BY schedule_area2,
2020                   schedule_country,
2021                   business_unit_id,
2022                   resource_id,
2023                   aggregate_by,
2024                   display_type,
2025                   fiscal_year,
2026                   fiscal_qtr,
2027                   fiscal_month
2028 
2029 	  ---------- for admin
2030 				  union all
2031 				  -------------
2032 
2033 
2034 				  SELECT   schedule_area2,
2035                   schedule_country,
2036                   business_unit_id,
2037                   -1 resource_id,
2038                   aggregate_by,
2039                   display_type,
2040                   fiscal_year,
2041                   fiscal_qtr,
2042                   fiscal_month,
2043                   SUM(curr_count) curr_count,
2044                   SUM(pre_count) pre_count,
2045                   SUM(curr_started) curr_started,
2046                   SUM(pre_started) pre_started,
2047                   SUM(curr_ended) curr_ended,
2048                   SUM(pre_ended) pre_ended,
2049                   'BIN' report_type
2050              FROM (SELECT    /*+ use_hash(a)   use_hash(b)  use_hash(c) use_hash(d) */
2051 	                     a.campaign_id campaign_id,
2052                              c.area2_code schedule_area2,
2053                              a.country_id schedule_country,
2054                              NVL(d.business_unit_id, 0) business_unit_id,
2055                              'QTR' aggregate_by,
2056                              'Z' display_type,
2057                              b.fiscal_year fiscal_year,
2058                              b.fiscal_qtr fiscal_qtr,
2059                              'N' fiscal_month,
2060                              COUNT(a.schedule_id) curr_count,
2061                              0 pre_count,
2062                              0 curr_started,
2063                              0 pre_started,
2064                              0 curr_ended,
2065                              0 pre_ended
2066                        FROM ams_campaign_schedules_b a,
2067                             bim_dates b,
2068                             jtf_loc_hierarchies_b c,
2069                             ams_campaigns_all_b d
2070                       WHERE a.status_code IN
2071                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
2072                         AND trunc(a.start_date_time) < b.fiscal_qtr_start
2073                         AND a.country_id = c.location_hierarchy_id
2074                         AND a.campaign_id = d.campaign_id
2075                         AND trunc(a.start_date_time) >= l_min_start_date
2076                         AND b.period_type = 'QUARTER'
2077                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date)) >=
2078                                                             b.fiscal_qtr_start
2079                    GROUP BY a.campaign_id,
2080 		            c.area2_code,
2081                             a.country_id,
2082                             NVL(d.business_unit_id, 0),
2083                             b.fiscal_year,
2084                             b.fiscal_qtr,
2085                             'N'
2086 ------------------------------------------------------------------------------------------
2087                    UNION ALL
2088 ------------------------------------------------------------------------------------------
2089                    SELECT    /*+ use_hash(a)   use_hash(b)  use_hash(c) use_hash(d) */
2090 		             a.campaign_id campaign_id,
2091                              c.area2_code schedule_area2,
2092                              a.country_id schedule_country,
2093                              NVL(d.business_unit_id, 0) business_unit_id,
2094                              'QTR' aggregate_by,
2095                              'Z' display_type,
2096                              b.fiscal_year fiscal_year,
2097                              b.fiscal_qtr fiscal_qtr,
2098                              'N' fiscal_month,
2099                              0 curr_count,
2100                              COUNT(a.schedule_id) pre_count,
2101                              0 curr_started,
2102                              0 pre_started,
2103                              0 curr_ended,
2104                              0 pre_ended
2105                        FROM ams_campaign_schedules_b a,
2106                             bim_dates b,
2107                             jtf_loc_hierarchies_b c,
2108                             ams_campaigns_all_b d
2109                       WHERE a.status_code IN
2110                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
2111                         AND trunc(a.start_date_time) < b.pre_fiscal_qtr_start
2112                         AND a.country_id = c.location_hierarchy_id
2113                         AND a.campaign_id = d.campaign_id
2114                         AND trunc(a.start_date_time) >= l_min_start_date
2115                         AND b.period_type = 'QUARTER'
2116                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date)) >=
2117                                                         b.pre_fiscal_qtr_start
2118                    GROUP BY a.campaign_id ,c.area2_code,
2119                             a.country_id,
2120                             NVL(d.business_unit_id, 0),
2121                             b.fiscal_year,
2122                             b.fiscal_qtr,
2123                             'N'
2124 ------------------------------------------------------------------------------------------
2125                    UNION ALL
2126 ------------------------------------------------------------------------------------------
2127                    SELECT    /*+ use_hash(a)   use_hash(b)  use_hash(c) use_hash(d) */
2128 		             a.campaign_id campaign_id,
2129                              c.area2_code schedule_area2,
2130                              a.country_id schedule_country,
2131                              NVL(d.business_unit_id, 0) business_unit_id,
2132                              'QTR' aggregate_by,
2133                              'Z' display_type,
2134                              b.fiscal_year fiscal_year,
2135                              b.fiscal_qtr fiscal_qtr,
2136                              'N' fiscal_month,
2137                              0 curr_count,
2138                              0 pre_count,
2139                              COUNT(schedule_id) curr_started,
2140                              0 pre_started,
2141                              0 curr_ended,
2142                              0 pre_ended
2143                        FROM ams_campaign_schedules_b a,
2144                             bim_dates b,
2145                             jtf_loc_hierarchies_b c,
2146                             ams_campaigns_all_b d
2147                       WHERE a.status_code IN
2148                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
2149                         AND trunc(a.start_date_time) BETWEEN b.fiscal_qtr_start
2150                                                   AND b.fiscal_qtr_end
2151                         AND a.country_id = c.location_hierarchy_id
2152                         AND a.campaign_id = d.campaign_id
2153                         AND trunc(a.start_date_time) >= l_min_start_date
2154                         AND b.period_type = 'QUARTER'
2155                    GROUP BY a.campaign_id ,
2156 		            c.area2_code,
2157                             a.country_id,
2158                             NVL(d.business_unit_id, 0),
2159                             b.fiscal_year,
2160                             b.fiscal_qtr,
2161                             'N'
2162 ------------------------------------------------------------------------------------------
2163                    UNION ALL
2164 ------------------------------------------------------------------------------------------
2165                    SELECT    /*+ use_hash(a)   use_hash(b)  use_hash(c) use_hash(d) */
2166 		             a.campaign_id campaign_id,
2167                              c.area2_code schedule_area2,
2168                              a.country_id schedule_country,
2169                              NVL(d.business_unit_id, 0) business_unit_id,
2170                              'QTR' aggregate_by,
2171                              'Z' display_type,
2172                              b.fiscal_year fiscal_year,
2173                              b.fiscal_qtr fiscal_qtr,
2174                              'N' fiscal_month,
2175                              0 curr_count,
2176                              0 pre_count,
2177                              0 curr_started,
2178                              COUNT(schedule_id) pre_started,
2179                              0 curr_ended,
2180                              0 pre_ended
2181                        FROM ams_campaign_schedules_b a,
2182                             bim_dates b,
2183                             jtf_loc_hierarchies_b c,
2184                             ams_campaigns_all_b d
2185                       WHERE a.status_code IN
2186                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
2187                         AND trunc(a.start_date_time) BETWEEN b.pre_fiscal_qtr_start
2188                                                   AND(b.fiscal_qtr_start - 1)
2189                         AND a.country_id = c.location_hierarchy_id
2190                         AND a.campaign_id = d.campaign_id
2191                         AND trunc(a.start_date_time) >= l_min_start_date
2192                         AND b.period_type = 'QUARTER'
2193                    GROUP BY a.campaign_id,
2194 		            c.area2_code,
2195                             a.country_id,
2196                             NVL(d.business_unit_id, 0),
2197                             b.fiscal_year,
2198                             b.fiscal_qtr,
2199                             'N'
2200 ------------------------------------------------------------------------------------------
2201                    UNION ALL
2202 ------------------------------------------------------------------------------------------
2203                    SELECT    /*+ use_hash(a)   use_hash(b)  use_hash(c) use_hash(d) */
2204 		             a.campaign_id campaign_id,
2205                              c.area2_code schedule_area2,
2206                              a.country_id schedule_country,
2207                              NVL(d.business_unit_id, 0) business_unit_id,
2208                              'QTR' aggregate_by,
2209                              'Z' display_type,
2210                              b.fiscal_year fiscal_year,
2211                              b.fiscal_qtr fiscal_qtr,
2212                              'N' fiscal_month,
2213                              0 curr_count,
2214                              0 pre_count,
2215                              0 curr_started,
2216                              0 pre_started,
2217                              COUNT(schedule_id) curr_ended,
2218                              0 pre_ended
2219                        FROM ams_campaign_schedules_b a,
2220                             bim_dates b,
2221                             jtf_loc_hierarchies_b c,
2222                             ams_campaigns_all_b d
2223                       WHERE a.status_code IN
2224                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
2225                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date))
2226                                BETWEEN b.fiscal_qtr_start
2227                                    AND b.fiscal_qtr_end
2228                         AND a.country_id = c.location_hierarchy_id
2229                         AND a.campaign_id = d.campaign_id
2230                         AND trunc(a.start_date_time) >= l_min_start_date
2231                         AND b.period_type = 'QUARTER'
2232                    GROUP BY a.campaign_id,
2233 		            c.area2_code,
2234                             a.country_id,
2235                             NVL(d.business_unit_id, 0),
2236                             b.fiscal_year,
2237                             b.fiscal_qtr,
2238                             'N'
2239 ------------------------------------------------------------------------------------------
2240                    UNION ALL
2241 ------------------------------------------------------------------------------------------
2242                    SELECT    /*+ use_hash(a)    use_hash(b)  use_hash(c) use_hash(d) */
2243                              a.campaign_id campaign_id,
2244 			     c.area2_code schedule_area2,
2245                              a.country_id schedule_country,
2246                              NVL(d.business_unit_id, 0) business_unit_id,
2247                              'QTR' aggregate_by,
2248                              'Z' display_type,
2249                              b.fiscal_year fiscal_year,
2250                              b.fiscal_qtr fiscal_qtr,
2251                              'N' fiscal_month,
2252                              0 curr_count,
2253                              0 pre_count,
2254                              0 curr_started,
2255                              0 pre_started,
2256                              0 curr_ended,
2257                              COUNT(schedule_id) pre_ended
2258                        FROM ams_campaign_schedules_b a,
2259                             bim_dates b,
2260                             jtf_loc_hierarchies_b c,
2261                             ams_campaigns_all_b d
2262                       WHERE a.status_code IN
2263                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
2264                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date))
2265                                BETWEEN b.pre_fiscal_qtr_start
2266                                    AND(b.fiscal_qtr_start - 1)
2267                         AND a.country_id = c.location_hierarchy_id
2268                         AND a.campaign_id = d.campaign_id
2269                         AND trunc(a.start_date_time) >= l_min_start_date
2270                         AND b.period_type = 'QUARTER'
2271                    GROUP BY a.campaign_id,
2272 		            c.area2_code,
2273                             a.country_id,
2274                             NVL(d.business_unit_id, 0),
2275                             b.fiscal_year,
2276                             b.fiscal_qtr,
2277                             'N')
2278          GROUP BY schedule_area2,
2279                   schedule_country,
2280                   business_unit_id,
2281                   aggregate_by,
2282                   display_type,
2283                   fiscal_year,
2284                   fiscal_qtr,
2285                   fiscal_month;
2286 
2287 
2288 				  ---------union all admin end
2289 --commit;
2290 -- FOR THE PERIOD TYPE YEAR
2291       INSERT INTO  /*+ append parallel(i,5) */
2292               bim_r_camp_collection i
2293                   (schedule_area2,
2294                    schedule_country,
2295                    business_unit_id,
2296                    resource_id,
2297                    aggregate_by,
2298                    display_type,
2299                    YEAR,
2300                    qtr,
2301                    MONTH,
2302                    current_count_value,
2303                    previous_count_value,
2304                    current_started_value,
2305                    previous_started_value,
2306                    current_ended_value,
2307                    previous_ended_value,
2308                    report_type
2309                   )
2310          SELECT  /* +INDEX_FFS(R,BIM_DBI_U2) */
2311 		  schedule_area2,
2312                   schedule_country,
2313                   business_unit_id,
2314                   resource_id,
2315                   aggregate_by,
2316                   display_type,
2317                   fiscal_year,
2318                   fiscal_qtr,
2319                   fiscal_month,
2320                   SUM(curr_count) curr_count,
2321                   SUM(pre_count) pre_count,
2322                   SUM(curr_started) curr_started,
2323                   SUM(pre_started) pre_started,
2324                   SUM(curr_ended) curr_ended,
2325                   SUM(pre_ended) pre_ended,
2326                   'BIN' report_type
2327              FROM (SELECT   /*+ use_hash(a)   use_hash(b)  use_hash(c) use_hash(d) */
2328 	                     a.campaign_id campaign_id,
2329                              c.area2_code schedule_area2,
2330                              a.country_id schedule_country,
2331                              NVL(d.business_unit_id, 0) business_unit_id,
2332                              'YEAR' aggregate_by,
2333                              'Z' display_type,
2334                              b.fiscal_year fiscal_year,
2335                              'N' fiscal_qtr,
2336                              'N' fiscal_month,
2337                              COUNT(a.schedule_id) curr_count,
2338                              0 pre_count,
2339                              0 curr_started,
2340                              0 pre_started,
2341                              0 curr_ended,
2342                              0 pre_ended
2343                        FROM ams_campaign_schedules_b a,
2344                             bim_dates b,
2345                             jtf_loc_hierarchies_b c,
2346                             ams_campaigns_all_b d
2347                       WHERE a.status_code IN
2348                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
2349                         AND trunc(a.start_date_time) < b.fiscal_year_start
2350                         AND a.country_id = c.location_hierarchy_id
2351                         AND a.campaign_id = d.campaign_id
2352                         AND trunc(a.start_date_time) >= l_min_start_date
2353                         AND b.period_type = 'YEAR'
2354                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date)) >=
2355                                                            b.fiscal_year_start
2356                    GROUP BY a.campaign_id,
2357 		            c.area2_code,
2358                             a.country_id,
2359                             NVL(d.business_unit_id, 0),
2360                             b.fiscal_year,
2361                             'N',
2362                             'N'
2363 ------------------------------------------------------------------------------------------
2364                    UNION ALL
2365 ------------------------------------------------------------------------------------------
2366                    SELECT    /*+ use_hash(a)  use_hash(b)  use_hash(c) use_hash(d) */
2367 		             a.campaign_id campaign_id,
2368                              c.area2_code schedule_area2,
2369                              a.country_id schedule_country,
2370                              NVL(d.business_unit_id, 0) business_unit_id,
2371                              'YEAR' aggregate_by,
2372                              'Z' display_type,
2373                              b.fiscal_year fiscal_year,
2374                              'N' fiscal_qtr,
2375                              'N' fiscal_month,
2376                              0 curr_count,
2377                              COUNT(a.schedule_id) pre_count,
2378                              0 curr_started,
2379                              0 pre_started,
2380                              0 curr_ended,
2381                              0 pre_ended
2382                        FROM ams_campaign_schedules_b a,
2383                             bim_dates b,
2384                             jtf_loc_hierarchies_b c,
2385                             ams_campaigns_all_b d
2386                       WHERE a.status_code IN
2387                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
2388                         AND trunc(a.start_date_time) < b.pre_fiscal_year_start
2389                         AND a.country_id = c.location_hierarchy_id
2390                         AND a.campaign_id = d.campaign_id
2391                         AND trunc(a.start_date_time) >= l_min_start_date
2392                         AND b.period_type = 'YEAR'
2393                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date)) >=
2394                                                        b.pre_fiscal_year_start
2395                    GROUP BY a.campaign_id,c.area2_code,
2396                             a.country_id,
2397                             NVL(d.business_unit_id, 0),
2398                             b.fiscal_year,
2399                             'N',
2400                             'N'
2401 ------------------------------------------------------------------------------------------
2402                    UNION ALL
2403 ------------------------------------------------------------------------------------------
2404                    SELECT    /*+ use_hash(a)  use_hash(b)  use_hash(c) use_hash(d) */
2405 	                     a.campaign_id campaign_id,
2406                              c.area2_code schedule_area2,
2407                              a.country_id schedule_country,
2408                              NVL(d.business_unit_id, 0) business_unit_id,
2409                              'YEAR' aggregate_by,
2410                              'Z' display_type,
2411                              b.fiscal_year fiscal_year,
2412                              'N' fiscal_qtr,
2413                              'N' fiscal_month,
2414                              0 curr_count,
2415                              0 pre_count,
2416                              COUNT(schedule_id) curr_started,
2417                              0 pre_started,
2418                              0 curr_ended,
2419                              0 pre_ended
2420                        FROM ams_campaign_schedules_b a,
2421                             bim_dates b,
2422                             jtf_loc_hierarchies_b c,
2423                             ams_campaigns_all_b d
2424                       WHERE a.status_code IN
2425                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
2426                         AND trunc(a.start_date_time) BETWEEN b.fiscal_year_start
2427                                                   AND b.fiscal_year_end
2428                         AND a.country_id = c.location_hierarchy_id
2429                         AND a.campaign_id = d.campaign_id
2430                         AND trunc(a.start_date_time) >= l_min_start_date
2431                         AND b.period_type = 'YEAR'
2432                    GROUP BY a.campaign_id,
2433 		            c.area2_code,
2434                             a.country_id,
2435                             NVL(d.business_unit_id, 0),
2436                             b.fiscal_year,
2437                             'N',
2438                             'N'
2439 ------------------------------------------------------------------------------------------
2440                    UNION ALL
2441 ------------------------------------------------------------------------------------------
2442                    SELECT    /*+ use_hash(a)  use_hash(b)  use_hash(c) use_hash(d) */
2443 		             a.campaign_id campaign_id,
2444                              c.area2_code schedule_area2,
2445                              a.country_id schedule_country,
2446                              NVL(d.business_unit_id, 0) business_unit_id,
2447                              'YEAR' aggregate_by,
2448                              'Z' display_type,
2449                              b.fiscal_year fiscal_year,
2450                              'N' fiscal_qtr,
2451                              'N' fiscal_month,
2452                              0 curr_count,
2453                              0 pre_count,
2454                              0 curr_started,
2455                              COUNT(schedule_id) pre_started,
2456                              0 curr_ended,
2457                              0 pre_ended
2458                        FROM ams_campaign_schedules_b a,
2459                             bim_dates b,
2460                             jtf_loc_hierarchies_b c,
2461                             ams_campaigns_all_b d
2462                       WHERE a.status_code IN
2463                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
2464                         AND trunc(a.start_date_time) BETWEEN b.pre_fiscal_year_start
2465                                                   AND(b.fiscal_year_start - 1
2466                                                      )
2467                         AND a.country_id = c.location_hierarchy_id
2468                         AND a.campaign_id = d.campaign_id
2469                         AND trunc(a.start_date_time) >= l_min_start_date
2470                         AND b.period_type = 'YEAR'
2471                    GROUP BY a.campaign_id ,
2472 		           c.area2_code,
2473                             a.country_id,
2474                             NVL(d.business_unit_id, 0),
2475                             b.fiscal_year,
2476                             'N',
2477                             'N'
2478 ------------------------------------------------------------------------------------------
2479                    UNION ALL
2480 ------------------------------------------------------------------------------------------
2481                    SELECT    /*+ use_hash(a)  use_hash(b)  use_hash(c) use_hash(d) */
2482                              a.campaign_id campaign_id,
2483 			     c.area2_code schedule_area2,
2484                              a.country_id schedule_country,
2485                              NVL(d.business_unit_id, 0) business_unit_id,
2486                              'YEAR' aggregate_by,
2487                              'Z' display_type,
2488                              b.fiscal_year fiscal_year,
2489                              'N' fiscal_qtr,
2490                              'N' fiscal_month,
2491                              0 curr_count,
2492                              0 pre_count,
2493                              0 curr_started,
2494                              0 pre_started,
2495                              COUNT(schedule_id) curr_ended,
2496                              0 pre_ended
2497                        FROM ams_campaign_schedules_b a,
2498                             bim_dates b,
2499                             jtf_loc_hierarchies_b c,
2500                             ams_campaigns_all_b d
2501                       WHERE a.status_code IN
2502                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
2503                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date))
2504                                BETWEEN b.fiscal_year_start
2505                                    AND b.fiscal_year_end
2506                         AND a.country_id = c.location_hierarchy_id
2507                         AND a.campaign_id = d.campaign_id
2508                         AND trunc(a.start_date_time) >= l_min_start_date
2509                         AND b.period_type = 'YEAR'
2510                    GROUP BY a.campaign_id,
2511 		            c.area2_code,
2512                             a.country_id,
2513                             NVL(d.business_unit_id, 0),
2514                             b.fiscal_year,
2515                             'N',
2516                             'N'
2517 ------------------------------------------------------------------------------------------
2518                    UNION ALL
2519 ------------------------------------------------------------------------------------------
2520                    SELECT   /*+ use_hash(a)  use_hash(b)  use_hash(c) use_hash(d) */
2521                             a.campaign_id campaign_id,
2522 			    c.area2_code schedule_area2,
2523                              a.country_id schedule_country,
2524                              NVL(d.business_unit_id, 0) business_unit_id,
2525                              'YEAR' aggregate_by,
2526                              'Z' display_type,
2527                              b.fiscal_year fiscal_year,
2528                              'N' fiscal_qtr,
2529                              'N' fiscal_month,
2530                              0 curr_count,
2531                              0 pre_count,
2532                              0 curr_started,
2533                              0 pre_started,
2534                              0 curr_ended,
2535                              COUNT(schedule_id) pre_ended
2536                        FROM ams_campaign_schedules_b a,
2537                             bim_dates b,
2538                             jtf_loc_hierarchies_b c,
2539                             ams_campaigns_all_b d
2540                       WHERE a.status_code IN
2541                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
2542                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date))
2543                                BETWEEN b.pre_fiscal_year_start
2544                                    AND(b.fiscal_year_start - 1)
2545                         AND a.country_id = c.location_hierarchy_id
2546                         AND a.campaign_id = d.campaign_id
2547                         AND trunc(a.start_date_time) >= l_min_start_date
2548                         AND b.period_type = 'YEAR'
2549                    GROUP BY a.campaign_id ,
2550 		            c.area2_code,
2551                             a.country_id,
2552                             NVL(d.business_unit_id, 0),
2553                             b.fiscal_year,
2554                             'N',
2555                             'N'),ams_act_access_denorm R
2556 			    where campaign_id=r.object_id
2557 			    and r.object_type='CAMP'
2558          GROUP BY schedule_area2,
2559                   schedule_country,
2560                   business_unit_id,
2561                   resource_id,
2562                   aggregate_by,
2563                   display_type,
2564                   fiscal_year,
2565                   fiscal_qtr,
2566                   fiscal_month
2567 
2568 	  ---------- for admin
2569 				  union all
2570 				  -------------
2571 
2572 				  SELECT
2573 		  schedule_area2,
2574                   schedule_country,
2575                   business_unit_id,
2576                   -1 resource_id,
2577                   aggregate_by,
2578                   display_type,
2579                   fiscal_year,
2580                   fiscal_qtr,
2581                   fiscal_month,
2582                   SUM(curr_count) curr_count,
2583                   SUM(pre_count) pre_count,
2584                   SUM(curr_started) curr_started,
2585                   SUM(pre_started) pre_started,
2586                   SUM(curr_ended) curr_ended,
2587                   SUM(pre_ended) pre_ended,
2588                   'BIN' report_type
2589              FROM (SELECT   /*+ use_hash(a)   use_hash(b)  use_hash(c) use_hash(d) */
2590 	                     a.campaign_id campaign_id,
2591                              c.area2_code schedule_area2,
2592                              a.country_id schedule_country,
2593                              NVL(d.business_unit_id, 0) business_unit_id,
2594                              'YEAR' aggregate_by,
2595                              'Z' display_type,
2596                              b.fiscal_year fiscal_year,
2597                              'N' fiscal_qtr,
2598                              'N' fiscal_month,
2599                              COUNT(a.schedule_id) curr_count,
2600                              0 pre_count,
2601                              0 curr_started,
2602                              0 pre_started,
2603                              0 curr_ended,
2604                              0 pre_ended
2605                        FROM ams_campaign_schedules_b a,
2606                             bim_dates b,
2607                             jtf_loc_hierarchies_b c,
2608                             ams_campaigns_all_b d
2609                       WHERE a.status_code IN
2610                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
2611                         AND trunc(a.start_date_time) < b.fiscal_year_start
2612                         AND a.country_id = c.location_hierarchy_id
2613                         AND a.campaign_id = d.campaign_id
2614                         AND trunc(a.start_date_time) >= l_min_start_date
2615                         AND b.period_type = 'YEAR'
2616                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date)) >=
2617                                                            b.fiscal_year_start
2618                    GROUP BY a.campaign_id,
2619 		            c.area2_code,
2620                             a.country_id,
2621                             NVL(d.business_unit_id, 0),
2622                             b.fiscal_year,
2623                             'N',
2624                             'N'
2625 ------------------------------------------------------------------------------------------
2626                    UNION ALL
2627 ------------------------------------------------------------------------------------------
2628                    SELECT    /*+ use_hash(a)  use_hash(b)  use_hash(c) use_hash(d) */
2629 		             a.campaign_id campaign_id,
2630                              c.area2_code schedule_area2,
2631                              a.country_id schedule_country,
2632                              NVL(d.business_unit_id, 0) business_unit_id,
2633                              'YEAR' aggregate_by,
2634                              'Z' display_type,
2635                              b.fiscal_year fiscal_year,
2636                              'N' fiscal_qtr,
2637                              'N' fiscal_month,
2638                              0 curr_count,
2639                              COUNT(a.schedule_id) pre_count,
2640                              0 curr_started,
2641                              0 pre_started,
2642                              0 curr_ended,
2643                              0 pre_ended
2644                        FROM ams_campaign_schedules_b a,
2645                             bim_dates b,
2646                             jtf_loc_hierarchies_b c,
2647                             ams_campaigns_all_b d
2648                       WHERE a.status_code IN
2649                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
2650                         AND trunc(a.start_date_time) < b.pre_fiscal_year_start
2651                         AND a.country_id = c.location_hierarchy_id
2652                         AND a.campaign_id = d.campaign_id
2653                         AND trunc(a.start_date_time) >= l_min_start_date
2654                         AND b.period_type = 'YEAR'
2655                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date)) >=
2656                                                        b.pre_fiscal_year_start
2657                    GROUP BY a.campaign_id,c.area2_code,
2658                             a.country_id,
2659                             NVL(d.business_unit_id, 0),
2660                             b.fiscal_year,
2661                             'N',
2662                             'N'
2663 ------------------------------------------------------------------------------------------
2664                    UNION ALL
2665 ------------------------------------------------------------------------------------------
2666                    SELECT    /*+ use_hash(a)  use_hash(b)  use_hash(c) use_hash(d) */
2667 	                     a.campaign_id campaign_id,
2668                              c.area2_code schedule_area2,
2669                              a.country_id schedule_country,
2670                              NVL(d.business_unit_id, 0) business_unit_id,
2671                              'YEAR' aggregate_by,
2672                              'Z' display_type,
2673                              b.fiscal_year fiscal_year,
2674                              'N' fiscal_qtr,
2675                              'N' fiscal_month,
2676                              0 curr_count,
2677                              0 pre_count,
2678                              COUNT(schedule_id) curr_started,
2679                              0 pre_started,
2680                              0 curr_ended,
2681                              0 pre_ended
2682                        FROM ams_campaign_schedules_b a,
2683                             bim_dates b,
2684                             jtf_loc_hierarchies_b c,
2685                             ams_campaigns_all_b d
2686                       WHERE a.status_code IN
2687                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
2688                         AND trunc(a.start_date_time) BETWEEN b.fiscal_year_start
2689                                                   AND b.fiscal_year_end
2690                         AND a.country_id = c.location_hierarchy_id
2691                         AND a.campaign_id = d.campaign_id
2692                         AND trunc(a.start_date_time) >= l_min_start_date
2693                         AND b.period_type = 'YEAR'
2694                    GROUP BY a.campaign_id,
2695 		            c.area2_code,
2696                             a.country_id,
2697                             NVL(d.business_unit_id, 0),
2698                             b.fiscal_year,
2699                             'N',
2700                             'N'
2701 ------------------------------------------------------------------------------------------
2702                    UNION ALL
2703 ------------------------------------------------------------------------------------------
2704                    SELECT    /*+ use_hash(a)  use_hash(b)  use_hash(c) use_hash(d) */
2705 		             a.campaign_id campaign_id,
2706                              c.area2_code schedule_area2,
2707                              a.country_id schedule_country,
2708                              NVL(d.business_unit_id, 0) business_unit_id,
2709                              'YEAR' aggregate_by,
2710                              'Z' display_type,
2711                              b.fiscal_year fiscal_year,
2712                              'N' fiscal_qtr,
2713                              'N' fiscal_month,
2714                              0 curr_count,
2715                              0 pre_count,
2716                              0 curr_started,
2717                              COUNT(schedule_id) pre_started,
2718                              0 curr_ended,
2719                              0 pre_ended
2720                        FROM ams_campaign_schedules_b a,
2721                             bim_dates b,
2722                             jtf_loc_hierarchies_b c,
2723                             ams_campaigns_all_b d
2724                       WHERE a.status_code IN
2725                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
2726                         AND trunc(a.start_date_time) BETWEEN b.pre_fiscal_year_start
2727                                                   AND(b.fiscal_year_start - 1
2728                                                      )
2729                         AND a.country_id = c.location_hierarchy_id
2730                         AND a.campaign_id = d.campaign_id
2731                         AND trunc(a.start_date_time) >= l_min_start_date
2732                         AND b.period_type = 'YEAR'
2733                    GROUP BY a.campaign_id ,
2734 		           c.area2_code,
2735                             a.country_id,
2736                             NVL(d.business_unit_id, 0),
2737                             b.fiscal_year,
2738                             'N',
2739                             'N'
2740 ------------------------------------------------------------------------------------------
2741                    UNION ALL
2742 ------------------------------------------------------------------------------------------
2743                    SELECT    /*+ use_hash(a)  use_hash(b)  use_hash(c) use_hash(d) */
2744                              a.campaign_id campaign_id,
2745 			     c.area2_code schedule_area2,
2746                              a.country_id schedule_country,
2747                              NVL(d.business_unit_id, 0) business_unit_id,
2748                              'YEAR' aggregate_by,
2749                              'Z' display_type,
2750                              b.fiscal_year fiscal_year,
2751                              'N' fiscal_qtr,
2752                              'N' fiscal_month,
2753                              0 curr_count,
2754                              0 pre_count,
2755                              0 curr_started,
2756                              0 pre_started,
2757                              COUNT(schedule_id) curr_ended,
2758                              0 pre_ended
2759                        FROM ams_campaign_schedules_b a,
2760                             bim_dates b,
2761                             jtf_loc_hierarchies_b c,
2762                             ams_campaigns_all_b d
2763                       WHERE a.status_code IN
2764                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
2765                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date))
2766                                BETWEEN b.fiscal_year_start
2767                                    AND b.fiscal_year_end
2768                         AND a.country_id = c.location_hierarchy_id
2769                         AND a.campaign_id = d.campaign_id
2770                         AND trunc(a.start_date_time) >= l_min_start_date
2771                         AND b.period_type = 'YEAR'
2772                    GROUP BY a.campaign_id,
2773 		            c.area2_code,
2774                             a.country_id,
2775                             NVL(d.business_unit_id, 0),
2776                             b.fiscal_year,
2777                             'N',
2778                             'N'
2779 ------------------------------------------------------------------------------------------
2780                    UNION ALL
2781 ------------------------------------------------------------------------------------------
2782                    SELECT   /*+ use_hash(a)  use_hash(b)  use_hash(c) use_hash(d) */
2783                             a.campaign_id campaign_id,
2784 			    c.area2_code schedule_area2,
2785                              a.country_id schedule_country,
2786                              NVL(d.business_unit_id, 0) business_unit_id,
2787                              'YEAR' aggregate_by,
2788                              'Z' display_type,
2789                              b.fiscal_year fiscal_year,
2790                              'N' fiscal_qtr,
2791                              'N' fiscal_month,
2792                              0 curr_count,
2793                              0 pre_count,
2794                              0 curr_started,
2795                              0 pre_started,
2796                              0 curr_ended,
2797                              COUNT(schedule_id) pre_ended
2798                        FROM ams_campaign_schedules_b a,
2799                             bim_dates b,
2800                             jtf_loc_hierarchies_b c,
2801                             ams_campaigns_all_b d
2802                       WHERE a.status_code IN
2803                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
2804                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date))
2805                                BETWEEN b.pre_fiscal_year_start
2806                                    AND(b.fiscal_year_start - 1)
2807                         AND a.country_id = c.location_hierarchy_id
2808                         AND a.campaign_id = d.campaign_id
2809                         AND trunc(a.start_date_time) >= l_min_start_date
2810                         AND b.period_type = 'YEAR'
2811                    GROUP BY a.campaign_id ,
2812 		            c.area2_code,
2813                             a.country_id,
2814                             NVL(d.business_unit_id, 0),
2815                             b.fiscal_year,
2816                             'N',
2817                             'N')
2818          GROUP BY schedule_area2,
2819                   schedule_country,
2820                   business_unit_id,
2821                   aggregate_by,
2822                   display_type,
2823                   fiscal_year,
2824                   fiscal_qtr,
2825                   fiscal_month;
2826 
2827 
2828 
2829 				  ---------union all admin end
2830       ams_utility_pvt.write_conc_log('BIM_CAMP_COLLECTION: Q/M/Y COUNT UPDATE END');
2831 /* END OF THE MONTH, QUARTER ,YEAR DATA INSERT */
2832    EXCEPTION
2833       WHEN OTHERS
2834       THEN
2835          ams_utility_pvt.write_conc_log(   'BIM_CAMP_COLLECTION:IN OTHERS EXCEPTION '
2836                                         || SQLERRM(SQLCODE));
2837          --dbms_utility.FORMAT_ERROR_STACK );
2838    END get_increment_mqy_data;
2839 -------------------------------------------------------------------------------------------
2840 ---
2841 --PROCEDURE TO GET THE SUMMARY DATA
2842 ---
2843 ------------------------------------------------------------------------------------------
2844    PROCEDURE get_summary_data
2845    IS
2846       l_org_id                       NUMBER;
2847       l_fiscal_month                 VARCHAR2(10);
2848       l_fiscal_month_end             DATE;
2849       l_fiscal_month_start           DATE;
2850       l_pre_fiscal_month_end         DATE;
2851       l_pre_fiscal_month_start       DATE;
2852       l_pre_pre_fiscal_month_start   DATE;
2853       l_fiscal_qtr                   VARCHAR2(10);
2854       l_fiscal_qtr_end               DATE;
2855       l_fiscal_qtr_start             DATE;
2856       l_pre_fiscal_qtr_end           DATE;
2857       l_pre_fiscal_qtr_start         DATE;
2858       l_fiscal_roll_year_start       DATE;
2859       l_fiscal_year                  VARCHAR2(10);
2860       l_fiscal_year_end              DATE;
2861       l_fiscal_year_start            DATE;
2862       l_pre_fiscal_year_end          DATE;
2863       l_pre_fiscal_year_start        DATE;
2864       --l_pre_fiscal_roll_year_end    DATE;
2865       l_pre_fiscal_roll_year_start   DATE;
2866       l_min_start_date               DATE;
2867       l_sysdate  date := trunc(sysdate);--'02-SEP-2000';
2868       sysdate_1  date := TRUNC(SYSDATE- 1);
2869       CURSOR chk_history_data
2870       IS
2871          SELECT MIN(start_date)
2872            FROM bim_rep_history
2873           WHERE OBJECT = 'CAMPAIGN';
2874    BEGIN
2875       OPEN chk_history_data;
2876       FETCH chk_history_data INTO l_min_start_date;
2877       CLOSE chk_history_data;
2878       l_org_id := 0;
2879       l_fiscal_month := bim_set_of_books.get_fiscal_month(l_SYSDATE - 1, l_org_id);
2880       l_fiscal_month_end := bim_set_of_books.get_fiscal_month_end(l_SYSDATE - 1, l_org_id);
2881       l_fiscal_month_start := bim_set_of_books.get_fiscal_month_start(l_SYSDATE - 1, l_org_id);
2882       l_pre_fiscal_month_end := bim_set_of_books.get_fiscal_month_end(l_fiscal_month_start - 1,l_org_id);
2883       l_pre_fiscal_month_start :=bim_set_of_books.get_pre_fiscal_month_start(l_SYSDATE - 1, l_org_id);
2884       l_fiscal_qtr := bim_set_of_books.get_fiscal_qtr(l_SYSDATE - 1, l_org_id);
2885       l_fiscal_qtr_end :=bim_set_of_books.get_fiscal_qtr_end(l_SYSDATE - 1, l_org_id);
2886       l_fiscal_qtr_start :=bim_set_of_books.get_fiscal_qtr_start(l_SYSDATE - 1, l_org_id);
2887       l_pre_fiscal_qtr_end :=bim_set_of_books.get_fiscal_qtr_end(l_fiscal_qtr_start - 1, l_org_id);
2888       l_pre_fiscal_qtr_start :=bim_set_of_books.get_pre_fiscal_qtr_start(l_SYSDATE - 1, l_org_id);
2889       l_fiscal_roll_year_start :=bim_set_of_books.get_fiscal_roll_year_start(l_SYSDATE - 1, l_org_id);
2890       l_fiscal_year := bim_set_of_books.get_fiscal_year(l_SYSDATE - 1, l_org_id);
2891       l_fiscal_year_end :=bim_set_of_books.get_fiscal_year_end(l_SYSDATE - 1, l_org_id);
2892       l_fiscal_year_start :=bim_set_of_books.get_fiscal_year_start(l_SYSDATE - 1, l_org_id);
2893       l_pre_fiscal_roll_year_start :=bim_set_of_books.get_pre_fiscal_roll_year_start(l_SYSDATE - 1,l_org_id);
2894 --commit;
2895       --EXECUTE IMMEDIATE 'TRUNCATE TABLE bim_r_camp_collection';
2896 /* START OF THE INCREMENT DATA FIRST INSERT */
2897       ams_utility_pvt.write_conc_log('BIM_CAMP_COLLECTION: INCREMENT FIRST INSERT START');
2898       INSERT INTO  /*+ append parallel(i,5) */
2899               bim_r_camp_collection i
2900                   (campaign_area1,
2901                    campaign_area2,
2902                    schedule_area1,
2903                    schedule_area2,
2904                    campaign_country,
2905                    schedule_country,
2906                    business_unit_id,
2907                    resource_id,
2908                    campaign_type,
2909                    schedule_activity,
2910                    schedule_activity_type,
2911                    campaign_status,
2912                    schedule_status,
2913                    aggregate_by,
2914                    display_type,
2915                    YEAR,
2916                    qtr,
2917                    MONTH,
2918                    report_type,
2919                    current_count_value,
2920                    current_started_value,
2921                    current_ended_value,
2922                    previous_count_value,
2923                    previous_started_value,
2924                    previous_ended_value,
2925                    campaign_count,
2926                    forecasted_responses,
2927                    targeted_customers,
2928                    prev_targeted_customers,
2929                    budget_approved
2930                   )
2931          SELECT  /* +INDEX_FFS(R,BIM_DBI_U2) */
2932 	          'N' campaign_area1,
2933                   'N' campaign_area2,
2934                   'N' schedule_area1,
2935                   schedule_area2,
2936                   'N' campaign_country,
2937                   schedule_country,
2938                   business_unit_id,
2939                   resource_id,
2940                   'N' campaign_type,
2941                   schedule_activity_id,
2942                   'N' schedule_activity_type,
2943                   'N' campaign_status,
2944                   'N' schedule_status,
2945                   aggregate_by,
2946                   display_type,
2947                   'N' YEAR,
2948                   'N' qtr,
2949                   'N' MONTH,
2950                   'ACT' report_type,
2951                   SUM(curr_count_value) current_count_value,
2952                   SUM(curr_started_value) current_started_value,
2953                   SUM(curr_ended_value) current_ended_value,
2954                   SUM(prev_count_value) previous_count_value,
2955                   SUM(prev_started_value) previous_started_value,
2956                   SUM(prev_ended_value) previous_ended_value,
2957                   SUM(campaign_count) campaign_count,
2958                   SUM(forecasted_responses) forecasted_responses,
2959                   SUM(targeted_customers) targeted_customers,
2960                   SUM(prev_targeted_customers) prev_targeted_customers,
2961                   SUM(budget_approved) budget_approved
2962              FROM (SELECT  /*+ use_hash(A)  use_hash(B) use_hash(C)  */
2963 			   a.campaign_id campaign_id,
2964 			   c.area2_code schedule_area2,
2965                             a.country_id schedule_country,
2966                             NVL(b.business_unit_id, 0) business_unit_id,
2967                             DECODE(a.activity_type_code,
2968                                    'EVENTS', -9999,
2969                                    a.activity_id
2970                                   ) schedule_activity_id,
2971                             'INCREMENT' aggregate_by,
2972                             'DAY' display_type,
2973                             SUM(DECODE(TRUNC(start_date_time),
2974                                        TRUNC(SYSDATE - 1), 0,
2975                                        DECODE(LEAST(TRUNC(start_date_time),
2976                                                     TRUNC(SYSDATE - 1)),
2977                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
2978                                                                                       TRUNC(  SYSDATE
2979                                                                                             - 1)),
2980                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
2981                                                                              0
2982                                                                             ),
2983                                               0
2984                                              )
2985                                       )) + SUM(DECODE(TRUNC(start_date_time),
2986                                        TRUNC(SYSDATE - 1), 1,
2987                                        0
2988                                       )) curr_count_value,
2989 	--*** added curr_started_value to curr_count_value
2990 /*
2991                             SUM(DECODE(TRUNC(start_date_time),
2992                                        TRUNC(SYSDATE - 1), 1,
2993                                        0
2994                                       )) curr_started_value,
2995                             SUM(DECODE(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
2996                                        TRUNC(SYSDATE - 1), 1,
2997                                        0
2998                                       )) curr_ended_value,
2999 */
3000                             SUM(DECODE(TRUNC(start_date_time),
3001                                        TRUNC(SYSDATE - 2), 0,
3002                                        DECODE(LEAST(TRUNC(start_date_time),
3003                                                     TRUNC(SYSDATE - 2)),
3004                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
3005                                                                                       TRUNC(  SYSDATE
3006                                                                                             - 2)),
3007                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
3008                                                                              0
3009                                                                             ),
3010                                               0
3011                                              )
3012                                       )) +  SUM(DECODE(TRUNC(start_date_time),
3013                                        TRUNC(SYSDATE - 2), 1,
3014                                        0
3015                                       )) prev_count_value,
3016 
3017 --*** added prev_started_value to prev_count_value
3018 /*
3019                             SUM(DECODE(TRUNC(start_date_time),
3020                                        TRUNC(SYSDATE - 2), 1,
3021                                        0
3022                                       )) prev_started_value,
3023                             SUM(DECODE(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
3024                                        TRUNC(SYSDATE - 2), 1,
3025                                        0
3026                                       )) prev_ended_value,
3027 */
3028                             0 curr_started_value,
3029 			    0 curr_ended_value,
3030 			    0 prev_started_value,
3031                             0 prev_ended_value,
3032                             0 campaign_count,
3033                             0 forecasted_responses,
3034                             0 targeted_customers,
3035                             0 prev_targeted_customers,
3036                             0 budget_approved
3037                        FROM ams_campaign_schedules_b a,
3038                             jtf_loc_hierarchies_b c,
3039                             ams_campaigns_all_b b
3040                       WHERE a.status_code IN
3041                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
3042                         AND trunc(a.start_date_time) >= l_min_start_date
3043 			and trunc(a.start_date_time) <= sysdate_1
3044                         AND a.country_id = c.location_hierarchy_id
3045                         AND a.campaign_id = b.campaign_id
3046 			and trunc(NVL(a.end_date_time, b.actual_exec_end_date)) >=SYSDATE - 2
3047                    GROUP BY a.campaign_id,c.area2_code,
3048                             a.country_id,
3049                             NVL(b.business_unit_id, 0),
3050                             DECODE(a.activity_type_code,
3051                                    'EVENTS', -9999,
3052                                    a.activity_id
3053                                   )
3054 ------------------------------------------------------------------------------------------------
3055                    UNION ALL
3056 ------------------------------------------------------------------------------------------------
3057                    SELECT  /*+ use_hash(A)   use_hash(B) use_hash(C)  */
3058 			   a.campaign_id campaign_id,
3059 			   c.area2_code schedule_area2,
3060                             a.country_id schedule_country,
3061                             NVL(b.business_unit_id, 0) business_unit_id,
3062                             DECODE(a.activity_type_code,
3063                                    'EVENTS', -9999,
3064                                    a.activity_id
3065                                   ) schedule_activity_id,
3066                             'INCREMENT' aggregate_by,
3067                             'WEEK' display_type,
3068                             SUM(DECODE(TRUNC(start_date_time),
3069                                        TRUNC(SYSDATE - 8), 0,
3070                                        DECODE(LEAST(TRUNC(start_date_time),
3071                                                     TRUNC(SYSDATE - 8)),
3072                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
3073                                                                                       TRUNC(  SYSDATE
3074                                                                                             - 8)),
3075                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
3076                                                                              0
3077                                                                             ),
3078                                               0
3079                                              )
3080                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date_time),
3081                                                 TRUNC(SYSDATE - 8)),
3082                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
3083                                                                             TRUNC(  SYSDATE
3084                                                                                   - 1)),
3085                                                                       TRUNC(start_date_time), 1,
3086                                                                       0
3087                                                                      ),
3088                                        0
3089                                       )) curr_count_value,
3090 --*** started value is added to count value
3091 
3092  /*                           SUM(DECODE(GREATEST(TRUNC(start_date_time),
3093                                                 TRUNC(SYSDATE - 8)),
3094                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
3095                                                                             TRUNC(  SYSDATE
3096                                                                                   - 1)),
3097                                                                       TRUNC(start_date_time), 1,
3098                                                                       0
3099                                                                      ),
3100                                        0
3101                                       )) curr_started_value,
3102                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
3103                                                 TRUNC(SYSDATE - 8)),
3104                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
3105                                                                             TRUNC(  SYSDATE
3106                                                                                   - 1)),
3107                                                                       trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
3108                                                                       0
3109                                                                      ),
3110                                        0
3111                                       )) curr_ended_value,
3112 */
3113 
3114 			    SUM(DECODE(TRUNC(start_date_time),
3115                                        TRUNC(SYSDATE - 15), 0,
3116                                        DECODE(LEAST(TRUNC(start_date_time),
3117                                                     TRUNC(SYSDATE - 15)),
3118                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
3119                                                                                       TRUNC(  SYSDATE
3120                                                                                             - 15)),
3121                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
3122                                                                              0
3123                                                                             ),
3124                                               0
3125                                              )
3126                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date_time),
3127                                                 TRUNC(SYSDATE - 15)),
3128                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
3129                                                                             TRUNC(  SYSDATE
3130                                                                                   - 8)),
3131                                                                       TRUNC(start_date_time), 1,
3132                                                                       0
3133                                                                      ),
3134                                        0
3135                                       )) prev_count_value,
3136 --***
3137 /*
3138                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
3139                                                 TRUNC(SYSDATE - 15)),
3140                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
3141                                                                             TRUNC(  SYSDATE
3142                                                                                   - 8)),
3143                                                                       TRUNC(start_date_time), 1,
3144                                                                       0
3145                                                                      ),
3146                                        0
3147                                       )) prev_started_value,
3148                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
3149                                                 TRUNC(SYSDATE - 15)),
3150                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
3151                                                                           TRUNC(  SYSDATE
3152                                                                                 - 8)),
3153                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
3154                                                                     0
3155                                                                    ),
3156                                        0
3157                                       )) prev_ended_value,
3158 */
3159 			    0 curr_started_value,
3160 			    0 curr_ended_value,
3161 			    0 prev_started_value,
3162                             0 prev_ended_value,
3163                             0 campaign_count,
3164                             0 forecasted_responses,
3165                             0 targeted_customers,
3166                             0 prev_targeted_customers,
3167                             0 budget_approved
3168                        FROM ams_campaign_schedules_b a,
3169                             jtf_loc_hierarchies_b c,
3170                             ams_campaigns_all_b b
3171                       WHERE a.status_code IN
3172                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
3173                         AND trunc(a.start_date_time) >= l_min_start_date
3174 			and trunc(a.start_date_time) <= sysdate_1
3175                         AND a.country_id = c.location_hierarchy_id
3176                         AND a.campaign_id = b.campaign_id
3177 			and trunc(NVL(a.end_date_time, b.actual_exec_end_date)) >=SYSDATE - 15
3178                    GROUP BY a.campaign_id,
3179 		            c.area1_code,
3180                             c.area2_code,
3181                             a.country_id,
3182                             NVL(b.business_unit_id, 0),
3183                             DECODE(a.activity_type_code,
3184                                    'EVENTS', -9999,
3185                                    a.activity_id
3186                                   )
3187 ------------------------------------------------------------------------------------------------
3188                    UNION ALL
3189 ------------------------------------------------------------------------------------------------
3190                    SELECT  /*+ use_hash(A)  use_hash(B) use_hash(C)  */
3191 			   a.campaign_id campaign_id,
3192 			   c.area2_code schedule_area2,
3193                             a.country_id schedule_country,
3194                             NVL(b.business_unit_id, 0) business_unit_id,
3195                             DECODE(a.activity_type_code,
3196                                    'EVENTS', -9999,
3197                                    a.activity_id
3198                                   ) schedule_activity_id,
3199                             'INCREMENT' aggregate_by,
3200                             'MONTH' display_type,
3201                             SUM(DECODE(TRUNC(start_date_time),
3202                                        l_fiscal_month_start, 0,
3203                                        DECODE(LEAST(TRUNC(start_date_time),
3204                                                     l_fiscal_month_start),
3205                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
3206                                                                                       l_fiscal_month_start),
3207                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
3208                                                                              0
3209                                                                             ),
3210                                               0
3211                                              )
3212                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date_time),
3213                                                 l_fiscal_month_start),
3214                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
3215                                                                               SYSDATE
3216                                                                             - 1),
3217                                                                       TRUNC(start_date_time), 1,
3218                                                                       0
3219                                                                      ),
3220                                        0
3221                                       )) curr_count_value,
3222 --***
3223 /*
3224 
3225                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
3226                                                 l_fiscal_month_start),
3227                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
3228                                                                               SYSDATE
3229                                                                             - 1),
3230                                                                       TRUNC(start_date_time), 1,
3231                                                                       0
3232                                                                      ),
3233                                        0
3234                                       )) curr_started_value,
3235                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
3236                                                 l_fiscal_month_start),
3237                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
3238                                                                             SYSDATE
3239                                                                           - 1),
3240                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
3241                                                                     0
3242                                                                    ),
3243                                        0
3244                                       )) curr_ended_value,
3245 */
3246 
3247                             SUM(DECODE(TRUNC(start_date_time),
3248                                        l_pre_fiscal_month_start, 0,
3249                                        DECODE(LEAST(TRUNC(start_date_time),
3250                                                     l_pre_fiscal_month_start),
3251                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
3252                                                                                       l_pre_fiscal_month_start),
3253                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
3254                                                                              0
3255                                                                             ),
3256                                               0
3257                                              )
3258                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date_time),
3259                                                 l_pre_fiscal_month_start),
3260                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
3261                                                                             l_pre_fiscal_month_end),
3262                                                                       TRUNC(start_date_time), 1,
3263                                                                       0
3264                                                                      ),
3265                                        0
3266                                       ))  prev_count_value,
3267 
3268 --***
3269 /*
3270                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
3271                                                 l_pre_fiscal_month_start),
3272                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
3273                                                                             l_pre_fiscal_month_end),
3274                                                                       TRUNC(start_date_time), 1,
3275                                                                       0
3276                                                                      ),
3277                                        0
3278                                       )) prev_started_value,
3279                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
3280                                                 l_pre_fiscal_month_start),
3281                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
3282                                                                           l_pre_fiscal_month_end),
3283                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
3284                                                                     0
3285                                                                    ),
3286                                        0
3287                                       )) prev_ended_value,
3288 */
3289 			    0 curr_started_value,
3290 			    0 curr_ended_value,
3291 			    0 prev_started_value,
3292                             0 prev_ended_value,
3293                             0 campaign_count,
3294                             0 forecasted_responses,
3295                             0 targeted_customers,
3296                             0 prev_targeted_customers,
3297                             0 budget_approved
3298                        FROM ams_campaign_schedules_b a,
3299                             jtf_loc_hierarchies_b c,
3300                             ams_campaigns_all_b b
3301                       WHERE a.status_code IN
3302                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
3303                         AND trunc(a.start_date_time) >= l_min_start_date
3304 			and trunc(a.start_date_time) <= sysdate_1
3305                         AND a.country_id = c.location_hierarchy_id
3306                         AND a.campaign_id = b.campaign_id
3307 			and trunc(NVL(a.end_date_time, b.actual_exec_end_date)) >=l_pre_fiscal_month_start
3308                    GROUP BY a.campaign_id ,
3309 		            c.area2_code,
3310                             a.country_id,
3311                             NVL(b.business_unit_id, 0),
3312                             DECODE(a.activity_type_code,
3313                                    'EVENTS', -9999,
3314                                    a.activity_id
3315                                   )
3316 ------------------------------------------------------------------------------------------------
3317                    UNION ALL
3318 ------------------------------------------------------------------------------------------------
3319                    SELECT   /*+ use_hash(A)  use_hash(B) use_hash(C)  */
3320 			    a.campaign_id campaign_id,
3321 			    c.area2_code schedule_area2,
3322                             a.country_id schedule_country,
3323                             NVL(b.business_unit_id, 0) business_unit_id,
3324                             DECODE(a.activity_type_code,
3325                                    'EVENTS', -9999,
3326                                    a.activity_id
3327                                   ) schedule_activity_id,
3328                             'INCREMENT' aggregate_by,
3329                             'QUARTER' display_type,
3330                             SUM(DECODE(TRUNC(start_date_time),
3331                                        l_fiscal_qtr_start, 0,
3332                                        DECODE(LEAST(TRUNC(start_date_time),
3333                                                     l_fiscal_qtr_start),
3334                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
3335                                                                                       l_fiscal_qtr_start),
3336                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
3337                                                                              0
3338                                                                             ),
3339                                               0
3340                                              )
3341                                       )) +  SUM(DECODE(GREATEST(TRUNC(start_date_time),
3342                                                 l_fiscal_qtr_start),
3343                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
3344                                                                               SYSDATE
3345                                                                             - 1),
3346                                                                       TRUNC(start_date_time), 1,
3347                                                                       0
3348                                                                      ),
3349                                        0
3350                                       ))  curr_count_value,
3351 --***
3352 /*
3353                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
3354                                                 l_fiscal_qtr_start),
3355                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
3356                                                                               SYSDATE
3357                                                                             - 1),
3358                                                                       TRUNC(start_date_time), 1,
3359                                                                       0
3360                                                                      ),
3361                                        0
3362                                       )) curr_started_value,
3363                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
3364                                                 l_fiscal_qtr_start),
3365                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
3366                                                                             SYSDATE
3367                                                                           - 1),
3368                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
3369                                                                     0
3370                                                                    ),
3371                                        0
3372                                       )) curr_ended_value,
3373 */
3374 
3375                             SUM(DECODE(TRUNC(start_date_time),
3376                                        l_pre_fiscal_qtr_start, 0,
3377                                        DECODE(LEAST(TRUNC(start_date_time),
3378                                                     l_pre_fiscal_qtr_start),
3379                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
3380                                                                                       l_pre_fiscal_qtr_start),
3381                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
3382                                                                              0
3383                                                                             ),
3384                                               0
3385                                              )
3386                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date_time),
3387                                                 l_pre_fiscal_qtr_start),
3388                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
3389                                                                             l_pre_fiscal_qtr_end),
3390                                                                       TRUNC(start_date_time), 1,
3391                                                                       0
3392                                                                      ),
3393                                        0
3394                                       )) prev_count_value,
3395 --***
3396 /*
3397 
3398                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
3399                                                 l_pre_fiscal_qtr_start),
3400                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
3401                                                                             l_pre_fiscal_qtr_end),
3402                                                                       TRUNC(start_date_time), 1,
3403                                                                       0
3404                                                                      ),
3405                                        0
3406                                       )) prev_started_value,
3407                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
3408                                                 l_pre_fiscal_qtr_start),
3409                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
3410                                                                           l_pre_fiscal_qtr_end),
3411                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
3412                                                                     0
3413                                                                    ),
3414                                        0
3415                                       )) prev_ended_value,
3416 */
3417 
3418 			    0 curr_started_value,
3419 			    0 curr_ended_value,
3420 			    0 prev_started_value,
3421                             0 prev_ended_value,
3422                             0 campaign_count,
3423                             0 forecasted_responses,
3424                             0 targeted_customers,
3425                             0 prev_targeted_customers,
3426                             0 budget_approved
3427                        FROM ams_campaign_schedules_b a,
3428                             jtf_loc_hierarchies_b c,
3429                             ams_campaigns_all_b b
3430                       WHERE a.status_code IN
3431                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
3432                         AND trunc(a.start_date_time) >= l_min_start_date
3433 			and trunc(a.start_date_time) <= sysdate_1
3434                         AND a.country_id = c.location_hierarchy_id
3435                         AND a.campaign_id = b.campaign_id
3436 			and trunc(NVL(a.end_date_time, b.actual_exec_end_date)) >=l_pre_fiscal_qtr_start
3437                    GROUP BY a.campaign_id ,c.area2_code,
3438                             a.country_id,
3439                             NVL(b.business_unit_id, 0),
3440                             DECODE(a.activity_type_code,
3441                                    'EVENTS', -9999,
3442                                    a.activity_id
3443                                   )
3444 ------------------------------------------------------------------------------------------------
3445                    UNION ALL
3446 ------------------------------------------------------------------------------------------------
3447                    SELECT  /*+ use_hash(A)  use_hash(B) use_hash(C)  */
3448 			    a.campaign_id ,
3449 			    c.area2_code schedule_area2,
3450                             a.country_id schedule_country,
3451                             NVL(b.business_unit_id, 0) business_unit_id,
3452                             DECODE(a.activity_type_code,
3453                                    'EVENTS', -9999,
3454                                    a.activity_id
3455                                   ) schedule_activity_id,
3456                             'INCREMENT' aggregate_by,
3457                             'YEAR' display_type,
3458                             SUM(DECODE(TRUNC(start_date_time),
3459                                        l_fiscal_roll_year_start, 0,
3460                                        DECODE(LEAST(TRUNC(start_date_time),
3461                                                     l_fiscal_roll_year_start),
3462                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
3463                                                                                       l_fiscal_roll_year_start),
3464                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
3465                                                                              0
3466                                                                             ),
3467                                               0
3468                                              )
3469                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date_time),
3470                                                 l_fiscal_roll_year_start),
3471                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
3472                                                                               SYSDATE
3473                                                                             - 1),
3474                                                                       TRUNC(start_date_time), 1,
3475                                                                       0
3476                                                                      ),
3477                                        0
3478                                       )) curr_count_value,
3479 --***
3480 /*
3481                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
3482                                                 l_fiscal_roll_year_start),
3483                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
3484                                                                               SYSDATE
3485                                                                             - 1),
3486                                                                       TRUNC(start_date_time), 1,
3487                                                                       0
3488                                                                      ),
3489                                        0
3490                                       )) curr_started_value,
3491                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
3492                                                 l_fiscal_roll_year_start),
3493                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
3494                                                                             SYSDATE
3495                                                                           - 1),
3496                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
3497                                                                     0
3498                                                                    ),
3499                                        0
3500                                       )) curr_ended_value,
3501 */
3502                             SUM(DECODE(TRUNC(start_date_time),
3503                                        l_pre_fiscal_roll_year_start, 0,
3504                                        DECODE(LEAST(TRUNC(start_date_time),
3505                                                     l_pre_fiscal_roll_year_start),
3506                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
3507                                                                                       l_pre_fiscal_roll_year_start),
3508                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
3509                                                                              0
3510                                                                             ),
3511                                               0
3512                                              )
3513                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date_time),
3514                                                 l_pre_fiscal_roll_year_start),
3515                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
3516                                                                               l_fiscal_roll_year_start
3517                                                                             - 1),
3518                                                                       TRUNC(start_date_time), 1,
3519                                                                       0
3520                                                                      ),
3521                                        0
3522                                       )) prev_count_value,
3523 --***
3524 /*
3525                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
3526                                                 l_pre_fiscal_roll_year_start),
3527                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
3528                                                                               l_fiscal_roll_year_start
3529                                                                             - 1),
3530                                                                       TRUNC(start_date_time), 1,
3531                                                                       0
3532                                                                      ),
3533                                        0
3534                                       )) prev_started_value,
3535                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
3536                                                 l_pre_fiscal_roll_year_start),
3537                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
3538                                                                             l_fiscal_roll_year_start
3539                                                                           - 1),
3540                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
3541                                                                     0
3542                                                                    ),
3543                                        0
3544                                       )) prev_ended_value,
3545 */
3546 
3547 			    0 curr_started_value,
3548 			    0 curr_ended_value,
3549 			    0 prev_started_value,
3550                             0 prev_ended_value,
3551 			    0 campaign_count,
3552                             0 forecasted_responses,
3553                             0 targeted_customers,
3554                             0 prev_targeted_customers,
3555                             0 budget_approved
3556                        FROM ams_campaign_schedules_b a,
3557                             jtf_loc_hierarchies_b c,
3558                             ams_campaigns_all_b b
3559                       WHERE a.status_code IN
3560                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
3561                         AND trunc(a.start_date_time) >= l_min_start_date
3562 			and trunc(a.start_date_time) <= sysdate_1
3563                         AND a.country_id = c.location_hierarchy_id
3564                         AND a.campaign_id = b.campaign_id
3565 			and trunc(NVL(a.end_date_time, b.actual_exec_end_date)) >=l_pre_fiscal_roll_year_start
3566                    GROUP BY a.campaign_id ,
3567 		            c.area2_code,
3568                             a.country_id,
3569                             NVL(b.business_unit_id, 0),
3570                             DECODE(a.activity_type_code,
3571                                    'EVENTS', -9999,
3572                                    a.activity_id
3573                                   )
3574 -------------------------------------------------------------------------------------
3575 --START OF CAMPAIGN COUNT CODE
3576 ------------------------------------------------------------------------------------------------
3577                    UNION ALL
3578 ------------------------------------------------------------------------------------------------
3579                    SELECT  /*+ use_hash(A)   use_hash(B) use_hash(C)  */
3580 		            a.campaign_id campaign_id,
3581 			    c.area2_code schedule_area2,
3582                             a.country_id schedule_country,
3583                             NVL(b.business_unit_id, 0) business_unit_id,
3584                             DECODE(a.activity_type_code,
3585                                    'EVENTS', -9999,
3586                                    a.activity_id
3587                                   ) schedule_activity_id,
3588                             'INCREMENT' aggregate_by,
3589                             'DAY' display_type,
3590                             0 curr_count_value,
3591                             0 curr_started_value,
3592                             0 curr_ended_value,
3593                             0 prev_count_value,
3594                             0 prev_started_value,
3595                             0 prev_ended_value,
3596                             COUNT(DISTINCT a.campaign_id) campaign_count,
3597                             0 forecasted_responses,
3598                             0 targeted_customers,
3599                             0 prev_targeted_customers,
3600                             0 budget_approved
3601                        FROM ams_campaign_schedules_b a,
3602                             jtf_loc_hierarchies_b c,
3603                             ams_campaigns_all_b b
3604                       WHERE a.status_code IN
3605                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
3606 --***  <= will get both active and started campaigns for DAY diaplay_type
3607                         AND trunc(a.start_date_time) <= TRUNC(SYSDATE - 1)
3608                         AND a.country_id = c.location_hierarchy_id
3609                         AND a.campaign_id = b.campaign_id
3610                         AND trunc(a.start_date_time) >= l_min_start_date
3611                         AND trunc(NVL(a.end_date_time, b.actual_exec_end_date)) >=
3612                                                             TRUNC(SYSDATE - 1)
3613 
3614                    GROUP BY a.campaign_id ,
3615 		            c.area2_code,
3616                             a.country_id,
3617                             NVL(b.business_unit_id, 0),
3618                             DECODE(a.activity_type_code,
3619                                    'EVENTS', -9999,
3620                                    a.activity_id
3621                                   )
3622 ------------------------------------------------------------------------------------------------
3623                    UNION ALL
3624 ------------------------------------------------------------------------------------------------
3625                    SELECT  /*+ use_hash(A)  use_hash(B) use_hash(C)  */
3626 		            a.campaign_id campaign_id,
3627 		            c.area2_code schedule_area2,
3628                             a.country_id schedule_country,
3629                             NVL(b.business_unit_id, 0) business_unit_id,
3630                             DECODE(a.activity_type_code,
3631                                    'EVENTS', -9999,
3632                                    a.activity_id
3633                                   ) schedule_activity_id,
3634                             'INCREMENT' aggregate_by,
3635                             'WEEK' display_type,
3636                             0 curr_count_value,
3637                             0 curr_started_value,
3638                             0 curr_ended_value,
3639                             0 prev_count_value,
3640                             0 prev_started_value,
3641                             0 prev_ended_value,
3642                             COUNT(DISTINCT a.campaign_id) campaign_count,
3643                             0 forecasted_responses,
3644                             0 targeted_customers,
3645                             0 prev_targeted_customers,
3646                             0 budget_approved
3647                        FROM ams_campaign_schedules_b a,
3648                             jtf_loc_hierarchies_b c,
3649                             ams_campaigns_all_b b
3650                       WHERE a.status_code IN
3651                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
3652 --***
3653                         AND (trunc(a.start_date_time) < TRUNC(SYSDATE - 8)
3654 			     or
3655                              trunc(a.start_date_time) between TRUNC(SYSDATE - 8) and TRUNC(  SYSDATE
3656                                                                                   - 1)
3657                              )
3658                         AND a.country_id = c.location_hierarchy_id
3659                         AND a.campaign_id = b.campaign_id
3660                         AND trunc(a.start_date_time) >= l_min_start_date
3661 			and trunc(a.start_date_time) <= sysdate_1
3662                         AND trunc(NVL(a.end_date_time, b.actual_exec_end_date)) >=
3663                                                             TRUNC(SYSDATE - 8)
3664 
3665                    GROUP BY a.campaign_id ,c.area2_code,
3666                             a.country_id,
3667                             NVL(b.business_unit_id, 0),
3668                             DECODE(a.activity_type_code,
3669                                    'EVENTS', -9999,
3670                                    a.activity_id
3671                                   )
3672 ------------------------------------------------------------------------------------------------
3673                    UNION ALL
3674 ------------------------------------------------------------------------------------------------
3675                    SELECT   /*+ use_hash(A)  use_hash(B) use_hash(C)  */
3676 		           a.campaign_id campaign_id,
3677 		            c.area2_code schedule_area2,
3678                             a.country_id schedule_country,
3679                             NVL(b.business_unit_id, 0) business_unit_id,
3680                             DECODE(a.activity_type_code,
3681                                    'EVENTS', -9999,
3682                                    a.activity_id
3683                                   ) schedule_activity_id,
3684                             'INCREMENT' aggregate_by,
3685                             'MONTH' display_type,
3686                             0 curr_count_value,
3687                             0 curr_started_value,
3688                             0 curr_ended_value,
3689                             0 prev_count_value,
3690                             0 prev_started_value,
3691                             0 prev_ended_value,
3692                             COUNT(DISTINCT a.campaign_id) campaign_count,
3693                             0 forecasted_responses,
3694                             0 targeted_customers,
3695                             0 prev_targeted_customers,
3696                             0 budget_approved
3697                        FROM ams_campaign_schedules_b a,
3698                             jtf_loc_hierarchies_b c,
3699                             ams_campaigns_all_b b
3700                       WHERE a.status_code IN
3701                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
3702 --***
3703                         AND (trunc(a.start_date_time) < l_fiscal_month_start
3704 			     or
3705                              trunc(a.start_date_time) between l_fiscal_month_start and TRUNC(  SYSDATE
3706                                                                                   - 1)
3707                              )
3708                         AND a.country_id = c.location_hierarchy_id
3709                         AND a.campaign_id = b.campaign_id
3710                         AND trunc(a.start_date_time) >= l_min_start_date
3711 			and trunc(a.start_date_time) <= sysdate_1
3712                         AND trunc(NVL(a.end_date_time, b.actual_exec_end_date)) >=
3713                                                           l_fiscal_month_start
3714                    GROUP BY a.campaign_id ,c.area2_code,
3715                             a.country_id,
3716                             NVL(b.business_unit_id, 0),
3717                             DECODE(a.activity_type_code,
3718                                    'EVENTS', -9999,
3719                                    a.activity_id
3720                                   )
3721 ------------------------------------------------------------------------------------------------
3722                    UNION ALL
3723 ------------------------------------------------------------------------------------------------
3724                    SELECT   /*+ use_hash(A)  use_hash(B) use_hash(C)  */
3725 		            a.campaign_id campaign_id,
3726 		            c.area2_code schedule_area2,
3727                             a.country_id schedule_country,
3728                             NVL(b.business_unit_id, 0) business_unit_id,
3729                             DECODE(a.activity_type_code,
3730                                    'EVENTS', -9999,
3731                                    a.activity_id
3732                                   ) schedule_activity_id,
3733                             'INCREMENT' aggregate_by,
3734                             'QUARTER' display_type,
3735                             0 curr_count_value,
3736                             0 curr_started_value,
3737                             0 curr_ended_value,
3738                             0 prev_count_value,
3739                             0 prev_started_value,
3740                             0 prev_ended_value,
3741                             COUNT(DISTINCT a.campaign_id) campaign_count,
3742                             0 forecasted_responses,
3743                             0 targeted_customers,
3744                             0 prev_targeted_customers,
3745                             0 budget_approved
3746                        FROM ams_campaign_schedules_b a,
3747                             jtf_loc_hierarchies_b c,
3748                             ams_campaigns_all_b b
3749                       WHERE a.status_code IN
3750                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
3751 --***
3752                         AND (trunc(a.start_date_time) < l_fiscal_qtr_start
3753 			     or
3754                              trunc(a.start_date_time) between l_fiscal_qtr_start and TRUNC(  SYSDATE
3755                                                                                   - 1)
3756                               )
3757                         AND a.country_id = c.location_hierarchy_id
3758                         AND a.campaign_id = b.campaign_id
3759                         AND trunc(a.start_date_time) >= l_min_start_date
3760 			and trunc(a.start_date_time) <= sysdate_1
3761                         AND trunc(NVL(a.end_date_time, b.actual_exec_end_date)) >=
3762                                                             l_fiscal_qtr_start
3763                    GROUP BY a.campaign_id,
3764 		            c.area2_code,
3765                             a.country_id,
3766                             NVL(b.business_unit_id, 0),
3767                             DECODE(a.activity_type_code,
3768                                    'EVENTS', -9999,
3769                                    a.activity_id
3770                                   )
3771 ------------------------------------------------------------------------------------------------
3772                    UNION ALL
3773 ------------------------------------------------------------------------------------------------
3774                    SELECT   /*+ use_hash(A)  use_hash(B) use_hash(C)  */
3775 		            a.campaign_id campaign_id,
3776 			    c.area2_code schedule_area2,
3777                             a.country_id schedule_country,
3778                             NVL(b.business_unit_id, 0) business_unit_id,
3779                             DECODE(a.activity_type_code,
3780                                    'EVENTS', -9999,
3781                                    a.activity_id
3782                                   ) schedule_activity_id,
3783                             'INCREMENT' aggregate_by,
3784                             'YEAR' display_type,
3785                             0 curr_count_value,
3786                             0 curr_started_value,
3787                             0 curr_ended_value,
3788                             0 prev_count_value,
3789                             0 prev_started_value,
3790                             0 prev_ended_value,
3791                             COUNT(DISTINCT a.campaign_id) campaign_count,
3792                             0 forecasted_responses,
3793                             0 targeted_customers,
3794                             0 prev_targeted_customers,
3795                             0 budget_approved
3796                        FROM ams_campaign_schedules_b a,
3797                             jtf_loc_hierarchies_b c,
3798                             ams_campaigns_all_b b
3799                       WHERE a.status_code IN
3800                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
3801 --***
3802                         AND (trunc(a.start_date_time) < l_fiscal_roll_year_start
3803 			     or
3804                              trunc(a.start_date_time) between l_fiscal_roll_year_start and TRUNC(  SYSDATE
3805                                                                                   - 1)
3806                              )
3807                         AND a.country_id = c.location_hierarchy_id
3808                         AND a.campaign_id = b.campaign_id
3809                         AND trunc(a.start_date_time) >= l_min_start_date
3810 			and trunc(a.start_date_time) <= sysdate_1
3811                         AND trunc(NVL(a.end_date_time, b.actual_exec_end_date)) >=
3812                                                       l_fiscal_roll_year_start
3813                    GROUP BY a.campaign_id ,
3814 		            c.area2_code,
3815                             a.country_id,
3816                             NVL(b.business_unit_id, 0),
3817                             DECODE(a.activity_type_code,
3818                                    'EVENTS', -9999,
3819                                    a.activity_id
3820                                   )
3821 ------------------------------------------------------------------------------------------------
3822 ---START OF METRICS CODE FOR FRESP, TCUST, BAPPROVED
3823 ------------------------------------------------------------------------------------------------
3824 ------------------------------------------------------------------------------------------------
3825                    UNION ALL
3826 ------------------------------------------------------------------------------------------------
3827                    SELECT   /*+ use_hash(A)  use_hash(C)  */
3828 		            a.campaign_id campaign_id,
3829 			    c.area2_code schedule_area2,
3830                             a.schedule_country schedule_country,
3831                             NVL(a.business_unit_id, 0) business_unit_id,
3832                             a.schedule_activity_id schedule_activity_id,
3833                             'INCREMENT' aggregate_by,
3834                             'DAY' display_type,
3835                             0 curr_count_value,
3836                             0 curr_started_value,
3837                             0 curr_ended_value,
3838                             0 prev_count_value,
3839                             0 prev_started_value,
3840                             0 prev_ended_value,
3841                             0 campaign_count,
3842                             SUM(DECODE(TRUNC(start_date),
3843                                        TRUNC(SYSDATE - 1), 0,
3844                                        DECODE(LEAST(TRUNC(start_date),
3845                                                     TRUNC(SYSDATE - 1)),
3846                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
3847                                                                                  TRUNC(  SYSDATE
3848                                                                                        - 1)),
3849                                                                         TRUNC(end_date), forecasted_responses,
3850                                                                         0
3851                                                                        ),
3852                                               0
3853                                              )
3854                                       )) + SUM(DECODE(TRUNC(start_date),
3855                                        TRUNC(SYSDATE - 1), forecasted_responses,
3856                                        0
3857                                       )) forecasted_responses,
3858                             SUM(DECODE(TRUNC(start_date),
3859                                        TRUNC(SYSDATE - 1), 0,
3860                                        DECODE(LEAST(TRUNC(start_date),
3861                                                     TRUNC(SYSDATE - 1)),
3862                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
3863                                                                                  TRUNC(  SYSDATE
3864                                                                                        - 1)),
3865                                                                         TRUNC(end_date), targeted_customers,
3866                                                                         0
3867                                                                        ),
3868                                               0
3869                                              )
3870                                       )) + SUM(DECODE(TRUNC(start_date),
3871                                        TRUNC(SYSDATE - 1), targeted_customers,
3872                                        0
3873                                       )) targeted_customers,
3874 
3875 					SUM(DECODE(TRUNC(start_date),
3876                                        TRUNC(SYSDATE - 2), 0,
3877                                        DECODE(LEAST(TRUNC(start_date),
3878                                                     TRUNC(SYSDATE - 2)),
3879                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
3880                                                                                  TRUNC(  SYSDATE
3881                                                                                        - 2)),
3882                                                                         TRUNC(end_date), targeted_customers,
3883                                                                         0
3884                                                                        ),
3885                                               0
3886                                              )
3887                                       )) +    SUM(DECODE(TRUNC(start_date),
3888                                        TRUNC(SYSDATE - 2), targeted_customers,
3889                                        0
3890                                       ))  prev_targeted_customers,
3891                             SUM(DECODE(TRUNC(start_date),
3892                                        TRUNC(SYSDATE - 1), 0,
3893                                        DECODE(LEAST(TRUNC(start_date),
3894                                                     TRUNC(SYSDATE - 1)),
3895                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
3896                                                                                  TRUNC(  SYSDATE
3897                                                                                        - 1)),
3898                                                                         TRUNC(end_date), budget_approved,
3899                                                                         0
3900                                                                        ),
3901                                               0
3902                                              )
3903                                       )) + SUM(DECODE(TRUNC(start_date),
3904                                        TRUNC(SYSDATE - 1), budget_approved,
3905                                        0
3906                                       )) budget_approved
3907                        FROM bim_r_camp_daily_facts a,
3908                             jtf_loc_hierarchies_b c
3909                       WHERE a.schedule_country = c.location_hierarchy_id
3910 		           and trunc(a.start_date) <=  sysdate_1
3911 			   and trunc(a.end_date) >= SYSDATE - 2
3912                    GROUP BY a.campaign_id,
3913 		            c.area2_code,
3914                             a.schedule_country,
3915                             NVL(a.business_unit_id, 0),
3916                             a.schedule_activity_id
3917 ------------------------------------------------------------------------------------------------
3918                    UNION ALL
3919 ------------------------------------------------------------------------------------------------
3920                    SELECT   /*+ use_hash(A)  use_hash(C)  */
3921 		            a.campaign_id campaign_id,
3922 		            c.area2_code schedule_area2,
3923                             a.schedule_country schedule_country,
3924                             NVL(a.business_unit_id, 0) business_unit_id,
3925                             a.schedule_activity_id schedule_activity_id,
3926                             'INCREMENT' aggregate_by,
3927                             'WEEK' display_type,
3928                             0 curr_count_value,
3929                             0 curr_started_value,
3930                             0 curr_ended_value,
3931                             0 prev_count_value,
3932                             0 prev_started_value,
3933                             0 prev_ended_value,
3934                             0 campaign_count,
3935                             SUM(DECODE(TRUNC(start_date),
3936                                        TRUNC(SYSDATE - 8), 0,
3937                                        DECODE(LEAST(TRUNC(start_date),
3938                                                     TRUNC(SYSDATE - 8)),
3939                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
3940                                                                                  TRUNC(  SYSDATE
3941                                                                                        - 8)),
3942                                                                         TRUNC(end_date), forecasted_responses,
3943                                                                         0
3944                                                                        ),
3945                                               0
3946                                              )
3947                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date),
3948                                                 TRUNC(SYSDATE - 8)),
3949                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
3950                                                                             TRUNC(  SYSDATE
3951                                                                                   - 1)),
3952                                                                       TRUNC(start_date), forecasted_responses,
3953                                                                       0
3954                                                                      )))  forecasted_responses,
3955 
3956                             SUM(DECODE(TRUNC(start_date),
3957                                        TRUNC(SYSDATE - 8), 0,
3958                                        DECODE(LEAST(TRUNC(start_date),
3959                                                     TRUNC(SYSDATE - 8)),
3960                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
3961                                                                                  TRUNC(  SYSDATE
3962                                                                                        - 8)),
3963                                                                         TRUNC(end_date), targeted_customers,
3964                                                                         0
3965                                                                        ),
3966                                               0
3967                                              )
3968                                       ))+ SUM(DECODE(GREATEST(TRUNC(start_date),
3969                                                 TRUNC(SYSDATE - 8)),
3970                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
3971                                                                             TRUNC(  SYSDATE
3972                                                                                   - 1)),
3973                                                                       TRUNC(start_date), targeted_customers,
3974                                                                       0
3975                                                                      ))) targeted_customers,
3976                             SUM(DECODE(TRUNC(start_date),
3977                                        TRUNC(SYSDATE - 15), 0,
3978                                        DECODE(LEAST(TRUNC(start_date),
3979                                                     TRUNC(SYSDATE - 15)),
3980                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
3981                                                                                  TRUNC(  SYSDATE
3982                                                                                        - 15)),
3983                                                                         TRUNC(end_date), targeted_customers,
3984                                                                         0
3985                                                                        ),
3986                                               0
3987                                              )
3988                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date),
3989                                                 TRUNC(SYSDATE - 15)),
3990                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
3991                                                                             TRUNC(  SYSDATE
3992                                                                                   - 8)),
3993                                                                       TRUNC(start_date), targeted_customers,
3994                                                                       0
3995                                                                      ),
3996                                        0
3997                                       ))  prev_targeted_customers,
3998 
3999                             SUM(DECODE(TRUNC(start_date),
4000                                        TRUNC(SYSDATE - 8), 0,
4001                                        DECODE(LEAST(TRUNC(start_date),
4002                                                     TRUNC(SYSDATE - 8)),
4003                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
4004                                                                                  TRUNC(  SYSDATE
4005                                                                                        - 8)),
4006                                                                         TRUNC(end_date), budget_approved,
4007                                                                         0
4008                                                                        ),
4009                                               0
4010                                              )
4011                                       )) +  SUM(DECODE(GREATEST(TRUNC(start_date),
4012                                                 TRUNC(SYSDATE - 8)),
4013                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
4014                                                                             TRUNC(  SYSDATE
4015                                                                                   - 1)),
4016                                                                       TRUNC(start_date), budget_approved,
4017                                                                       0
4018                                                                      ))) budget_approved
4019                        FROM bim_r_camp_daily_facts a,
4020                             jtf_loc_hierarchies_b c
4021                       WHERE a.schedule_country = c.location_hierarchy_id
4022 		             and trunc(a.start_date) <=  sysdate_1
4023 			     and trunc(a.end_date) >= SYSDATE - 15
4024                    GROUP BY a.campaign_id ,c.area2_code,
4025                             a.schedule_country,
4026                             NVL(a.business_unit_id, 0),
4027                             a.schedule_activity_id
4028 ------------------------------------------------------------------------------------------------
4029                    UNION ALL
4030 ------------------------------------------------------------------------------------------------
4031                    SELECT  /*+ use_hash(A)   use_hash(C)  */
4032 		            a.campaign_id campaign_id,
4033 		            c.area2_code schedule_area2,
4034                             a.schedule_country schedule_country,
4035                             NVL(a.business_unit_id, 0) business_unit_id,
4036                             a.schedule_activity_id schedule_activity_id,
4037                             'INCREMENT' aggregate_by,
4038                             'MONTH' display_type,
4039                             0 curr_count_value,
4040                             0 curr_started_value,
4041                             0 curr_ended_value,
4042                             0 prev_count_value,
4043                             0 prev_started_value,
4044                             0 prev_ended_value,
4045                             0 campaign_count,
4046                             SUM(DECODE(TRUNC(start_date),
4047                                        l_fiscal_month_start, 0,
4048                                        DECODE(LEAST(TRUNC(start_date),
4049                                                     l_fiscal_month_start),
4050                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
4051                                                                                  l_fiscal_month_start),
4052                                                                         TRUNC(end_date), forecasted_responses,
4053                                                                         0
4054                                                                        ),
4055                                               0
4056                                              )
4057                                       )) +
4058 				      SUM(DECODE(GREATEST(TRUNC(start_date),
4059                                                 l_fiscal_month_start),
4060                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
4061                                                                               SYSDATE
4062                                                                             - 1),
4063                                                                       TRUNC(start_date), forecasted_responses,
4064                                                                       0
4065                                                                      ),
4066                                        0
4067                                       ))  forecasted_responses,
4068                             SUM(DECODE(TRUNC(start_date),
4069                                        l_fiscal_month_start, 0,
4070                                        DECODE(LEAST(TRUNC(start_date),
4071                                                     l_fiscal_month_start),
4072                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
4073                                                                                  l_fiscal_month_start),
4074                                                                         TRUNC(end_date), targeted_customers,
4075                                                                         0
4076                                                                        ),
4077                                               0
4078                                              )
4079                                       )) +
4080 				      SUM(DECODE(GREATEST(TRUNC(start_date),
4081                                                 l_fiscal_month_start),
4082                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
4083                                                                               SYSDATE
4084                                                                             - 1),
4085                                                                       TRUNC(start_date), targeted_customers,
4086                                                                       0
4087                                                                      ),
4088                                        0
4089                                       ))  targeted_customers,
4090                             SUM(DECODE(TRUNC(start_date),
4091                                        l_pre_fiscal_month_start, 0,
4092                                        DECODE(LEAST(TRUNC(start_date),
4093                                                     l_pre_fiscal_month_start),
4094                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
4095                                                                                  l_pre_fiscal_month_start),
4096                                                                         TRUNC(end_date), targeted_customers,
4097                                                                         0
4098                                                                        ),
4099                                               0
4100                                              )
4101                                       ))  +
4102 
4103 				 SUM(DECODE(GREATEST(TRUNC(start_date),
4104                                                 l_pre_fiscal_month_start),
4105                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
4106                                                                             l_pre_fiscal_month_end),
4107                                                                       TRUNC(start_date), targeted_customers,
4108                                                                       0
4109                                                                      ),
4110                                        0
4111                                       ))  prev_targeted_customers,
4112                             SUM(DECODE(TRUNC(start_date),
4113                                        l_fiscal_month_start, 0,
4114                                        DECODE(LEAST(TRUNC(start_date),
4115                                                     l_fiscal_month_start),
4116                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
4117                                                                                  l_fiscal_month_start),
4118                                                                         TRUNC(end_date), budget_approved,
4119                                                                         0
4120                                                                        ),
4121                                               0
4122                                              )
4123                                       )) +
4124 				      SUM(DECODE(GREATEST(TRUNC(start_date),
4125                                                 l_fiscal_month_start),
4126                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
4127                                                                               SYSDATE
4128                                                                             - 1),
4129                                                                       TRUNC(start_date), budget_approved,
4130                                                                       0
4131                                                                      ),
4132                                        0
4133                                       ))  budget_approved
4134                        FROM bim_r_camp_daily_facts a,
4135                             jtf_loc_hierarchies_b c
4136                       WHERE a.schedule_country = c.location_hierarchy_id
4137 		            and trunc(a.start_date) <=  sysdate_1
4138 			    and trunc(a.end_date) >= l_pre_fiscal_month_start
4139                    GROUP BY a.campaign_id ,c.area2_code,
4140                             a.schedule_country,
4141                             NVL(a.business_unit_id, 0),
4142                             a.schedule_activity_id
4143 ------------------------------------------------------------------------------------------------
4144                    UNION ALL
4145 ------------------------------------------------------------------------------------------------
4146                    SELECT  /*+ use_hash(A)  use_hash(C)  */
4147 		            a.campaign_id campaign_id,
4148 		            c.area2_code schedule_area2,
4149                             a.schedule_country schedule_country,
4150                             NVL(a.business_unit_id, 0) business_unit_id,
4151                             a.schedule_activity_id schedule_activity_id,
4152                             'INCREMENT' aggregate_by,
4153                             'QUARTER' display_type,
4154                             0 curr_count_value,
4155                             0 curr_started_value,
4156                             0 curr_ended_value,
4157                             0 prev_count_value,
4158                             0 prev_started_value,
4159                             0 prev_ended_value,
4160                             0 campaign_count,
4161                             SUM(DECODE(TRUNC(start_date),
4162                                        l_fiscal_qtr_start, 0,
4163                                        DECODE(LEAST(TRUNC(start_date),
4164                                                     l_fiscal_qtr_start),
4165                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
4166                                                                                  l_fiscal_qtr_start),
4167                                                                         TRUNC(end_date), forecasted_responses,
4168                                                                         0
4169                                                                        ),
4170                                               0
4171                                              )
4172                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date),
4173                                                 l_fiscal_qtr_start),
4174                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
4175                                                                               SYSDATE
4176                                                                             - 1),
4177                                                                       TRUNC(start_date), forecasted_responses,
4178                                                                       0
4179                                                                      ),
4180                                        0
4181                                       ))  forecasted_responses,
4182                             SUM(DECODE(TRUNC(start_date),
4183                                        l_fiscal_qtr_start, 0,
4184                                        DECODE(LEAST(TRUNC(start_date),
4185                                                     l_fiscal_qtr_start),
4186                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
4187                                                                                  l_fiscal_qtr_start),
4188                                                                         TRUNC(end_date), targeted_customers,
4189                                                                         0
4190                                                                        ),
4191                                               0
4192                                              )
4193                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date),
4194                                                 l_fiscal_qtr_start),
4195                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
4196                                                                               SYSDATE
4197                                                                             - 1),
4198                                                                       TRUNC(start_date), targeted_customers,
4199                                                                       0
4200                                                                      ),
4201                                        0
4202                                       )) targeted_customers,
4203                             SUM(DECODE(TRUNC(start_date),
4204                                        l_pre_fiscal_qtr_start, 0,
4205                                        DECODE(LEAST(TRUNC(start_date),
4206                                                     l_pre_fiscal_qtr_start),
4207                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
4208                                                                                  l_pre_fiscal_qtr_start),
4209                                                                         TRUNC(end_date), targeted_customers,
4210                                                                         0
4211                                                                        ),
4212                                               0
4213                                              )
4214                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date),
4215                                                 l_pre_fiscal_qtr_start),
4216                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
4217                                                                             l_pre_fiscal_qtr_end),
4218                                                                       TRUNC(start_date), targeted_customers,
4219                                                                       0
4220                                                                      ),
4221                                        0
4222                                       ))  prev_targeted_customers,
4223                             SUM(DECODE(TRUNC(start_date),
4224                                        l_fiscal_qtr_start, 0,
4225                                        DECODE(LEAST(TRUNC(start_date),
4226                                                     l_fiscal_qtr_start),
4227                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
4228                                                                                  l_fiscal_qtr_start),
4229                                                                         TRUNC(end_date), budget_approved,
4230                                                                         0
4231                                                                        ),
4232                                               0
4233                                              )
4234                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date),
4235                                                 l_fiscal_qtr_start),
4236                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
4237                                                                               SYSDATE
4238                                                                             - 1),
4239                                                                       TRUNC(start_date), budget_approved,
4240                                                                       0
4241                                                                      ),
4242                                        0
4243                                       )) budget_approved
4244                        FROM bim_r_camp_daily_facts a,
4245                             jtf_loc_hierarchies_b c
4246                       WHERE a.schedule_country = c.location_hierarchy_id
4247 		             and trunc(a.start_date) <=  sysdate_1
4248 			     and trunc(a.end_date) >= l_pre_fiscal_qtr_start
4249                    GROUP BY a.campaign_id,c.area2_code,
4250                             a.schedule_country,
4251                             NVL(a.business_unit_id, 0),
4252                             a.schedule_activity_id
4253 ------------------------------------------------------------------------------------------------
4254                    UNION ALL
4255 ------------------------------------------------------------------------------------------------
4256                    SELECT  /*+ use_hash(A)  use_hash(C)  */
4257 		            a.campaign_id campaign_id,
4258 		            c.area2_code schedule_area2,
4259                             a.schedule_country schedule_country,
4260                             NVL(a.business_unit_id, 0) business_unit_id,
4261                             a.schedule_activity_id schedule_activity_id,
4262                             'INCREMENT' aggregate_by,
4263                             'YEAR' display_type,
4264                             0 curr_count_value,
4265                             0 curr_started_value,
4266                             0 curr_ended_value,
4267                             0 prev_count_value,
4268                             0 prev_started_value,
4269                             0 prev_ended_value,
4270                             0 campaign_count,
4271                             SUM(DECODE(TRUNC(start_date),
4272                                        l_fiscal_roll_year_start, 0,
4273                                        DECODE(LEAST(TRUNC(start_date),
4274                                                     l_fiscal_roll_year_start),
4275                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
4276                                                                                  l_fiscal_roll_year_start),
4277                                                                         TRUNC(end_date), forecasted_responses,
4278                                                                         0
4279                                                                        ),
4280                                               0
4281                                              )
4282                                       ))  + SUM(DECODE(GREATEST(TRUNC(start_date),
4283                                                 l_fiscal_roll_year_start),
4284                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
4285                                                                               SYSDATE
4286                                                                             - 1),
4287                                                                       TRUNC(start_date), forecasted_responses,
4288                                                                       0
4289                                                                      ),
4290                                        0
4291                                       )) forecasted_responses,
4292                             SUM(DECODE(TRUNC(start_date),
4293                                        l_fiscal_roll_year_start, 0,
4294                                        DECODE(LEAST(TRUNC(start_date),
4295                                                     l_fiscal_roll_year_start),
4296                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
4297                                                                                  l_fiscal_roll_year_start),
4298                                                                         TRUNC(end_date), targeted_customers,
4299                                                                         0
4300                                                                        ),
4301                                               0
4302                                              )
4303                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date),
4304                                                 l_fiscal_roll_year_start),
4305                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
4306                                                                               SYSDATE
4307                                                                             - 1),
4308                                                                       TRUNC(start_date), targeted_customers,
4309                                                                       0
4310                                                                      ),
4311                                        0
4312                                       )) targeted_customers,
4313                             SUM(DECODE(TRUNC(start_date),
4314                                        l_pre_fiscal_roll_year_start, 0,
4315                                        DECODE(LEAST(TRUNC(start_date),
4316                                                     l_pre_fiscal_roll_year_start),
4317                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
4318                                                                                  l_pre_fiscal_roll_year_start),
4319                                                                         TRUNC(end_date), targeted_customers,
4320                                                                         0
4321                                                                        ),
4322                                               0
4323                                              )
4324                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date),
4325                                                 l_pre_fiscal_roll_year_start),
4326                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
4327                                                                               l_fiscal_roll_year_start
4328                                                                             - 1),
4329                                                                       TRUNC(start_date), targeted_customers,
4330                                                                       0
4331                                                                      ),
4332                                        0
4333                                       ))  prev_targeted_customers,
4334                             SUM(DECODE(TRUNC(start_date),
4335                                        l_fiscal_roll_year_start, 0,
4336                                        DECODE(LEAST(TRUNC(start_date),
4337                                                     l_fiscal_roll_year_start),
4338                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
4339                                                                                  l_fiscal_roll_year_start),
4340                                                                         TRUNC(end_date), budget_approved,
4341                                                                         0
4342                                                                        ),
4343                                               0
4344                                              )
4345                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date),
4346                                                 l_fiscal_roll_year_start),
4347                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
4348                                                                               SYSDATE
4349                                                                             - 1),
4350                                                                       TRUNC(start_date), budget_approved,
4351                                                                       0
4352                                                                      ),
4353                                        0
4354                                       )) budget_approved
4355                        FROM bim_r_camp_daily_facts a,
4356 		           jtf_loc_hierarchies_b c
4357                       WHERE a.schedule_country = c.location_hierarchy_id
4358 		             and trunc(a.start_date) <=  sysdate_1
4359 			     and trunc(a.end_date) >= l_pre_fiscal_roll_year_start
4360                    GROUP BY a.campaign_id ,c.area2_code,
4361                             a.schedule_country,
4362                             NVL(a.business_unit_id, 0),
4363                             a.schedule_activity_id
4364                   ),ams_act_access_denorm R
4365 			    where campaign_id=r.object_id
4366 			    and r.object_type='CAMP'
4367          GROUP BY schedule_area2,
4368                   schedule_country,
4369                   business_unit_id,
4370                   resource_id,
4371                   schedule_activity_id,
4372                   aggregate_by,
4373                   display_type
4374 
4375 
4376 	  ---------- for admin
4377 				  union all
4378 				  -------------
4379 
4380 		 SELECT  /* +INDEX_FFS(R,BIM_DBI_U2) */
4381 	          'N' campaign_area1,
4382                   'N' campaign_area2,
4383                   'N' schedule_area1,
4384                   schedule_area2,
4385                   'N' campaign_country,
4386                   schedule_country,
4387                   business_unit_id,
4388                   -1 resource_id,
4389                   'N' campaign_type,
4390                   schedule_activity_id,
4391                   'N' schedule_activity_type,
4392                   'N' campaign_status,
4393                   'N' schedule_status,
4394                   aggregate_by,
4395                   display_type,
4396                   'N' YEAR,
4397                   'N' qtr,
4398                   'N' MONTH,
4399                   'ACT' report_type,
4400                   SUM(curr_count_value) current_count_value,
4401                   SUM(curr_started_value) current_started_value,
4402                   SUM(curr_ended_value) current_ended_value,
4403                   SUM(prev_count_value) previous_count_value,
4404                   SUM(prev_started_value) previous_started_value,
4405                   SUM(prev_ended_value) previous_ended_value,
4406                   SUM(campaign_count) campaign_count,
4407                   SUM(forecasted_responses) forecasted_responses,
4408                   SUM(targeted_customers) targeted_customers,
4409                   SUM(prev_targeted_customers) prev_targeted_customers,
4410                   SUM(budget_approved) budget_approved
4411              FROM (SELECT  /*+ use_hash(A)  use_hash(B) use_hash(C)  */
4412 			   a.campaign_id campaign_id,
4413 			   c.area2_code schedule_area2,
4414                             a.country_id schedule_country,
4415                             NVL(b.business_unit_id, 0) business_unit_id,
4416                             DECODE(a.activity_type_code,
4417                                    'EVENTS', -9999,
4418                                    a.activity_id
4419                                   ) schedule_activity_id,
4420                             'INCREMENT' aggregate_by,
4421                             'DAY' display_type,
4422                             SUM(DECODE(TRUNC(start_date_time),
4423                                        TRUNC(SYSDATE - 1), 0,
4424                                        DECODE(LEAST(TRUNC(start_date_time),
4425                                                     TRUNC(SYSDATE - 1)),
4426                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
4427                                                                                       TRUNC(  SYSDATE
4428                                                                                             - 1)),
4429                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
4430                                                                              0
4431                                                                             ),
4432                                               0
4433                                              )
4434                                       )) + SUM(DECODE(TRUNC(start_date_time),
4435                                        TRUNC(SYSDATE - 1), 1,
4436                                        0
4437                                       )) curr_count_value,
4438 	--*** added curr_started_value to curr_count_value
4439 /*
4440                             SUM(DECODE(TRUNC(start_date_time),
4441                                        TRUNC(SYSDATE - 1), 1,
4442                                        0
4443                                       )) curr_started_value,
4444                             SUM(DECODE(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
4445                                        TRUNC(SYSDATE - 1), 1,
4446                                        0
4447                                       )) curr_ended_value,
4448 */
4449                             SUM(DECODE(TRUNC(start_date_time),
4450                                        TRUNC(SYSDATE - 2), 0,
4451                                        DECODE(LEAST(TRUNC(start_date_time),
4452                                                     TRUNC(SYSDATE - 2)),
4453                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
4454                                                                                       TRUNC(  SYSDATE
4455                                                                                             - 2)),
4456                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
4457                                                                              0
4458                                                                             ),
4459                                               0
4460                                              )
4461                                       )) +  SUM(DECODE(TRUNC(start_date_time),
4462                                        TRUNC(SYSDATE - 2), 1,
4463                                        0
4464                                       )) prev_count_value,
4465 
4466 --*** added prev_started_value to prev_count_value
4467 /*
4468                             SUM(DECODE(TRUNC(start_date_time),
4469                                        TRUNC(SYSDATE - 2), 1,
4470                                        0
4471                                       )) prev_started_value,
4472                             SUM(DECODE(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
4473                                        TRUNC(SYSDATE - 2), 1,
4474                                        0
4475                                       )) prev_ended_value,
4476 */
4477                             0 curr_started_value,
4478 			    0 curr_ended_value,
4479 			    0 prev_started_value,
4480                             0 prev_ended_value,
4481                             0 campaign_count,
4482                             0 forecasted_responses,
4483                             0 targeted_customers,
4484                             0 prev_targeted_customers,
4485                             0 budget_approved
4486                        FROM ams_campaign_schedules_b a,
4487                             jtf_loc_hierarchies_b c,
4488                             ams_campaigns_all_b b
4489                       WHERE a.status_code IN
4490                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
4491                         AND trunc(a.start_date_time) >= l_min_start_date
4492 			and trunc(a.start_date_time) <= sysdate_1
4493 			and trunc(NVL(a.end_date_time, b.actual_exec_end_date)) >= SYSDATE - 2
4494                         AND a.country_id = c.location_hierarchy_id
4495                         AND a.campaign_id = b.campaign_id
4496                    GROUP BY a.campaign_id,c.area2_code,
4497                             a.country_id,
4498                             NVL(b.business_unit_id, 0),
4499                             DECODE(a.activity_type_code,
4500                                    'EVENTS', -9999,
4501                                    a.activity_id
4502                                   )
4503 ------------------------------------------------------------------------------------------------
4504                    UNION ALL
4505 ------------------------------------------------------------------------------------------------
4506                    SELECT  /*+ use_hash(A)   use_hash(B) use_hash(C)  */
4507 			   a.campaign_id campaign_id,
4508 			   c.area2_code schedule_area2,
4509                             a.country_id schedule_country,
4510                             NVL(b.business_unit_id, 0) business_unit_id,
4511                             DECODE(a.activity_type_code,
4512                                    'EVENTS', -9999,
4513                                    a.activity_id
4514                                   ) schedule_activity_id,
4515                             'INCREMENT' aggregate_by,
4516                             'WEEK' display_type,
4517                             SUM(DECODE(TRUNC(start_date_time),
4518                                        TRUNC(SYSDATE - 8), 0,
4519                                        DECODE(LEAST(TRUNC(start_date_time),
4520                                                     TRUNC(SYSDATE - 8)),
4521                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
4522                                                                                       TRUNC(  SYSDATE
4523                                                                                             - 8)),
4524                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
4525                                                                              0
4526                                                                             ),
4527                                               0
4528                                              )
4529                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date_time),
4530                                                 TRUNC(SYSDATE - 8)),
4531                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
4532                                                                             TRUNC(  SYSDATE
4533                                                                                   - 1)),
4534                                                                       TRUNC(start_date_time), 1,
4535                                                                       0
4536                                                                      ),
4537                                        0
4538                                       )) curr_count_value,
4539 --*** started value is added to count value
4540 
4541  /*                           SUM(DECODE(GREATEST(TRUNC(start_date_time),
4542                                                 TRUNC(SYSDATE - 8)),
4543                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
4544                                                                             TRUNC(  SYSDATE
4545                                                                                   - 1)),
4546                                                                       TRUNC(start_date_time), 1,
4547                                                                       0
4548                                                                      ),
4549                                        0
4550                                       )) curr_started_value,
4551                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
4552                                                 TRUNC(SYSDATE - 8)),
4553                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
4554                                                                             TRUNC(  SYSDATE
4555                                                                                   - 1)),
4556                                                                       trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
4557                                                                       0
4558                                                                      ),
4559                                        0
4560                                       )) curr_ended_value,
4561 */
4562 
4563 			    SUM(DECODE(TRUNC(start_date_time),
4564                                        TRUNC(SYSDATE - 15), 0,
4565                                        DECODE(LEAST(TRUNC(start_date_time),
4566                                                     TRUNC(SYSDATE - 15)),
4567                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
4568                                                                                       TRUNC(  SYSDATE
4569                                                                                             - 15)),
4570                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
4571                                                                              0
4572                                                                             ),
4573                                               0
4574                                              )
4575                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date_time),
4576                                                 TRUNC(SYSDATE - 15)),
4577                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
4578                                                                             TRUNC(  SYSDATE
4579                                                                                   - 8)),
4580                                                                       TRUNC(start_date_time), 1,
4581                                                                       0
4582                                                                      ),
4583                                        0
4584                                       )) prev_count_value,
4585 --***
4586 /*
4587                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
4588                                                 TRUNC(SYSDATE - 15)),
4589                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
4590                                                                             TRUNC(  SYSDATE
4591                                                                                   - 8)),
4592                                                                       TRUNC(start_date_time), 1,
4593                                                                       0
4594                                                                      ),
4595                                        0
4596                                       )) prev_started_value,
4597                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
4598                                                 TRUNC(SYSDATE - 15)),
4599                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
4600                                                                           TRUNC(  SYSDATE
4601                                                                                 - 8)),
4602                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
4603                                                                     0
4604                                                                    ),
4605                                        0
4606                                       )) prev_ended_value,
4607 */
4608 			    0 curr_started_value,
4609 			    0 curr_ended_value,
4610 			    0 prev_started_value,
4611                             0 prev_ended_value,
4612                             0 campaign_count,
4613                             0 forecasted_responses,
4614                             0 targeted_customers,
4615                             0 prev_targeted_customers,
4616                             0 budget_approved
4617                        FROM ams_campaign_schedules_b a,
4618                             jtf_loc_hierarchies_b c,
4619                             ams_campaigns_all_b b
4620                       WHERE a.status_code IN
4621                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
4622                         AND trunc(a.start_date_time) >= l_min_start_date
4623 			and trunc(a.start_date_time) <= sysdate_1
4624 			and trunc(NVL(a.end_date_time, b.actual_exec_end_date)) >=SYSDATE - 15
4625                         AND a.country_id = c.location_hierarchy_id
4626                         AND a.campaign_id = b.campaign_id
4627                    GROUP BY a.campaign_id,
4628 		            c.area1_code,
4629                             c.area2_code,
4630                             a.country_id,
4631                             NVL(b.business_unit_id, 0),
4632                             DECODE(a.activity_type_code,
4633                                    'EVENTS', -9999,
4634                                    a.activity_id
4635                                   )
4636 ------------------------------------------------------------------------------------------------
4637                    UNION ALL
4638 ------------------------------------------------------------------------------------------------
4639                    SELECT  /*+ use_hash(A)  use_hash(B) use_hash(C)  */
4640 			   a.campaign_id campaign_id,
4641 			   c.area2_code schedule_area2,
4642                             a.country_id schedule_country,
4643                             NVL(b.business_unit_id, 0) business_unit_id,
4644                             DECODE(a.activity_type_code,
4645                                    'EVENTS', -9999,
4646                                    a.activity_id
4647                                   ) schedule_activity_id,
4648                             'INCREMENT' aggregate_by,
4649                             'MONTH' display_type,
4650                             SUM(DECODE(TRUNC(start_date_time),
4651                                        l_fiscal_month_start, 0,
4652                                        DECODE(LEAST(TRUNC(start_date_time),
4653                                                     l_fiscal_month_start),
4654                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
4655                                                                                       l_fiscal_month_start),
4656                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
4657                                                                              0
4658                                                                             ),
4659                                               0
4660                                              )
4661                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date_time),
4662                                                 l_fiscal_month_start),
4663                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
4664                                                                               SYSDATE
4665                                                                             - 1),
4666                                                                       TRUNC(start_date_time), 1,
4667                                                                       0
4668                                                                      ),
4669                                        0
4670                                       )) curr_count_value,
4671 --***
4672 /*
4673 
4674                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
4675                                                 l_fiscal_month_start),
4676                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
4677                                                                               SYSDATE
4678                                                                             - 1),
4679                                                                       TRUNC(start_date_time), 1,
4680                                                                       0
4681                                                                      ),
4682                                        0
4683                                       )) curr_started_value,
4684                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
4685                                                 l_fiscal_month_start),
4686                                        TRUNC(end_date_time), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
4687                                                                             SYSDATE
4688                                                                           - 1),
4689                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
4690                                                                     0
4691                                                                    ),
4692                                        0
4693                                       )) curr_ended_value,
4694 */
4695 
4696                             SUM(DECODE(TRUNC(start_date_time),
4697                                        l_pre_fiscal_month_start, 0,
4698                                        DECODE(LEAST(TRUNC(start_date_time),
4699                                                     l_pre_fiscal_month_start),
4700                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
4701                                                                                       l_pre_fiscal_month_start),
4702                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
4703                                                                              0
4704                                                                             ),
4705                                               0
4706                                              )
4707                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date_time),
4708                                                 l_pre_fiscal_month_start),
4709                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
4710                                                                             l_pre_fiscal_month_end),
4711                                                                       TRUNC(start_date_time), 1,
4712                                                                       0
4713                                                                      ),
4714                                        0
4715                                       ))  prev_count_value,
4716 
4717 --***
4718 /*
4719                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
4720                                                 l_pre_fiscal_month_start),
4721                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
4722                                                                             l_pre_fiscal_month_end),
4723                                                                       TRUNC(start_date_time), 1,
4724                                                                       0
4725                                                                      ),
4726                                        0
4727                                       )) prev_started_value,
4728                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
4729                                                 l_pre_fiscal_month_start),
4730                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
4731                                                                           l_pre_fiscal_month_end),
4732                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
4733                                                                     0
4734                                                                    ),
4735                                        0
4736                                       )) prev_ended_value,
4737 */
4738 			    0 curr_started_value,
4739 			    0 curr_ended_value,
4740 			    0 prev_started_value,
4741                             0 prev_ended_value,
4742                             0 campaign_count,
4743                             0 forecasted_responses,
4744                             0 targeted_customers,
4745                             0 prev_targeted_customers,
4746                             0 budget_approved
4747                        FROM ams_campaign_schedules_b a,
4748                             jtf_loc_hierarchies_b c,
4749                             ams_campaigns_all_b b
4750                       WHERE a.status_code IN
4751                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
4752                         AND trunc(a.start_date_time) >= l_min_start_date
4753 			and trunc(a.start_date_time) <= sysdate_1
4754 			and trunc(NVL(a.end_date_time, b.actual_exec_end_date)) >=l_pre_fiscal_month_start
4755                         AND a.country_id = c.location_hierarchy_id
4756                         AND a.campaign_id = b.campaign_id
4757                    GROUP BY a.campaign_id ,
4758 		            c.area2_code,
4759                             a.country_id,
4760                             NVL(b.business_unit_id, 0),
4761                             DECODE(a.activity_type_code,
4762                                    'EVENTS', -9999,
4763                                    a.activity_id
4764                                   )
4765 ------------------------------------------------------------------------------------------------
4766                    UNION ALL
4767 ------------------------------------------------------------------------------------------------
4768                    SELECT   /*+ use_hash(A)  use_hash(B) use_hash(C)  */
4769 			    a.campaign_id campaign_id,
4770 			    c.area2_code schedule_area2,
4771                             a.country_id schedule_country,
4772                             NVL(b.business_unit_id, 0) business_unit_id,
4773                             DECODE(a.activity_type_code,
4774                                    'EVENTS', -9999,
4775                                    a.activity_id
4776                                   ) schedule_activity_id,
4777                             'INCREMENT' aggregate_by,
4778                             'QUARTER' display_type,
4779                             SUM(DECODE(TRUNC(start_date_time),
4780                                        l_fiscal_qtr_start, 0,
4781                                        DECODE(LEAST(TRUNC(start_date_time),
4782                                                     l_fiscal_qtr_start),
4783                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
4784                                                                                       l_fiscal_qtr_start),
4785                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
4786                                                                              0
4787                                                                             ),
4788                                               0
4789                                              )
4790                                       )) +  SUM(DECODE(GREATEST(TRUNC(start_date_time),
4791                                                 l_fiscal_qtr_start),
4792                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
4793                                                                               SYSDATE
4794                                                                             - 1),
4795                                                                       TRUNC(start_date_time), 1,
4796                                                                       0
4797                                                                      ),
4798                                        0
4799                                       ))  curr_count_value,
4800 --***
4801 /*
4802                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
4803                                                 l_fiscal_qtr_start),
4804                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
4805                                                                               SYSDATE
4806                                                                             - 1),
4807                                                                       TRUNC(start_date_time), 1,
4808                                                                       0
4809                                                                      ),
4810                                        0
4811                                       )) curr_started_value,
4812                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
4813                                                 l_fiscal_qtr_start),
4814                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
4815                                                                             SYSDATE
4816                                                                           - 1),
4817                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
4818                                                                     0
4819                                                                    ),
4820                                        0
4821                                       )) curr_ended_value,
4822 */
4823 
4824                             SUM(DECODE(TRUNC(start_date_time),
4825                                        l_pre_fiscal_qtr_start, 0,
4826                                        DECODE(LEAST(TRUNC(start_date_time),
4827                                                     l_pre_fiscal_qtr_start),
4828                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
4829                                                                                       l_pre_fiscal_qtr_start),
4830                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
4831                                                                              0
4832                                                                             ),
4833                                               0
4834                                              )
4835                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date_time),
4836                                                 l_pre_fiscal_qtr_start),
4837                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
4838                                                                             l_pre_fiscal_qtr_end),
4839                                                                       TRUNC(start_date_time), 1,
4840                                                                       0
4841                                                                      ),
4842                                        0
4843                                       )) prev_count_value,
4844 --***
4845 /*
4846 
4847                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
4848                                                 l_pre_fiscal_qtr_start),
4849                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
4850                                                                             l_pre_fiscal_qtr_end),
4851                                                                       TRUNC(start_date_time), 1,
4852                                                                       0
4853                                                                      ),
4854                                        0
4855                                       )) prev_started_value,
4856                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
4857                                                 l_pre_fiscal_qtr_start),
4858                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
4859                                                                           l_pre_fiscal_qtr_end),
4860                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
4861                                                                     0
4862                                                                    ),
4863                                        0
4864                                       )) prev_ended_value,
4865 */
4866 
4867 			    0 curr_started_value,
4868 			    0 curr_ended_value,
4869 			    0 prev_started_value,
4870                             0 prev_ended_value,
4871                             0 campaign_count,
4872                             0 forecasted_responses,
4873                             0 targeted_customers,
4874                             0 prev_targeted_customers,
4875                             0 budget_approved
4876                        FROM ams_campaign_schedules_b a,
4877                             jtf_loc_hierarchies_b c,
4878                             ams_campaigns_all_b b
4879                       WHERE a.status_code IN
4880                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
4881                         AND trunc(a.start_date_time) >= l_min_start_date
4882 			and trunc(a.start_date_time) <= sysdate_1
4883 			and trunc(NVL(a.end_date_time, b.actual_exec_end_date)) >=l_pre_fiscal_qtr_start
4884                         AND a.country_id = c.location_hierarchy_id
4885                         AND a.campaign_id = b.campaign_id
4886                    GROUP BY a.campaign_id ,c.area2_code,
4887                             a.country_id,
4888                             NVL(b.business_unit_id, 0),
4889                             DECODE(a.activity_type_code,
4890                                    'EVENTS', -9999,
4891                                    a.activity_id
4892                                   )
4893 ------------------------------------------------------------------------------------------------
4894                    UNION ALL
4895 ------------------------------------------------------------------------------------------------
4896                    SELECT  /*+ use_hash(A)  use_hash(B) use_hash(C)  */
4897 			    a.campaign_id ,
4898 			    c.area2_code schedule_area2,
4899                             a.country_id schedule_country,
4900                             NVL(b.business_unit_id, 0) business_unit_id,
4901                             DECODE(a.activity_type_code,
4902                                    'EVENTS', -9999,
4903                                    a.activity_id
4904                                   ) schedule_activity_id,
4905                             'INCREMENT' aggregate_by,
4906                             'YEAR' display_type,
4907                             SUM(DECODE(TRUNC(start_date_time),
4908                                        l_fiscal_roll_year_start, 0,
4909                                        DECODE(LEAST(TRUNC(start_date_time),
4910                                                     l_fiscal_roll_year_start),
4911                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
4912                                                                                       l_fiscal_roll_year_start),
4913                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
4914                                                                              0
4915                                                                             ),
4916                                               0
4917                                              )
4918                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date_time),
4919                                                 l_fiscal_roll_year_start),
4920                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
4921                                                                               SYSDATE
4922                                                                             - 1),
4923                                                                       TRUNC(start_date_time), 1,
4924                                                                       0
4925                                                                      ),
4926                                        0
4927                                       )) curr_count_value,
4928 --***
4929 /*
4930                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
4931                                                 l_fiscal_roll_year_start),
4932                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
4933                                                                               SYSDATE
4934                                                                             - 1),
4935                                                                       TRUNC(start_date_time), 1,
4936                                                                       0
4937                                                                      ),
4938                                        0
4939                                       )) curr_started_value,
4940                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
4941                                                 l_fiscal_roll_year_start),
4942                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
4943                                                                             SYSDATE
4944                                                                           - 1),
4945                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
4946                                                                     0
4947                                                                    ),
4948                                        0
4949                                       )) curr_ended_value,
4950 */
4951                             SUM(DECODE(TRUNC(start_date_time),
4952                                        l_pre_fiscal_roll_year_start, 0,
4953                                        DECODE(LEAST(TRUNC(start_date_time),
4954                                                     l_pre_fiscal_roll_year_start),
4955                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
4956                                                                                       l_pre_fiscal_roll_year_start),
4957                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
4958                                                                              0
4959                                                                             ),
4960                                               0
4961                                              )
4962                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date_time),
4963                                                 l_pre_fiscal_roll_year_start),
4964                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
4965                                                                               l_fiscal_roll_year_start
4966                                                                             - 1),
4967                                                                       TRUNC(start_date_time), 1,
4968                                                                       0
4969                                                                      ),
4970                                        0
4971                                       )) prev_count_value,
4972 --***
4973 /*
4974                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
4975                                                 l_pre_fiscal_roll_year_start),
4976                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
4977                                                                               l_fiscal_roll_year_start
4978                                                                             - 1),
4979                                                                       TRUNC(start_date_time), 1,
4980                                                                       0
4981                                                                      ),
4982                                        0
4983                                       )) prev_started_value,
4984                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
4985                                                 l_pre_fiscal_roll_year_start),
4986                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
4987                                                                             l_fiscal_roll_year_start
4988                                                                           - 1),
4989                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
4990                                                                     0
4991                                                                    ),
4992                                        0
4993                                       )) prev_ended_value,
4994 */
4995 
4996 			    0 curr_started_value,
4997 			    0 curr_ended_value,
4998 			    0 prev_started_value,
4999                             0 prev_ended_value,
5000 			    0 campaign_count,
5001                             0 forecasted_responses,
5002                             0 targeted_customers,
5003                             0 prev_targeted_customers,
5004                             0 budget_approved
5005                        FROM ams_campaign_schedules_b a,
5006                             jtf_loc_hierarchies_b c,
5007                             ams_campaigns_all_b b
5008                       WHERE a.status_code IN
5009                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
5010                         AND trunc(a.start_date_time) >= l_min_start_date
5011 			and trunc(a.start_date_time) <= sysdate_1
5012 			and trunc(NVL(a.end_date_time, b.actual_exec_end_date)) >=l_pre_fiscal_roll_year_start
5013                         AND a.country_id = c.location_hierarchy_id
5014                         AND a.campaign_id = b.campaign_id
5015                    GROUP BY a.campaign_id ,
5016 		            c.area2_code,
5017                             a.country_id,
5018                             NVL(b.business_unit_id, 0),
5019                             DECODE(a.activity_type_code,
5020                                    'EVENTS', -9999,
5021                                    a.activity_id
5022                                   )
5023 -------------------------------------------------------------------------------------
5024 --START OF CAMPAIGN COUNT CODE
5025 ------------------------------------------------------------------------------------------------
5026                    UNION ALL
5027 ------------------------------------------------------------------------------------------------
5028                    SELECT  /*+ use_hash(A)   use_hash(B) use_hash(C)  */
5029 		            a.campaign_id campaign_id,
5030 			    c.area2_code schedule_area2,
5031                             a.country_id schedule_country,
5032                             NVL(b.business_unit_id, 0) business_unit_id,
5033                             DECODE(a.activity_type_code,
5034                                    'EVENTS', -9999,
5035                                    a.activity_id
5036                                   ) schedule_activity_id,
5037                             'INCREMENT' aggregate_by,
5038                             'DAY' display_type,
5039                             0 curr_count_value,
5040                             0 curr_started_value,
5041                             0 curr_ended_value,
5042                             0 prev_count_value,
5043                             0 prev_started_value,
5044                             0 prev_ended_value,
5045                             COUNT(DISTINCT a.campaign_id) campaign_count,
5046                             0 forecasted_responses,
5047                             0 targeted_customers,
5048                             0 prev_targeted_customers,
5049                             0 budget_approved
5050                        FROM ams_campaign_schedules_b a,
5051                             jtf_loc_hierarchies_b c,
5052                             ams_campaigns_all_b b
5053                       WHERE a.status_code IN
5054                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
5055 --***  <= will get both active and started campaigns for DAY diaplay_type
5056                         AND trunc(a.start_date_time) <= TRUNC(SYSDATE - 1)
5057                         AND a.country_id = c.location_hierarchy_id
5058                         AND a.campaign_id = b.campaign_id
5059                         AND trunc(a.start_date_time) >= l_min_start_date
5060                         AND trunc(NVL(a.end_date_time, b.actual_exec_end_date)) >=
5061                                                             TRUNC(SYSDATE - 1)
5062                    GROUP BY a.campaign_id ,
5063 		            c.area2_code,
5064                             a.country_id,
5065                             NVL(b.business_unit_id, 0),
5066                             DECODE(a.activity_type_code,
5067                                    'EVENTS', -9999,
5068                                    a.activity_id
5069                                   )
5070 ------------------------------------------------------------------------------------------------
5071                    UNION ALL
5072 ------------------------------------------------------------------------------------------------
5073                    SELECT  /*+ use_hash(A)  use_hash(B) use_hash(C)  */
5074 		            a.campaign_id campaign_id,
5075 		            c.area2_code schedule_area2,
5076                             a.country_id schedule_country,
5077                             NVL(b.business_unit_id, 0) business_unit_id,
5078                             DECODE(a.activity_type_code,
5079                                    'EVENTS', -9999,
5080                                    a.activity_id
5081                                   ) schedule_activity_id,
5082                             'INCREMENT' aggregate_by,
5083                             'WEEK' display_type,
5084                             0 curr_count_value,
5085                             0 curr_started_value,
5086                             0 curr_ended_value,
5087                             0 prev_count_value,
5088                             0 prev_started_value,
5089                             0 prev_ended_value,
5090                             COUNT(DISTINCT a.campaign_id) campaign_count,
5091                             0 forecasted_responses,
5092                             0 targeted_customers,
5093                             0 prev_targeted_customers,
5094                             0 budget_approved
5095                        FROM ams_campaign_schedules_b a,
5096                             jtf_loc_hierarchies_b c,
5097                             ams_campaigns_all_b b
5098                       WHERE a.status_code IN
5099                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
5100 --***
5101                         AND (trunc(a.start_date_time) < TRUNC(SYSDATE - 8)
5102 			     or
5103                              trunc(a.start_date_time) between TRUNC(SYSDATE - 8) and TRUNC(  SYSDATE
5104                                                                                   - 1)
5105                              )
5106                         AND a.country_id = c.location_hierarchy_id
5107                         AND a.campaign_id = b.campaign_id
5108                         AND trunc(a.start_date_time) >= l_min_start_date
5109 			and trunc(a.start_date_time) <= sysdate_1
5110                         AND trunc(NVL(a.end_date_time, b.actual_exec_end_date)) >=
5111                                                             TRUNC(SYSDATE - 8)
5112                    GROUP BY a.campaign_id ,c.area2_code,
5113                             a.country_id,
5114                             NVL(b.business_unit_id, 0),
5115                             DECODE(a.activity_type_code,
5116                                    'EVENTS', -9999,
5117                                    a.activity_id
5118                                   )
5119 ------------------------------------------------------------------------------------------------
5120                    UNION ALL
5121 ------------------------------------------------------------------------------------------------
5122                    SELECT   /*+ use_hash(A)  use_hash(B) use_hash(C)  */
5123 		           a.campaign_id campaign_id,
5124 		            c.area2_code schedule_area2,
5125                             a.country_id schedule_country,
5126                             NVL(b.business_unit_id, 0) business_unit_id,
5127                             DECODE(a.activity_type_code,
5128                                    'EVENTS', -9999,
5129                                    a.activity_id
5130                                   ) schedule_activity_id,
5131                             'INCREMENT' aggregate_by,
5132                             'MONTH' display_type,
5133                             0 curr_count_value,
5134                             0 curr_started_value,
5135                             0 curr_ended_value,
5136                             0 prev_count_value,
5137                             0 prev_started_value,
5138                             0 prev_ended_value,
5139                             COUNT(DISTINCT a.campaign_id) campaign_count,
5140                             0 forecasted_responses,
5141                             0 targeted_customers,
5142                             0 prev_targeted_customers,
5143                             0 budget_approved
5144                        FROM ams_campaign_schedules_b a,
5145                             jtf_loc_hierarchies_b c,
5146                             ams_campaigns_all_b b
5147                       WHERE a.status_code IN
5148                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
5149 --***
5150                         AND (trunc(a.start_date_time) < l_fiscal_month_start
5151 			     or
5152                              trunc(a.start_date_time) between l_fiscal_month_start and TRUNC(  SYSDATE
5153                                                                                   - 1)
5154                              )
5155                         AND a.country_id = c.location_hierarchy_id
5156                         AND a.campaign_id = b.campaign_id
5157                         AND trunc(a.start_date_time) >= l_min_start_date
5158 			and trunc(a.start_date_time) <= sysdate_1
5159                         AND trunc(NVL(a.end_date_time, b.actual_exec_end_date)) >=
5160                                                           l_fiscal_month_start
5161                    GROUP BY a.campaign_id ,c.area2_code,
5162                             a.country_id,
5163                             NVL(b.business_unit_id, 0),
5164                             DECODE(a.activity_type_code,
5165                                    'EVENTS', -9999,
5166                                    a.activity_id
5167                                   )
5168 ------------------------------------------------------------------------------------------------
5169                    UNION ALL
5170 ------------------------------------------------------------------------------------------------
5171                    SELECT   /*+ use_hash(A)  use_hash(B) use_hash(C)  */
5172 		            a.campaign_id campaign_id,
5173 		            c.area2_code schedule_area2,
5174                             a.country_id schedule_country,
5175                             NVL(b.business_unit_id, 0) business_unit_id,
5176                             DECODE(a.activity_type_code,
5177                                    'EVENTS', -9999,
5178                                    a.activity_id
5179                                   ) schedule_activity_id,
5180                             'INCREMENT' aggregate_by,
5181                             'QUARTER' display_type,
5182                             0 curr_count_value,
5183                             0 curr_started_value,
5184                             0 curr_ended_value,
5185                             0 prev_count_value,
5186                             0 prev_started_value,
5187                             0 prev_ended_value,
5188                             COUNT(DISTINCT a.campaign_id) campaign_count,
5189                             0 forecasted_responses,
5190                             0 targeted_customers,
5191                             0 prev_targeted_customers,
5192                             0 budget_approved
5193                        FROM ams_campaign_schedules_b a,
5194                             jtf_loc_hierarchies_b c,
5195                             ams_campaigns_all_b b
5196                       WHERE a.status_code IN
5197                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
5198 --***
5199                         AND (trunc(a.start_date_time) < l_fiscal_qtr_start
5200 			     or
5201                              trunc(a.start_date_time) between l_fiscal_qtr_start and TRUNC(  SYSDATE
5202                                                                                   - 1)
5203                               )
5204                         AND a.country_id = c.location_hierarchy_id
5205                         AND a.campaign_id = b.campaign_id
5206                         AND trunc(a.start_date_time) >= l_min_start_date
5207 			and trunc(a.start_date_time) <= sysdate_1
5208                         AND trunc(NVL(a.end_date_time, b.actual_exec_end_date)) >=
5209                                                             l_fiscal_qtr_start
5210                    GROUP BY a.campaign_id,
5211 		            c.area2_code,
5212                             a.country_id,
5213                             NVL(b.business_unit_id, 0),
5214                             DECODE(a.activity_type_code,
5215                                    'EVENTS', -9999,
5216                                    a.activity_id
5217                                   )
5218 ------------------------------------------------------------------------------------------------
5219                    UNION ALL
5220 ------------------------------------------------------------------------------------------------
5221                    SELECT   /*+ use_hash(A)  use_hash(B) use_hash(C)  */
5222 		            a.campaign_id campaign_id,
5223 			    c.area2_code schedule_area2,
5224                             a.country_id schedule_country,
5225                             NVL(b.business_unit_id, 0) business_unit_id,
5226                             DECODE(a.activity_type_code,
5227                                    'EVENTS', -9999,
5228                                    a.activity_id
5229                                   ) schedule_activity_id,
5230                             'INCREMENT' aggregate_by,
5231                             'YEAR' display_type,
5232                             0 curr_count_value,
5233                             0 curr_started_value,
5234                             0 curr_ended_value,
5235                             0 prev_count_value,
5236                             0 prev_started_value,
5237                             0 prev_ended_value,
5238                             COUNT(DISTINCT a.campaign_id) campaign_count,
5239                             0 forecasted_responses,
5240                             0 targeted_customers,
5241                             0 prev_targeted_customers,
5242                             0 budget_approved
5243                        FROM ams_campaign_schedules_b a,
5244                             jtf_loc_hierarchies_b c,
5245                             ams_campaigns_all_b b
5246                       WHERE a.status_code IN
5247                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
5248 --***
5249                         AND (trunc(a.start_date_time) < l_fiscal_roll_year_start
5250 			     or
5251                              trunc(a.start_date_time) between l_fiscal_roll_year_start and TRUNC(  SYSDATE
5252                                                                                   - 1)
5253                              )
5254                         AND a.country_id = c.location_hierarchy_id
5255                         AND a.campaign_id = b.campaign_id
5256                         AND trunc(a.start_date_time) >= l_min_start_date
5257 			and trunc(a.start_date_time) <= sysdate_1
5258                         AND trunc(NVL(a.end_date_time, b.actual_exec_end_date)) >=
5259                                                       l_fiscal_roll_year_start
5260                    GROUP BY a.campaign_id ,
5261 		            c.area2_code,
5262                             a.country_id,
5263                             NVL(b.business_unit_id, 0),
5264                             DECODE(a.activity_type_code,
5265                                    'EVENTS', -9999,
5266                                    a.activity_id
5267                                   )
5268 ------------------------------------------------------------------------------------------------
5269 ---START OF METRICS CODE FOR FRESP, TCUST, BAPPROVED
5270 ------------------------------------------------------------------------------------------------
5271 ------------------------------------------------------------------------------------------------
5272                    UNION ALL
5273 ------------------------------------------------------------------------------------------------
5274                    SELECT   /*+ use_hash(A)  use_hash(C)  */
5275 		            a.campaign_id campaign_id,
5276 			    c.area2_code schedule_area2,
5277                             a.schedule_country schedule_country,
5278                             NVL(a.business_unit_id, 0) business_unit_id,
5279                             a.schedule_activity_id schedule_activity_id,
5280                             'INCREMENT' aggregate_by,
5281                             'DAY' display_type,
5282                             0 curr_count_value,
5283                             0 curr_started_value,
5284                             0 curr_ended_value,
5285                             0 prev_count_value,
5286                             0 prev_started_value,
5287                             0 prev_ended_value,
5288                             0 campaign_count,
5289                             SUM(DECODE(TRUNC(start_date),
5290                                        TRUNC(SYSDATE - 1), 0,
5291                                        DECODE(LEAST(TRUNC(start_date),
5292                                                     TRUNC(SYSDATE - 1)),
5293                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
5294                                                                                  TRUNC(  SYSDATE
5295                                                                                        - 1)),
5296                                                                         TRUNC(end_date), forecasted_responses,
5297                                                                         0
5298                                                                        ),
5299                                               0
5300                                              )
5301                                       )) + SUM(DECODE(TRUNC(start_date),
5302                                        TRUNC(SYSDATE - 1), forecasted_responses,
5303                                        0
5304                                       )) forecasted_responses,
5305                             SUM(DECODE(TRUNC(start_date),
5306                                        TRUNC(SYSDATE - 1), 0,
5307                                        DECODE(LEAST(TRUNC(start_date),
5308                                                     TRUNC(SYSDATE - 1)),
5309                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
5310                                                                                  TRUNC(  SYSDATE
5311                                                                                        - 1)),
5312                                                                         TRUNC(end_date), targeted_customers,
5313                                                                         0
5314                                                                        ),
5315                                               0
5316                                              )
5317                                       )) + SUM(DECODE(TRUNC(start_date),
5318                                        TRUNC(SYSDATE - 1), targeted_customers,
5319                                        0
5320                                       )) targeted_customers,
5321 
5322 					SUM(DECODE(TRUNC(start_date),
5323                                        TRUNC(SYSDATE - 2), 0,
5324                                        DECODE(LEAST(TRUNC(start_date),
5325                                                     TRUNC(SYSDATE - 2)),
5326                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
5327                                                                                  TRUNC(  SYSDATE
5328                                                                                        - 2)),
5329                                                                         TRUNC(end_date), targeted_customers,
5330                                                                         0
5331                                                                        ),
5332                                               0
5333                                              )
5334                                       )) +    SUM(DECODE(TRUNC(start_date),
5335                                        TRUNC(SYSDATE - 2), targeted_customers,
5336                                        0
5337                                       ))  prev_targeted_customers,
5338                             SUM(DECODE(TRUNC(start_date),
5339                                        TRUNC(SYSDATE - 1), 0,
5340                                        DECODE(LEAST(TRUNC(start_date),
5341                                                     TRUNC(SYSDATE - 1)),
5342                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
5343                                                                                  TRUNC(  SYSDATE
5344                                                                                        - 1)),
5345                                                                         TRUNC(end_date), budget_approved,
5346                                                                         0
5347                                                                        ),
5348                                               0
5349                                              )
5350                                       )) + SUM(DECODE(TRUNC(start_date),
5351                                        TRUNC(SYSDATE - 1), budget_approved,
5352                                        0
5353                                       )) budget_approved
5354                        FROM bim_r_camp_daily_facts a,
5355                             jtf_loc_hierarchies_b c
5356                       WHERE a.schedule_country = c.location_hierarchy_id
5357 		             and trunc(a.start_date) <=  sysdate_1
5358 			     and trunc(a.end_date) >= SYSDATE - 2
5359                    GROUP BY a.campaign_id,
5360 		            c.area2_code,
5361                             a.schedule_country,
5362                             NVL(a.business_unit_id, 0),
5363                             a.schedule_activity_id
5364 ------------------------------------------------------------------------------------------------
5365                    UNION ALL
5366 ------------------------------------------------------------------------------------------------
5367                    SELECT   /*+ use_hash(A)  use_hash(C)  */
5368 		            a.campaign_id campaign_id,
5369 		            c.area2_code schedule_area2,
5370                             a.schedule_country schedule_country,
5371                             NVL(a.business_unit_id, 0) business_unit_id,
5372                             a.schedule_activity_id schedule_activity_id,
5373                             'INCREMENT' aggregate_by,
5374                             'WEEK' display_type,
5375                             0 curr_count_value,
5376                             0 curr_started_value,
5377                             0 curr_ended_value,
5378                             0 prev_count_value,
5379                             0 prev_started_value,
5380                             0 prev_ended_value,
5381                             0 campaign_count,
5382                             SUM(DECODE(TRUNC(start_date),
5383                                        TRUNC(SYSDATE - 8), 0,
5384                                        DECODE(LEAST(TRUNC(start_date),
5385                                                     TRUNC(SYSDATE - 8)),
5386                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
5387                                                                                  TRUNC(  SYSDATE
5388                                                                                        - 8)),
5389                                                                         TRUNC(end_date), forecasted_responses,
5390                                                                         0
5391                                                                        ),
5392                                               0
5393                                              )
5394                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date),
5395                                                 TRUNC(SYSDATE - 8)),
5396                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
5397                                                                             TRUNC(  SYSDATE
5398                                                                                   - 1)),
5399                                                                       TRUNC(start_date), forecasted_responses,
5400                                                                       0
5401                                                                      )))  forecasted_responses,
5402 
5403                             SUM(DECODE(TRUNC(start_date),
5404                                        TRUNC(SYSDATE - 8), 0,
5405                                        DECODE(LEAST(TRUNC(start_date),
5406                                                     TRUNC(SYSDATE - 8)),
5407                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
5408                                                                                  TRUNC(  SYSDATE
5409                                                                                        - 8)),
5410                                                                         TRUNC(end_date), targeted_customers,
5411                                                                         0
5412                                                                        ),
5413                                               0
5414                                              )
5415                                       ))+ SUM(DECODE(GREATEST(TRUNC(start_date),
5416                                                 TRUNC(SYSDATE - 8)),
5417                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
5418                                                                             TRUNC(  SYSDATE
5419                                                                                   - 1)),
5420                                                                       TRUNC(start_date), targeted_customers,
5421                                                                       0
5422                                                                      ))) targeted_customers,
5423                             SUM(DECODE(TRUNC(start_date),
5424                                        TRUNC(SYSDATE - 15), 0,
5425                                        DECODE(LEAST(TRUNC(start_date),
5426                                                     TRUNC(SYSDATE - 15)),
5427                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
5428                                                                                  TRUNC(  SYSDATE
5429                                                                                        - 15)),
5430                                                                         TRUNC(end_date), targeted_customers,
5431                                                                         0
5432                                                                        ),
5433                                               0
5434                                              )
5435                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date),
5436                                                 TRUNC(SYSDATE - 15)),
5437                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
5438                                                                             TRUNC(  SYSDATE
5439                                                                                   - 8)),
5440                                                                       TRUNC(start_date), targeted_customers,
5441                                                                       0
5442                                                                      ),
5443                                        0
5444                                       ))  prev_targeted_customers,
5445 
5446                             SUM(DECODE(TRUNC(start_date),
5447                                        TRUNC(SYSDATE - 8), 0,
5448                                        DECODE(LEAST(TRUNC(start_date),
5449                                                     TRUNC(SYSDATE - 8)),
5450                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
5451                                                                                  TRUNC(  SYSDATE
5452                                                                                        - 8)),
5453                                                                         TRUNC(end_date), budget_approved,
5454                                                                         0
5455                                                                        ),
5456                                               0
5457                                              )
5458                                       )) +  SUM(DECODE(GREATEST(TRUNC(start_date),
5459                                                 TRUNC(SYSDATE - 8)),
5460                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
5461                                                                             TRUNC(  SYSDATE
5462                                                                                   - 1)),
5463                                                                       TRUNC(start_date), budget_approved,
5464                                                                       0
5465                                                                      ))) budget_approved
5466                        FROM bim_r_camp_daily_facts a,
5467                             jtf_loc_hierarchies_b c
5468                       WHERE a.schedule_country = c.location_hierarchy_id
5469 		             and trunc(a.start_date) <=  sysdate_1
5470 			      and trunc(a.end_date) >= SYSDATE - 15
5471                    GROUP BY a.campaign_id ,c.area2_code,
5472                             a.schedule_country,
5473                             NVL(a.business_unit_id, 0),
5474                             a.schedule_activity_id
5475 ------------------------------------------------------------------------------------------------
5476                    UNION ALL
5477 ------------------------------------------------------------------------------------------------
5478                    SELECT  /*+ use_hash(A)   use_hash(C)  */
5479 		            a.campaign_id campaign_id,
5480 		            c.area2_code schedule_area2,
5481                             a.schedule_country schedule_country,
5482                             NVL(a.business_unit_id, 0) business_unit_id,
5483                             a.schedule_activity_id schedule_activity_id,
5484                             'INCREMENT' aggregate_by,
5485                             'MONTH' display_type,
5486                             0 curr_count_value,
5487                             0 curr_started_value,
5488                             0 curr_ended_value,
5489                             0 prev_count_value,
5490                             0 prev_started_value,
5491                             0 prev_ended_value,
5492                             0 campaign_count,
5493                             SUM(DECODE(TRUNC(start_date),
5494                                        l_fiscal_month_start, 0,
5495                                        DECODE(LEAST(TRUNC(start_date),
5496                                                     l_fiscal_month_start),
5497                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
5498                                                                                  l_fiscal_month_start),
5499                                                                         TRUNC(end_date), forecasted_responses,
5500                                                                         0
5501                                                                        ),
5502                                               0
5503                                              )
5504                                       )) +
5505 				      SUM(DECODE(GREATEST(TRUNC(start_date),
5506                                                 l_fiscal_month_start),
5507                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
5508                                                                               SYSDATE
5509                                                                             - 1),
5510                                                                       TRUNC(start_date), forecasted_responses,
5511                                                                       0
5512                                                                      ),
5513                                        0
5514                                       ))  forecasted_responses,
5515                             SUM(DECODE(TRUNC(start_date),
5516                                        l_fiscal_month_start, 0,
5517                                        DECODE(LEAST(TRUNC(start_date),
5518                                                     l_fiscal_month_start),
5519                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
5520                                                                                  l_fiscal_month_start),
5521                                                                         TRUNC(end_date), targeted_customers,
5522                                                                         0
5523                                                                        ),
5524                                               0
5525                                              )
5526                                       )) +
5527 				      SUM(DECODE(GREATEST(TRUNC(start_date),
5528                                                 l_fiscal_month_start),
5529                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
5530                                                                               SYSDATE
5531                                                                             - 1),
5532                                                                       TRUNC(start_date), targeted_customers,
5533                                                                       0
5534                                                                      ),
5535                                        0
5536                                       ))  targeted_customers,
5537                             SUM(DECODE(TRUNC(start_date),
5538                                        l_pre_fiscal_month_start, 0,
5539                                        DECODE(LEAST(TRUNC(start_date),
5540                                                     l_pre_fiscal_month_start),
5541                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
5542                                                                                  l_pre_fiscal_month_start),
5543                                                                         TRUNC(end_date), targeted_customers,
5544                                                                         0
5545                                                                        ),
5546                                               0
5547                                              )
5548                                       ))  +
5549 
5550 				 SUM(DECODE(GREATEST(TRUNC(start_date),
5551                                                 l_pre_fiscal_month_start),
5552                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
5553                                                                             l_pre_fiscal_month_end),
5554                                                                       TRUNC(start_date), targeted_customers,
5555                                                                       0
5556                                                                      ),
5557                                        0
5558                                       ))  prev_targeted_customers,
5559                             SUM(DECODE(TRUNC(start_date),
5560                                        l_fiscal_month_start, 0,
5561                                        DECODE(LEAST(TRUNC(start_date),
5562                                                     l_fiscal_month_start),
5563                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
5564                                                                                  l_fiscal_month_start),
5565                                                                         TRUNC(end_date), budget_approved,
5566                                                                         0
5567                                                                        ),
5568                                               0
5569                                              )
5570                                       )) +
5571 				      SUM(DECODE(GREATEST(TRUNC(start_date),
5572                                                 l_fiscal_month_start),
5573                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
5574                                                                               SYSDATE
5575                                                                             - 1),
5576                                                                       TRUNC(start_date), budget_approved,
5577                                                                       0
5578                                                                      ),
5579                                        0
5580                                       ))  budget_approved
5581                        FROM bim_r_camp_daily_facts a,
5582                             jtf_loc_hierarchies_b c
5583                       WHERE a.schedule_country = c.location_hierarchy_id
5584 		             and trunc(a.start_date) <=  sysdate_1
5585 			      and trunc(a.end_date) >= l_pre_fiscal_month_start
5586                    GROUP BY a.campaign_id ,c.area2_code,
5587                             a.schedule_country,
5588                             NVL(a.business_unit_id, 0),
5589                             a.schedule_activity_id
5590 ------------------------------------------------------------------------------------------------
5591                    UNION ALL
5592 ------------------------------------------------------------------------------------------------
5593                    SELECT  /*+ use_hash(A)  use_hash(C)  */
5594 		            a.campaign_id campaign_id,
5595 		            c.area2_code schedule_area2,
5596                             a.schedule_country schedule_country,
5597                             NVL(a.business_unit_id, 0) business_unit_id,
5598                             a.schedule_activity_id schedule_activity_id,
5599                             'INCREMENT' aggregate_by,
5600                             'QUARTER' display_type,
5601                             0 curr_count_value,
5602                             0 curr_started_value,
5603                             0 curr_ended_value,
5604                             0 prev_count_value,
5605                             0 prev_started_value,
5606                             0 prev_ended_value,
5607                             0 campaign_count,
5608                             SUM(DECODE(TRUNC(start_date),
5609                                        l_fiscal_qtr_start, 0,
5610                                        DECODE(LEAST(TRUNC(start_date),
5611                                                     l_fiscal_qtr_start),
5612                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
5613                                                                                  l_fiscal_qtr_start),
5614                                                                         TRUNC(end_date), forecasted_responses,
5615                                                                         0
5616                                                                        ),
5617                                               0
5618                                              )
5619                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date),
5620                                                 l_fiscal_qtr_start),
5621                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
5622                                                                               SYSDATE
5623                                                                             - 1),
5624                                                                       TRUNC(start_date), forecasted_responses,
5625                                                                       0
5626                                                                      ),
5627                                        0
5628                                       ))  forecasted_responses,
5629                             SUM(DECODE(TRUNC(start_date),
5630                                        l_fiscal_qtr_start, 0,
5631                                        DECODE(LEAST(TRUNC(start_date),
5632                                                     l_fiscal_qtr_start),
5633                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
5634                                                                                  l_fiscal_qtr_start),
5635                                                                         TRUNC(end_date), targeted_customers,
5636                                                                         0
5637                                                                        ),
5638                                               0
5639                                              )
5640                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date),
5641                                                 l_fiscal_qtr_start),
5642                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
5643                                                                               SYSDATE
5644                                                                             - 1),
5645                                                                       TRUNC(start_date), targeted_customers,
5646                                                                       0
5647                                                                      ),
5648                                        0
5649                                       )) targeted_customers,
5650                             SUM(DECODE(TRUNC(start_date),
5651                                        l_pre_fiscal_qtr_start, 0,
5652                                        DECODE(LEAST(TRUNC(start_date),
5653                                                     l_pre_fiscal_qtr_start),
5654                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
5655                                                                                  l_pre_fiscal_qtr_start),
5656                                                                         TRUNC(end_date), targeted_customers,
5657                                                                         0
5658                                                                        ),
5659                                               0
5660                                              )
5661                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date),
5662                                                 l_pre_fiscal_qtr_start),
5663                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
5664                                                                             l_pre_fiscal_qtr_end),
5665                                                                       TRUNC(start_date), targeted_customers,
5666                                                                       0
5667                                                                      ),
5668                                        0
5669                                       ))  prev_targeted_customers,
5670                             SUM(DECODE(TRUNC(start_date),
5671                                        l_fiscal_qtr_start, 0,
5672                                        DECODE(LEAST(TRUNC(start_date),
5673                                                     l_fiscal_qtr_start),
5674                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
5675                                                                                  l_fiscal_qtr_start),
5676                                                                         TRUNC(end_date), budget_approved,
5677                                                                         0
5678                                                                        ),
5679                                               0
5680                                              )
5681                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date),
5682                                                 l_fiscal_qtr_start),
5683                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
5684                                                                               SYSDATE
5685                                                                             - 1),
5686                                                                       TRUNC(start_date), budget_approved,
5687                                                                       0
5688                                                                      ),
5689                                        0
5690                                       )) budget_approved
5691                        FROM bim_r_camp_daily_facts a,
5692                             jtf_loc_hierarchies_b c
5693                       WHERE a.schedule_country = c.location_hierarchy_id
5694 		            and trunc(a.start_date) <=  sysdate_1
5695 			    and trunc(a.end_date) >= l_pre_fiscal_qtr_start
5696                    GROUP BY a.campaign_id,c.area2_code,
5697                             a.schedule_country,
5698                             NVL(a.business_unit_id, 0),
5699                             a.schedule_activity_id
5700 ------------------------------------------------------------------------------------------------
5701                    UNION ALL
5702 ------------------------------------------------------------------------------------------------
5703                    SELECT  /*+ use_hash(A)  use_hash(C)  */
5704 		            a.campaign_id campaign_id,
5705 		            c.area2_code schedule_area2,
5706                             a.schedule_country schedule_country,
5707                             NVL(a.business_unit_id, 0) business_unit_id,
5708                             a.schedule_activity_id schedule_activity_id,
5709                             'INCREMENT' aggregate_by,
5710                             'YEAR' display_type,
5711                             0 curr_count_value,
5712                             0 curr_started_value,
5713                             0 curr_ended_value,
5714                             0 prev_count_value,
5715                             0 prev_started_value,
5716                             0 prev_ended_value,
5717                             0 campaign_count,
5718                             SUM(DECODE(TRUNC(start_date),
5719                                        l_fiscal_roll_year_start, 0,
5720                                        DECODE(LEAST(TRUNC(start_date),
5721                                                     l_fiscal_roll_year_start),
5722                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
5723                                                                                  l_fiscal_roll_year_start),
5724                                                                         TRUNC(end_date), forecasted_responses,
5725                                                                         0
5726                                                                        ),
5727                                               0
5728                                              )
5729                                       ))  + SUM(DECODE(GREATEST(TRUNC(start_date),
5730                                                 l_fiscal_roll_year_start),
5731                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
5732                                                                               SYSDATE
5733                                                                             - 1),
5734                                                                       TRUNC(start_date), forecasted_responses,
5735                                                                       0
5736                                                                      ),
5737                                        0
5738                                       )) forecasted_responses,
5739                             SUM(DECODE(TRUNC(start_date),
5740                                        l_fiscal_roll_year_start, 0,
5741                                        DECODE(LEAST(TRUNC(start_date),
5742                                                     l_fiscal_roll_year_start),
5743                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
5744                                                                                  l_fiscal_roll_year_start),
5745                                                                         TRUNC(end_date), targeted_customers,
5746                                                                         0
5747                                                                        ),
5748                                               0
5749                                              )
5750                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date),
5751                                                 l_fiscal_roll_year_start),
5752                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
5753                                                                               SYSDATE
5754                                                                             - 1),
5755                                                                       TRUNC(start_date), targeted_customers,
5756                                                                       0
5757                                                                      ),
5758                                        0
5759                                       )) targeted_customers,
5760                             SUM(DECODE(TRUNC(start_date),
5761                                        l_pre_fiscal_roll_year_start, 0,
5762                                        DECODE(LEAST(TRUNC(start_date),
5763                                                     l_pre_fiscal_roll_year_start),
5764                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
5765                                                                                  l_pre_fiscal_roll_year_start),
5766                                                                         TRUNC(end_date), targeted_customers,
5767                                                                         0
5768                                                                        ),
5769                                               0
5770                                              )
5771                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date),
5772                                                 l_pre_fiscal_roll_year_start),
5773                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
5774                                                                               l_fiscal_roll_year_start
5775                                                                             - 1),
5776                                                                       TRUNC(start_date), targeted_customers,
5777                                                                       0
5778                                                                      ),
5779                                        0
5780                                       ))  prev_targeted_customers,
5781                             SUM(DECODE(TRUNC(start_date),
5782                                        l_fiscal_roll_year_start, 0,
5783                                        DECODE(LEAST(TRUNC(start_date),
5784                                                     l_fiscal_roll_year_start),
5785                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
5786                                                                                  l_fiscal_roll_year_start),
5787                                                                         TRUNC(end_date), budget_approved,
5788                                                                         0
5789                                                                        ),
5790                                               0
5791                                              )
5792                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date),
5793                                                 l_fiscal_roll_year_start),
5794                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
5795                                                                               SYSDATE
5796                                                                             - 1),
5797                                                                       TRUNC(start_date), budget_approved,
5798                                                                       0
5799                                                                      ),
5800                                        0
5801                                       )) budget_approved
5802                        FROM bim_r_camp_daily_facts a,
5803                             jtf_loc_hierarchies_b c
5804                       WHERE a.schedule_country = c.location_hierarchy_id
5805 		             and trunc(a.start_date) <=  sysdate_1
5806 			     and trunc(a.end_date) >= l_pre_fiscal_roll_year_start
5807                    GROUP BY a.campaign_id ,c.area2_code,
5808                             a.schedule_country,
5809                             NVL(a.business_unit_id, 0),
5810                             a.schedule_activity_id
5811                   )
5812          GROUP BY schedule_area2,
5813                   schedule_country,
5814                   business_unit_id,
5815                   schedule_activity_id,
5816                   aggregate_by,
5817                   display_type;
5818 
5819 
5820 				  ---------union all admin end
5821 
5822 
5823 /* END OF THE INCREMENT DATA FIRST INSERT */
5824 
5825 --commit;
5826       ams_utility_pvt.write_conc_log('BIM_CAMP_COLLECTION: INCREMENT FIRST INSERT END');
5827 /* START OF THE INCREMENT DATA SECOND INSERT */
5828       ams_utility_pvt.write_conc_log('BIM_CAMP_COLLECTION: INCREMENT SECOND INSERT START');
5829 
5830 
5831       INSERT INTO   /*+ append parallel(i,5) */
5832                 bim_r_camp_collection i
5833                   (campaign_area1,
5834                    campaign_area2,
5835                    schedule_area1,
5836                    schedule_area2,
5837                    campaign_country,
5838                    schedule_country,
5839                    business_unit_id,
5840                    resource_id,
5841                    campaign_type,
5842                    campaign_id,
5843                    schedule_activity_type,
5844                    campaign_status,
5845                    schedule_status,
5846                    aggregate_by,
5847                    display_type,
5848                    YEAR,
5849                    qtr,
5850                    MONTH,
5851                    report_type,
5852                    current_count_value,
5853                    current_started_value,
5854                    current_ended_value,
5855                    previous_count_value,
5856                    previous_started_value,
5857                    previous_ended_value,
5858                    campaign_count,
5859                    forecasted_responses,
5860                    targeted_customers,
5861                    prev_targeted_customers,
5862                    budget_approved
5863                   )
5864          SELECT  /* +INDEX_FFS(R,BIM_DBI_U2) */
5865 	          'N' campaign_area1,
5866                   'N' campaign_area2,
5867                   'N' schedule_area1,
5868                   schedule_area2,
5869                   'N' campaign_country,
5870                   schedule_country,
5871                   business_unit_id,
5872                   resource_id,
5873                   'N' campaign_type,
5874                   campaign_id,
5875                   'N' schedule_activity_type,
5876                   'N' campaign_status,
5877                   'N' schedule_status,
5878                   aggregate_by,
5879                   display_type,
5880                   'N' YEAR,
5881                   'N' qtr,
5882                   'N' MONTH,
5883                   'CAMP' report_type,
5884                   SUM(curr_count_value) current_count_value,
5885                   SUM(curr_started_value) current_started_value,
5886                   SUM(curr_ended_value) current_ended_value,
5887                   SUM(prev_count_value) previous_count_value,
5888                   SUM(prev_started_value) previous_started_value,
5889                   SUM(prev_ended_value) previous_ended_value,
5890                   SUM(campaign_count) campaign_count,
5891                   SUM(forecasted_responses) forecasted_responses,
5892                   SUM(targeted_customers) targeted_customers,
5893                   SUM(prev_targeted_customers) prev_targeted_customers,
5894                   SUM(budget_approved) budget_approved
5895              FROM (SELECT /*+ use_hash(A)    use_hash(B) use_hash(C)  */
5896 	                   c.area2_code schedule_area2,
5897                             b.city_id schedule_country,
5898                             NVL(b.business_unit_id, 0) business_unit_id,
5899                             a.campaign_id campaign_id,
5900                             'INCREMENT' aggregate_by,
5901                             'DAY' display_type,
5902                             SUM(DECODE(TRUNC(start_date_time),
5903                                        TRUNC(SYSDATE - 1), 0,
5904                                        DECODE(LEAST(TRUNC(start_date_time),
5905                                                     TRUNC(SYSDATE - 1)),
5906                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
5907                                                                                       TRUNC(  SYSDATE
5908                                                                                             - 1)),
5909                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
5910                                                                              0
5911                                                                             ),
5912                                               0
5913                                              )
5914                                       )) + SUM(DECODE(TRUNC(start_date_time),
5915                                        TRUNC(SYSDATE - 1), 1,
5916                                        0
5917                                       )) curr_count_value,
5918 --*** added curr_started_value to curr_count_value
5919  /*                           SUM(DECODE(TRUNC(start_date_time),
5920                                        TRUNC(SYSDATE - 1), 1,
5921                                        0
5922                                       )) curr_started_value,
5923                             SUM(DECODE(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
5924                                        TRUNC(SYSDATE - 1), 1,
5925                                        0
5926                                       )) curr_ended_value,
5927                             SUM(DECODE(TRUNC(start_date_time),
5928                                        TRUNC(SYSDATE - 2), 0,
5929                                        DECODE(LEAST(TRUNC(start_date_time),
5930                                                     TRUNC(SYSDATE - 2)),
5931                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
5932                                                                                       TRUNC(  SYSDATE
5933                                                                                             - 2)),
5934                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
5935                                                                              0
5936                                                                             ),
5937                                               0
5938                                              )
5939                                       )) prev_count_value,
5940                             SUM(DECODE(TRUNC(start_date_time),
5941                                        TRUNC(SYSDATE - 2), 1,
5942                                        0
5943                                       )) prev_started_value,
5944                             SUM(DECODE(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
5945                                        TRUNC(SYSDATE - 2), 1,
5946                                        0
5947                                       )) prev_ended_value,
5948 */
5949 			    0 curr_started_value,
5950 			    0 curr_ended_value,
5951 			    0 prev_count_value,
5952 			    0 prev_started_value,
5953                             0 prev_ended_value,
5954                             0 campaign_count,
5955                             0 forecasted_responses,
5956                             0 targeted_customers,
5957                             0 prev_targeted_customers,
5958                             0 budget_approved
5959                        FROM ams_campaign_schedules_b a,
5960                             jtf_loc_hierarchies_b c,
5961                             ams_campaigns_all_b b
5962                       WHERE a.status_code IN
5963                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
5964                         AND trunc(a.start_date_time) >= l_min_start_date
5965 			and trunc(a.start_date_time) <= sysdate_1
5966                         AND b.city_id = c.location_hierarchy_id
5967                         AND a.campaign_id = b.campaign_id
5968                    GROUP BY c.area2_code,
5969                             b.city_id,
5970                             NVL(b.business_unit_id, 0),
5971                             a.campaign_id
5972 ------------------------------------------------------------------------------------------------
5973                    UNION ALL
5974 ------------------------------------------------------------------------------------------------
5975                    SELECT  /*+ use_hash(A)    use_hash(B) use_hash(C)  */
5976 		           c.area2_code schedule_area2,
5977                             b.city_id schedule_country,
5978                             NVL(b.business_unit_id, 0) business_unit_id,
5979                             a.campaign_id campaign_id,
5980                             'INCREMENT' aggregate_by,
5981                             'WEEK' display_type,
5982                             SUM(DECODE(TRUNC(start_date_time),
5983                                        TRUNC(SYSDATE - 8), 0,
5984                                        DECODE(LEAST(TRUNC(start_date_time),
5985                                                     TRUNC(SYSDATE - 8)),
5986                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
5987                                                                                       TRUNC(  SYSDATE
5988                                                                                             - 8)),
5989                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
5990                                                                              0
5991                                                                             ),
5992                                               0
5993                                              )
5994                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date_time),
5995                                                 TRUNC(SYSDATE - 8)),
5996                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
5997                                                                             TRUNC(  SYSDATE
5998                                                                                   - 1)),
5999                                                                       TRUNC(start_date_time), 1,
6000                                                                       0
6001                                                                      ),
6002                                        0
6003                                       )) curr_count_value,
6004 --*** started value is added to count value
6005 /*
6006                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
6007                                                 TRUNC(SYSDATE - 8)),
6008                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
6009                                                                             TRUNC(  SYSDATE
6010                                                                                   - 1)),
6011                                                                       TRUNC(start_date_time), 1,
6012                                                                       0
6013                                                                      ),
6014                                        0
6015                                       )) curr_started_value,
6016                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
6017                                                 TRUNC(SYSDATE - 8)),
6018                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
6019                                                                             TRUNC(  SYSDATE
6020                                                                                   - 1)),
6021                                                                       trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
6022                                                                       0
6023                                                                      ),
6024                                        0
6025                                       )) curr_ended_value,
6026                             SUM(DECODE(TRUNC(start_date_time),
6027                                        TRUNC(SYSDATE - 15), 0,
6028                                        DECODE(LEAST(TRUNC(start_date_time),
6029                                                     TRUNC(SYSDATE - 15)),
6030                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
6031                                                                                       TRUNC(  SYSDATE
6032                                                                                             - 15)),
6033                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
6034                                                                              0
6035                                                                             ),
6036                                               0
6037                                              )
6038                                       )) prev_count_value,
6039                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
6040                                                 TRUNC(SYSDATE - 15)),
6041                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
6042                                                                             TRUNC(  SYSDATE
6043                                                                                   - 8)),
6044                                                                       TRUNC(start_date_time), 1,
6045                                                                       0
6046                                                                      ),
6047                                        0
6048                                       )) prev_started_value,
6049                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
6050                                                 TRUNC(SYSDATE - 15)),
6051                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
6052                                                                           TRUNC(  SYSDATE
6053                                                                                 - 8)),
6054                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
6055                                                                     0
6056                                                                    ),
6057                                        0
6058                                       )) prev_ended_value,
6059 */
6060 			    0 curr_started_value,
6061 			    0 curr_ended_value,
6062 			    0 prev_count_value,
6063 			    0 prev_started_value,
6064                             0 prev_ended_value,
6065                             0 campaign_count,
6066                             0 forecasted_responses,
6067                             0 targeted_customers,
6068                             0 prev_targeted_customers,
6069                             0 budget_approved
6070                        FROM ams_campaign_schedules_b a,
6071                             jtf_loc_hierarchies_b c,
6072                             ams_campaigns_all_b b
6073                       WHERE a.status_code IN
6074                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
6075                         AND trunc(a.start_date_time) >= l_min_start_date
6076 			and trunc(a.start_date_time) <= sysdate_1
6077                         AND b.city_id = c.location_hierarchy_id
6078                         AND a.campaign_id = b.campaign_id
6079                    GROUP BY c.area2_code,
6080                             b.city_id,
6081                             NVL(b.business_unit_id, 0),
6082                             a.campaign_id
6083 ------------------------------------------------------------------------------------------------
6084                    UNION ALL
6085 ------------------------------------------------------------------------------------------------
6086                    SELECT  /*+ use_hash(A)    use_hash(B) use_hash(C)  */
6087 		            c.area2_code schedule_area2,
6088                             b.city_id schedule_country,
6089                             NVL(b.business_unit_id, 0) business_unit_id,
6090                             a.campaign_id campaign_id,
6091                             'INCREMENT' aggregate_by,
6092                             'MONTH' display_type,
6093                             SUM(DECODE(TRUNC(start_date_time),
6094                                        l_fiscal_month_start, 0,
6095                                        DECODE(LEAST(TRUNC(start_date_time),
6096                                                     l_fiscal_month_start),
6097                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
6098                                                                                       l_fiscal_month_start),
6099                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
6100                                                                              0
6101                                                                             ),
6102                                               0
6103                                              )
6104                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date_time),
6105                                                 l_fiscal_month_start),
6106                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
6107                                                                               SYSDATE
6108                                                                             - 1),
6109                                                                       TRUNC(start_date_time), 1,
6110                                                                       0
6111                                                                      ),
6112                                        0
6113                                       ))  curr_count_value,
6114 /*
6115                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
6116                                                 l_fiscal_month_start),
6117                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
6118                                                                               SYSDATE
6119                                                                             - 1),
6120                                                                       TRUNC(start_date_time), 1,
6121                                                                       0
6122                                                                      ),
6123                                        0
6124                                       )) curr_started_value,
6125                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
6126                                                 l_fiscal_month_start),
6127                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
6128                                                                             SYSDATE
6129                                                                           - 1),
6130                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
6131                                                                     0
6132                                                                    ),
6133                                        0
6134                                       )) curr_ended_value,
6135                             SUM(DECODE(TRUNC(start_date_time),
6136                                        l_pre_fiscal_month_start, 0,
6137                                        DECODE(LEAST(TRUNC(start_date_time),
6138                                                     l_pre_fiscal_month_start),
6139                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
6140                                                                                       l_pre_fiscal_month_start),
6141                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
6142                                                                              0
6143                                                                             ),
6144                                               0
6145                                              )
6146                                       )) prev_count_value,
6147                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
6148                                                 l_pre_fiscal_month_start),
6149                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
6150                                                                             l_pre_fiscal_month_end),
6151                                                                       TRUNC(start_date_time), 1,
6152                                                                       0
6153                                                                      ),
6154                                        0
6155                                       )) prev_started_value,
6156                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
6157                                                 l_pre_fiscal_month_start),
6158                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
6159                                                                           l_pre_fiscal_month_end),
6160                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
6161                                                                     0
6162                                                                    ),
6163                                        0
6164                                       )) prev_ended_value,
6165 */
6166 			    0 curr_started_value,
6167 			    0 curr_ended_value,
6168 			    0 prev_count_value,
6169 			    0 prev_started_value,
6170                             0 prev_ended_value,
6171                             0 campaign_count,
6172                             0 forecasted_responses,
6173                             0 targeted_customers,
6174                             0 prev_targeted_customers,
6175                             0 budget_approved
6176                        FROM ams_campaign_schedules_b a,
6177                             jtf_loc_hierarchies_b c,
6178                             ams_campaigns_all_b b
6179                       WHERE a.status_code IN
6180                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
6181                         AND trunc(a.start_date_time) >= l_min_start_date
6182 			and trunc(a.start_date_time) <= sysdate_1
6183                         AND b.city_id = c.location_hierarchy_id
6184                         AND a.campaign_id = b.campaign_id
6185                    GROUP BY c.area2_code,
6186                             b.city_id,
6187                             NVL(b.business_unit_id, 0),
6188                             a.campaign_id
6189 ------------------------------------------------------------------------------------------------
6190                    UNION ALL
6191 ------------------------------------------------------------------------------------------------
6192                    SELECT  /*+ use_hash(A)    use_hash(B) use_hash(C)  */
6193 		            c.area2_code schedule_area2,
6194                             b.city_id schedule_country,
6195                             NVL(b.business_unit_id, 0) business_unit_id,
6196                             a.campaign_id campaign_id,
6197                             'INCREMENT' aggregate_by,
6198                             'QUARTER' display_type,
6199                             SUM(DECODE(TRUNC(start_date_time),
6200                                        l_fiscal_qtr_start, 0,
6201                                        DECODE(LEAST(TRUNC(start_date_time),
6202                                                     l_fiscal_qtr_start),
6203                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
6204                                                                                       l_fiscal_qtr_start),
6205                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
6206                                                                              0
6207                                                                             ),
6208                                               0
6209                                              )
6210                                       )) +  SUM(DECODE(GREATEST(TRUNC(start_date_time),
6211                                                 l_fiscal_qtr_start),
6212                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
6213                                                                               SYSDATE
6214                                                                             - 1),
6215                                                                       TRUNC(start_date_time), 1,
6216                                                                       0
6217                                                                      ),
6218                                        0
6219                                       )) curr_count_value,
6220 --***
6221 /*
6222                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
6223                                                 l_fiscal_qtr_start),
6224                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
6225                                                                               SYSDATE
6226                                                                             - 1),
6227                                                                       TRUNC(start_date_time), 1,
6228                                                                       0
6229                                                                      ),
6230                                        0
6231                                       )) curr_started_value,
6232                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
6233                                                 l_fiscal_qtr_start),
6234                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
6235                                                                             SYSDATE
6236                                                                           - 1),
6237                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
6238                                                                     0
6239                                                                    ),
6240                                        0
6241                                       )) curr_ended_value,
6242                             SUM(DECODE(TRUNC(start_date_time),
6243                                        l_pre_fiscal_qtr_start, 0,
6244                                        DECODE(LEAST(TRUNC(start_date_time),
6245                                                     l_pre_fiscal_qtr_start),
6246                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
6247                                                                                       l_pre_fiscal_qtr_start),
6248                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
6249                                                                              0
6250                                                                             ),
6251                                               0
6252                                              )
6253                                       )) prev_count_value,
6254                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
6255                                                 l_pre_fiscal_qtr_start),
6256                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
6257                                                                             l_pre_fiscal_qtr_end),
6258                                                                       TRUNC(start_date_time), 1,
6259                                                                       0
6260                                                                      ),
6261                                        0
6262                                       )) prev_started_value,
6263                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
6264                                                 l_pre_fiscal_qtr_start),
6265                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
6266                                                                           l_pre_fiscal_qtr_end),
6267                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
6268                                                                     0
6269                                                                    ),
6270                                        0
6271                                       )) prev_ended_value,
6272 */
6273 			    0 curr_started_value,
6274 			    0 curr_ended_value,
6275 			    0 prev_count_value,
6276 			    0 prev_started_value,
6277                             0 prev_ended_value,
6278                             0 campaign_count,
6279                             0 forecasted_responses,
6280                             0 targeted_customers,
6281                             0 prev_targeted_customers,
6282                             0 budget_approved
6283                        FROM ams_campaign_schedules_b a,
6284                             jtf_loc_hierarchies_b c,
6285                             ams_campaigns_all_b b
6286                       WHERE a.status_code IN
6287                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
6288                         AND trunc(a.start_date_time) >= l_min_start_date
6289 			and trunc(a.start_date_time) <= sysdate_1
6290                         AND b.city_id = c.location_hierarchy_id
6291                         AND a.campaign_id = b.campaign_id
6292                    GROUP BY c.area2_code,
6293                             b.city_id,
6294                             NVL(b.business_unit_id, 0),
6295                             a.campaign_id
6296 ------------------------------------------------------------------------------------------------
6297                    UNION ALL
6298 ------------------------------------------------------------------------------------------------
6299                    SELECT   /*+ use_hash(A)    use_hash(B) use_hash(C)  */
6300 		            c.area2_code schedule_area2,
6301                             b.city_id schedule_country,
6302                             NVL(b.business_unit_id, 0) business_unit_id,
6303                             a.campaign_id campaign_id,
6304                             'INCREMENT' aggregate_by,
6305                             'YEAR' display_type,
6306                             SUM(DECODE(TRUNC(start_date_time),
6307                                        l_fiscal_roll_year_start, 0,
6308                                        DECODE(LEAST(TRUNC(start_date_time),
6309                                                     l_fiscal_roll_year_start),
6310                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
6311                                                                                       l_fiscal_roll_year_start),
6312                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
6313                                                                              0
6314                                                                             ),
6315                                               0
6316                                              )
6317                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date_time),
6318                                                 l_fiscal_roll_year_start),
6319                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
6320                                                                               SYSDATE
6321                                                                             - 1),
6322                                                                       TRUNC(start_date_time), 1,
6323                                                                       0
6324                                                                      ),
6325                                        0
6326                                       ))  curr_count_value,
6327 --***
6328 /*
6329                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
6330                                                 l_fiscal_roll_year_start),
6331                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
6332                                                                               SYSDATE
6333                                                                             - 1),
6334                                                                       TRUNC(start_date_time), 1,
6335                                                                       0
6336                                                                      ),
6337                                        0
6338                                       )) curr_started_value,
6339                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
6340                                                 l_fiscal_roll_year_start),
6341                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
6342                                                                             SYSDATE
6343                                                                           - 1),
6344                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
6345                                                                     0
6346                                                                    ),
6347                                        0
6348                                       )) curr_ended_value,
6349                             SUM(DECODE(TRUNC(start_date_time),
6350                                        l_pre_fiscal_roll_year_start, 0,
6351                                        DECODE(LEAST(TRUNC(start_date_time),
6352                                                     l_pre_fiscal_roll_year_start),
6353                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
6354                                                                                       l_pre_fiscal_roll_year_start),
6355                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
6356                                                                              0
6357                                                                             ),
6358                                               0
6359                                              )
6360                                       )) prev_count_value,
6361                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
6362                                                 l_pre_fiscal_roll_year_start),
6363                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
6364                                                                               l_fiscal_roll_year_start
6365                                                                             - 1),
6366                                                                       TRUNC(start_date_time), 1,
6367                                                                       0
6368                                                                      ),
6369                                        0
6370                                       )) prev_started_value,
6371                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
6372                                                 l_pre_fiscal_roll_year_start),
6373                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
6374                                                                             l_fiscal_roll_year_start
6375                                                                           - 1),
6376                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
6377                                                                     0
6378                                                                    ),
6379                                        0
6380                                       )) prev_ended_value,
6381 */
6382 			    0 curr_started_value,
6383 			    0 curr_ended_value,
6384 			    0 prev_count_value,
6385 			    0 prev_started_value,
6386                             0 prev_ended_value,
6387                             0 campaign_count,
6388                             0 forecasted_responses,
6389                             0 targeted_customers,
6390                             0 prev_targeted_customers,
6391                             0 budget_approved
6392                        FROM ams_campaign_schedules_b a,
6393                             jtf_loc_hierarchies_b c,
6394                             ams_campaigns_all_b b
6395                       WHERE a.status_code IN
6396                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
6397                         AND trunc(a.start_date_time) >= l_min_start_date
6398 			and trunc(a.start_date_time) <= sysdate_1
6399                         AND b.city_id = c.location_hierarchy_id
6400                         AND a.campaign_id = b.campaign_id
6401                    GROUP BY c.area2_code,
6402                             b.city_id,
6403                             NVL(b.business_unit_id, 0),
6404                             a.campaign_id
6405 -----------------------------------------------------------------------------------------------
6406 ---
6407 ---START OF METRICS CODE FOR FRESP, TCUST, BAPPROVED
6408 ---
6409 -----------------------------------------------------------------------------------------------
6410 ------------------------------------------------------------------------------------------------
6411                    UNION ALL
6412 ------------------------------------------------------------------------------------------------
6413                    SELECT  /*+ use_hash(A)   use_hash(C)  */
6414 		           c.area2_code schedule_area2,
6415                             a.campaign_country schedule_country,
6416                             NVL(a.business_unit_id, 0) business_unit_id,
6417                             a.campaign_id campaign_id,
6418                             'INCREMENT' aggregate_by,
6419                             'DAY' display_type,
6420                             0 curr_count_value,
6421                             0 curr_started_value,
6422                             0 curr_ended_value,
6423                             0 prev_count_value,
6424                             0 prev_started_value,
6425                             0 prev_ended_value,
6426                             0 campaign_count,
6427                             SUM(DECODE(TRUNC(start_date),
6428                                        TRUNC(SYSDATE - 1), 0,
6429                                        DECODE(LEAST(TRUNC(start_date),
6430                                                     TRUNC(SYSDATE - 1)),
6431                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
6432                                                                                  TRUNC(  SYSDATE
6433                                                                                        - 1)),
6434                                                                         TRUNC(end_date), forecasted_responses,
6435                                                                         0
6436                                                                        ),
6437                                               0
6438                                              )
6439                                       )) + SUM(DECODE(TRUNC(start_date),
6440                                        TRUNC(SYSDATE - 1), forecasted_responses,
6441                                        0
6442                                       ))  forecasted_responses,
6443                             SUM(DECODE(TRUNC(start_date),
6444                                        TRUNC(SYSDATE - 1), 0,
6445                                        DECODE(LEAST(TRUNC(start_date),
6446                                                     TRUNC(SYSDATE - 1)),
6447                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
6448                                                                                  TRUNC(  SYSDATE
6449                                                                                        - 1)),
6450                                                                         TRUNC(end_date), targeted_customers,
6451                                                                         0
6452                                                                        ),
6453                                               0
6454                                              )
6455                                       )) + SUM(DECODE(TRUNC(start_date),
6456                                        TRUNC(SYSDATE - 1), targeted_customers,
6457                                        0
6458                                       ))   targeted_customers,
6459 --***
6460 /*
6461                             SUM(DECODE(TRUNC(start_date),
6462                                        TRUNC(SYSDATE - 2), 0,
6463                                        DECODE(LEAST(TRUNC(start_date),
6464                                                     TRUNC(SYSDATE - 2)),
6465                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
6466                                                                                  TRUNC(  SYSDATE
6467                                                                                        - 2)),
6468                                                                         TRUNC(end_date), targeted_customers,
6469                                                                         0
6470                                                                        ),
6471                                               0
6472                                              )
6473                                       )) prev_targeted_customers,
6474 */
6475                            0 prev_targeted_customers,
6476                             SUM(DECODE(TRUNC(start_date),
6477                                        TRUNC(SYSDATE - 1), 0,
6478                                        DECODE(LEAST(TRUNC(start_date),
6479                                                     TRUNC(SYSDATE - 1)),
6480                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
6481                                                                                  TRUNC(  SYSDATE
6482                                                                                        - 1)),
6483                                                                         TRUNC(end_date), budget_approved,
6484                                                                         0
6485                                                                        ),
6486                                               0
6487                                              )
6488                                       )) + SUM(DECODE(TRUNC(start_date),
6489                                        TRUNC(SYSDATE - 1), budget_approved,
6490                                        0
6491                                       )) budget_approved
6492 
6493                        FROM bim_r_camp_daily_facts a,
6494                             jtf_loc_hierarchies_b c
6495                       WHERE a.campaign_country = c.location_hierarchy_id
6496 		            and trunc(a.start_date) <=  sysdate_1
6497                    GROUP BY c.area2_code,
6498                             a.campaign_country,
6499                             NVL(a.business_unit_id, 0),
6500                             a.campaign_id
6501 ------------------------------------------------------------------------------------------------
6502                    UNION ALL
6503 ------------------------------------------------------------------------------------------------
6504                    SELECT   /*+ use_hash(A)   use_hash(C)  */
6505 		           c.area2_code schedule_area2,
6506                             a.campaign_country schedule_country,
6507                             NVL(a.business_unit_id, 0) business_unit_id,
6508                             a.campaign_id campaign_id,
6509                             'INCREMENT' aggregate_by,
6510                             'WEEK' display_type,
6511                             0 curr_count_value,
6512                             0 curr_started_value,
6513                             0 curr_ended_value,
6514                             0 prev_count_value,
6515                             0 prev_started_value,
6516                             0 prev_ended_value,
6517                             0 campaign_count,
6518                             SUM(DECODE(TRUNC(start_date),
6519                                        TRUNC(SYSDATE - 8), 0,
6520                                        DECODE(LEAST(TRUNC(start_date),
6521                                                     TRUNC(SYSDATE - 8)),
6522                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
6523                                                                                  TRUNC(  SYSDATE
6524                                                                                        - 8)),
6525                                                                         TRUNC(end_date), forecasted_responses,
6526                                                                         0
6527                                                                        ),
6528                                               0
6529                                              )
6530                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date),
6531                                                 TRUNC(SYSDATE - 8)),
6532                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
6533                                                                             TRUNC(  SYSDATE
6534                                                                                   - 1)),
6535                                                                       TRUNC(start_date), forecasted_responses,
6536                                                                       0
6537                                                                      )))  forecasted_responses,
6538                             SUM(DECODE(TRUNC(start_date),
6539                                        TRUNC(SYSDATE - 8), 0,
6540                                        DECODE(LEAST(TRUNC(start_date),
6541                                                     TRUNC(SYSDATE - 8)),
6542                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
6543                                                                                  TRUNC(  SYSDATE
6544                                                                                        - 8)),
6545                                                                         TRUNC(end_date), targeted_customers,
6546                                                                         0
6547                                                                        ),
6548                                               0
6549                                              )
6550                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date),
6551                                                 TRUNC(SYSDATE - 8)),
6552                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
6553                                                                             TRUNC(  SYSDATE
6554                                                                                   - 1)),
6555                                                                       TRUNC(start_date), targeted_customers,
6556                                                                       0
6557                                                                      )))  targeted_customers,
6558 --***
6559 /*
6560                             SUM(DECODE(TRUNC(start_date),
6561                                        TRUNC(SYSDATE - 15), 0,
6562                                        DECODE(LEAST(TRUNC(start_date),
6563                                                     TRUNC(SYSDATE - 15)),
6564                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
6565                                                                                  TRUNC(  SYSDATE
6566                                                                                        - 15)),
6567                                                                         TRUNC(end_date), targeted_customers,
6568                                                                         0
6569                                                                        ),
6570                                               0
6571                                              )
6572                                       )) prev_targeted_customers,
6573 */
6574                                 0 prev_targeted_customers,
6575                             SUM(DECODE(TRUNC(start_date),
6576                                        TRUNC(SYSDATE - 8), 0,
6577                                        DECODE(LEAST(TRUNC(start_date),
6578                                                     TRUNC(SYSDATE - 8)),
6579                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
6580                                                                                  TRUNC(  SYSDATE
6581                                                                                        - 8)),
6582                                                                         TRUNC(end_date), budget_approved,
6583                                                                         0
6584                                                                        ),
6585                                               0
6586                                              )
6587                                       )) +  SUM(DECODE(GREATEST(TRUNC(start_date),
6588                                                 TRUNC(SYSDATE - 8)),
6589                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
6590                                                                             TRUNC(  SYSDATE
6591                                                                                   - 1)),
6592                                                                       TRUNC(start_date), budget_approved,
6593                                                                       0
6594                                                                      ))) budget_approved
6595                        FROM bim_r_camp_daily_facts a,
6596                             jtf_loc_hierarchies_b c
6597                       WHERE a.campaign_country = c.location_hierarchy_id
6598 		            and trunc(a.start_date) <=  sysdate_1
6599                    GROUP BY c.area2_code,
6600                             a.campaign_country,
6601                             NVL(a.business_unit_id, 0),
6602                             a.campaign_id
6603 ------------------------------------------------------------------------------------------------
6604                    UNION ALL
6605 ------------------------------------------------------------------------------------------------
6606                    SELECT   /*+ use_hash(A)   use_hash(C)  */
6607 		            c.area2_code schedule_area2,
6608                             a.campaign_country schedule_country,
6609                             NVL(a.business_unit_id, 0) business_unit_id,
6610                             a.campaign_id campaign_id,
6611                             'INCREMENT' aggregate_by,
6612                             'MONTH' display_type,
6613                             0 curr_count_value,
6614                             0 curr_started_value,
6615                             0 curr_ended_value,
6616                             0 prev_count_value,
6617                             0 prev_started_value,
6618                             0 prev_ended_value,
6619                             0 campaign_count,
6620                             SUM(DECODE(TRUNC(start_date),
6621                                        l_fiscal_month_start, 0,
6622                                        DECODE(LEAST(TRUNC(start_date),
6623                                                     l_fiscal_month_start),
6624                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
6625                                                                                  l_fiscal_month_start),
6626                                                                         TRUNC(end_date), forecasted_responses,
6627                                                                         0
6628                                                                        ),
6629                                               0
6630                                              )
6631                                       )) +
6632 				      SUM(DECODE(GREATEST(TRUNC(start_date),
6633                                                 l_fiscal_month_start),
6634                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
6635                                                                               SYSDATE
6636                                                                             - 1),
6637                                                                       TRUNC(start_date), forecasted_responses,
6638                                                                       0
6639                                                                      ),
6640                                        0
6641                                       ))  forecasted_responses,
6642                             SUM(DECODE(TRUNC(start_date),
6643                                        l_fiscal_month_start, 0,
6644                                        DECODE(LEAST(TRUNC(start_date),
6645                                                     l_fiscal_month_start),
6646                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
6647                                                                                  l_fiscal_month_start),
6648                                                                         TRUNC(end_date), targeted_customers,
6649                                                                         0
6650                                                                        ),
6651                                               0
6652                                              )
6653                                       )) +
6654 				      SUM(DECODE(GREATEST(TRUNC(start_date),
6655                                                 l_fiscal_month_start),
6656                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
6657                                                                               SYSDATE
6658                                                                             - 1),
6659                                                                       TRUNC(start_date), targeted_customers,
6660                                                                       0
6661                                                                      ),
6662                                        0
6663                                       )) targeted_customers,
6664 --***
6665 /*
6666                             SUM(DECODE(TRUNC(start_date),
6667                                        l_pre_fiscal_month_start, 0,
6668                                        DECODE(LEAST(TRUNC(start_date),
6669                                                     l_pre_fiscal_month_start),
6670                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
6671                                                                                  l_pre_fiscal_month_start),
6672                                                                         TRUNC(end_date), targeted_customers,
6673                                                                         0
6674                                                                        ),
6675                                               0
6676                                              )
6677                                       )) prev_targeted_customers,
6678 */
6679                            0 prev_targeted_customers,
6680                             SUM(DECODE(TRUNC(start_date),
6681                                        l_fiscal_month_start, 0,
6682                                        DECODE(LEAST(TRUNC(start_date),
6683                                                     l_fiscal_month_start),
6684                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
6685                                                                                  l_fiscal_month_start),
6686                                                                         TRUNC(end_date), budget_approved,
6687                                                                         0
6688                                                                        ),
6689                                               0
6690                                              )
6691                                       )) +
6692 				      SUM(DECODE(GREATEST(TRUNC(start_date),
6693                                                 l_fiscal_month_start),
6694                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
6695                                                                               SYSDATE
6696                                                                             - 1),
6697                                                                       TRUNC(start_date), budget_approved,
6698                                                                       0
6699                                                                      ),
6700                                        0
6701                                       ))  budget_approved
6702 
6703                        FROM bim_r_camp_daily_facts a,
6704                             jtf_loc_hierarchies_b c
6705                       WHERE a.campaign_country = c.location_hierarchy_id
6706 		            and trunc(a.start_date) <=  sysdate_1
6707                    GROUP BY c.area2_code,
6708                             a.campaign_country,
6709                             NVL(a.business_unit_id, 0),
6710                             a.campaign_id
6711 ------------------------------------------------------------------------------------------------
6712                    UNION ALL
6713 ------------------------------------------------------------------------------------------------
6714                    SELECT    /*+ use_hash(A)   use_hash(C)  */
6715 		            c.area2_code schedule_area2,
6716                             a.campaign_country schedule_country,
6717                             NVL(a.business_unit_id, 0) business_unit_id,
6718                             a.campaign_id campaign_id,
6719                             'INCREMENT' aggregate_by,
6720                             'QUARTER' display_type,
6721                             0 curr_count_value,
6722                             0 curr_started_value,
6723                             0 curr_ended_value,
6724                             0 prev_count_value,
6725                             0 prev_started_value,
6726                             0 prev_ended_value,
6727                             0 campaign_count,
6728                             SUM(DECODE(TRUNC(start_date),
6729                                        l_fiscal_qtr_start, 0,
6730                                        DECODE(LEAST(TRUNC(start_date),
6731                                                     l_fiscal_qtr_start),
6732                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
6733                                                                                  l_fiscal_qtr_start),
6734                                                                         TRUNC(end_date), forecasted_responses,
6735                                                                         0
6736                                                                        ),
6737                                               0
6738                                              )
6739                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date),
6740                                                 l_fiscal_qtr_start),
6741                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
6742                                                                               SYSDATE
6743                                                                             - 1),
6744                                                                       TRUNC(start_date), forecasted_responses,
6745                                                                       0
6746                                                                      ),
6747                                        0
6748                                       ))  forecasted_responses,
6749                             SUM(DECODE(TRUNC(start_date),
6750                                        l_fiscal_qtr_start, 0,
6751                                        DECODE(LEAST(TRUNC(start_date),
6752                                                     l_fiscal_qtr_start),
6753                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
6754                                                                                  l_fiscal_qtr_start),
6755                                                                         TRUNC(end_date), targeted_customers,
6756                                                                         0
6757                                                                        ),
6758                                               0
6759                                              )
6760                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date),
6761                                                 l_fiscal_qtr_start),
6762                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
6763                                                                               SYSDATE
6764                                                                             - 1),
6765                                                                       TRUNC(start_date), targeted_customers,
6766                                                                       0
6767                                                                      ),
6768                                        0
6769                                       ))  targeted_customers,
6770 --***
6771 /*
6772                             SUM(DECODE(TRUNC(start_date),
6773                                        l_pre_fiscal_qtr_start, 0,
6774                                        DECODE(LEAST(TRUNC(start_date),
6775                                                     l_pre_fiscal_qtr_start),
6776                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
6777                                                                                  l_pre_fiscal_qtr_start),
6778                                                                         TRUNC(end_date), targeted_customers,
6779                                                                         0
6780                                                                        ),
6781                                               0
6782                                              )
6783                                       )) prev_targeted_customers,
6784 */
6785                            0 prev_targeted_customers,
6786                             SUM(DECODE(TRUNC(start_date),
6787                                        l_fiscal_qtr_start, 0,
6788                                        DECODE(LEAST(TRUNC(start_date),
6789                                                     l_fiscal_qtr_start),
6790                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
6791                                                                                  l_fiscal_qtr_start),
6792                                                                         TRUNC(end_date), budget_approved,
6793                                                                         0
6794                                                                        ),
6795                                               0
6796                                              )
6797                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date),
6798                                                 l_fiscal_qtr_start),
6799                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
6800                                                                               SYSDATE
6801                                                                             - 1),
6802                                                                       TRUNC(start_date), budget_approved,
6803                                                                       0
6804                                                                      ),
6805                                        0
6806                                       )) budget_approved
6807 
6808                        FROM bim_r_camp_daily_facts a,
6809                             jtf_loc_hierarchies_b c
6810                       WHERE a.campaign_country = c.location_hierarchy_id
6811 		            and trunc(a.start_date) <=  sysdate_1
6812                    GROUP BY c.area2_code,
6813                             a.campaign_country,
6814                             NVL(a.business_unit_id, 0),
6815                             a.campaign_id
6816 ------------------------------------------------------------------------------------------------
6817                    UNION ALL
6818 ------------------------------------------------------------------------------------------------
6819                    SELECT   /*+ use_hash(A)   use_hash(C)  */
6820 		            c.area2_code schedule_area2,
6821                             a.campaign_country schedule_country,
6822                             NVL(a.business_unit_id, 0) business_unit_id,
6823                             a.campaign_id campaign_id,
6824                             'INCREMENT' aggregate_by,
6825                             'YEAR' display_type,
6826                             0 curr_count_value,
6827                             0 curr_started_value,
6828                             0 curr_ended_value,
6829                             0 prev_count_value,
6830                             0 prev_started_value,
6831                             0 prev_ended_value,
6832                             0 campaign_count,
6833                             SUM(DECODE(TRUNC(start_date),
6834                                        l_fiscal_roll_year_start, 0,
6835                                        DECODE(LEAST(TRUNC(start_date),
6836                                                     l_fiscal_roll_year_start),
6837                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
6838                                                                                  l_fiscal_roll_year_start),
6839                                                                         TRUNC(end_date), forecasted_responses,
6840                                                                         0
6841                                                                        ),
6842                                               0
6843                                              )
6844                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date),
6845                                                 l_fiscal_roll_year_start),
6846                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
6847                                                                               SYSDATE
6848                                                                             - 1),
6849                                                                       TRUNC(start_date), forecasted_responses,
6850                                                                       0
6851                                                                      ),
6852                                        0
6853                                       ))  forecasted_responses,
6854                             SUM(DECODE(TRUNC(start_date),
6855                                        l_fiscal_roll_year_start, 0,
6856                                        DECODE(LEAST(TRUNC(start_date),
6857                                                     l_fiscal_roll_year_start),
6858                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
6859                                                                                  l_fiscal_roll_year_start),
6860                                                                         TRUNC(end_date), targeted_customers,
6861                                                                         0
6862                                                                        ),
6863                                               0
6864                                              )
6865                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date),
6866                                                 l_fiscal_roll_year_start),
6867                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
6868                                                                               SYSDATE
6869                                                                             - 1),
6870                                                                       TRUNC(start_date), targeted_customers,
6871                                                                       0
6872                                                                      ),
6873                                        0
6874                                       ))  targeted_customers,
6875 --***
6876 /*
6877                             SUM(DECODE(TRUNC(start_date),
6878                                        l_pre_fiscal_roll_year_start, 0,
6879                                        DECODE(LEAST(TRUNC(start_date),
6880                                                     l_pre_fiscal_roll_year_start),
6881                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
6882                                                                                  l_pre_fiscal_roll_year_start),
6883                                                                         TRUNC(end_date), targeted_customers,
6884                                                                         0
6885                                                                        ),
6886                                               0
6887                                              )
6888                                       )) prev_targeted_customers,
6889 */
6890                            0 prev_targeted_customers,
6891                             SUM(DECODE(TRUNC(start_date),
6892                                        l_fiscal_roll_year_start, 0,
6893                                        DECODE(LEAST(TRUNC(start_date),
6894                                                     l_fiscal_roll_year_start),
6895                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
6896                                                                                  l_fiscal_roll_year_start),
6897                                                                         TRUNC(end_date), budget_approved,
6898                                                                         0
6899                                                                        ),
6900                                               0
6901                                              )
6902                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date),
6903                                                 l_fiscal_roll_year_start),
6904                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
6905                                                                               SYSDATE
6906                                                                             - 1),
6907                                                                       TRUNC(start_date), budget_approved,
6908                                                                       0
6909                                                                      ),
6910                                        0
6911                                       )) budget_approved
6912 
6913                        FROM bim_r_camp_daily_facts a,
6914                             jtf_loc_hierarchies_b c
6915                       WHERE a.campaign_country = c.location_hierarchy_id
6916 		            and trunc(a.start_date) <=  sysdate_1
6917                    GROUP BY c.area2_code,
6918                             a.campaign_country,
6919                             NVL(a.business_unit_id, 0),
6920                             a.campaign_id
6921                   ),ams_act_access_denorm R
6922 			    where campaign_id=r.object_id
6923 			    and r.object_type='CAMP'
6924          GROUP BY schedule_area2,
6925                   schedule_country,
6926                   business_unit_id,
6927                   resource_id,
6928                   campaign_id,
6929                   aggregate_by,
6930                   display_type
6931          HAVING   SUM(curr_count_value) > 0
6932                OR SUM(curr_started_value) > 0
6933                OR SUM(curr_ended_value) > 0
6934                OR SUM(prev_count_value) > 0
6935                OR SUM(prev_started_value) > 0
6936                OR SUM(prev_ended_value) > 0
6937                OR SUM(targeted_customers) > 0
6938                OR SUM(prev_targeted_customers) > 0
6939                OR SUM(forecasted_responses) > 0
6940                OR SUM(budget_approved) <> 0
6941 
6942 			     ---------- for admin
6943 				  union all
6944 				  -------------
6945 
6946 SELECT
6947 	          'N' campaign_area1,
6948                   'N' campaign_area2,
6949                   'N' schedule_area1,
6950                   schedule_area2,
6951                   'N' campaign_country,
6952                   schedule_country,
6953                   business_unit_id,
6954                   -1 resource_id,
6955                   'N' campaign_type,
6956                   campaign_id,
6957                   'N' schedule_activity_type,
6958                   'N' campaign_status,
6959                   'N' schedule_status,
6960                   aggregate_by,
6961                   display_type,
6962                   'N' YEAR,
6963                   'N' qtr,
6964                   'N' MONTH,
6965                   'CAMP' report_type,
6966                   SUM(curr_count_value) current_count_value,
6967                   SUM(curr_started_value) current_started_value,
6968                   SUM(curr_ended_value) current_ended_value,
6969                   SUM(prev_count_value) previous_count_value,
6970                   SUM(prev_started_value) previous_started_value,
6971                   SUM(prev_ended_value) previous_ended_value,
6972                   SUM(campaign_count) campaign_count,
6973                   SUM(forecasted_responses) forecasted_responses,
6974                   SUM(targeted_customers) targeted_customers,
6975                   SUM(prev_targeted_customers) prev_targeted_customers,
6976                   SUM(budget_approved) budget_approved
6977              FROM (SELECT /*+ use_hash(A)    use_hash(B) use_hash(C)  */
6978 	                   c.area2_code schedule_area2,
6979                             b.city_id schedule_country,
6980                             NVL(b.business_unit_id, 0) business_unit_id,
6981                             a.campaign_id campaign_id,
6982                             'INCREMENT' aggregate_by,
6983                             'DAY' display_type,
6984                             SUM(DECODE(TRUNC(start_date_time),
6985                                        TRUNC(SYSDATE - 1), 0,
6986                                        DECODE(LEAST(TRUNC(start_date_time),
6987                                                     TRUNC(SYSDATE - 1)),
6988                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
6989                                                                                       TRUNC(  SYSDATE
6990                                                                                             - 1)),
6991                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
6992                                                                              0
6993                                                                             ),
6994                                               0
6995                                              )
6996                                       )) + SUM(DECODE(TRUNC(start_date_time),
6997                                        TRUNC(SYSDATE - 1), 1,
6998                                        0
6999                                       )) curr_count_value,
7000 --*** added curr_started_value to curr_count_value
7001  /*                           SUM(DECODE(TRUNC(start_date_time),
7002                                        TRUNC(SYSDATE - 1), 1,
7003                                        0
7004                                       )) curr_started_value,
7005                             SUM(DECODE(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
7006                                        TRUNC(SYSDATE - 1), 1,
7007                                        0
7008                                       )) curr_ended_value,
7009                             SUM(DECODE(TRUNC(start_date_time),
7010                                        TRUNC(SYSDATE - 2), 0,
7011                                        DECODE(LEAST(TRUNC(start_date_time),
7012                                                     TRUNC(SYSDATE - 2)),
7013                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
7014                                                                                       TRUNC(  SYSDATE
7015                                                                                             - 2)),
7016                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
7017                                                                              0
7018                                                                             ),
7019                                               0
7020                                              )
7021                                       )) prev_count_value,
7022                             SUM(DECODE(TRUNC(start_date_time),
7023                                        TRUNC(SYSDATE - 2), 1,
7024                                        0
7025                                       )) prev_started_value,
7026                             SUM(DECODE(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
7027                                        TRUNC(SYSDATE - 2), 1,
7028                                        0
7029                                       )) prev_ended_value,
7030 */
7031 			    0 curr_started_value,
7032 			    0 curr_ended_value,
7033 			    0 prev_count_value,
7034 			    0 prev_started_value,
7035                             0 prev_ended_value,
7036                             0 campaign_count,
7037                             0 forecasted_responses,
7038                             0 targeted_customers,
7039                             0 prev_targeted_customers,
7040                             0 budget_approved
7041                        FROM ams_campaign_schedules_b a,
7042                             jtf_loc_hierarchies_b c,
7043                             ams_campaigns_all_b b
7044                       WHERE a.status_code IN
7045                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
7046                         AND trunc(a.start_date_time) >= l_min_start_date
7047 			and trunc(a.start_date_time) <= sysdate_1
7048                         AND b.city_id = c.location_hierarchy_id
7049                         AND a.campaign_id = b.campaign_id
7050                    GROUP BY c.area2_code,
7051                             b.city_id,
7052                             NVL(b.business_unit_id, 0),
7053                             a.campaign_id
7054 ------------------------------------------------------------------------------------------------
7055                    UNION ALL
7056 ------------------------------------------------------------------------------------------------
7057                    SELECT  /*+ use_hash(A)    use_hash(B) use_hash(C)  */
7058 		           c.area2_code schedule_area2,
7059                             b.city_id schedule_country,
7060                             NVL(b.business_unit_id, 0) business_unit_id,
7061                             a.campaign_id campaign_id,
7062                             'INCREMENT' aggregate_by,
7063                             'WEEK' display_type,
7064                             SUM(DECODE(TRUNC(start_date_time),
7065                                        TRUNC(SYSDATE - 8), 0,
7066                                        DECODE(LEAST(TRUNC(start_date_time),
7067                                                     TRUNC(SYSDATE - 8)),
7068                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
7069                                                                                       TRUNC(  SYSDATE
7070                                                                                             - 8)),
7071                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
7072                                                                              0
7073                                                                             ),
7074                                               0
7075                                              )
7076                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date_time),
7077                                                 TRUNC(SYSDATE - 8)),
7078                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
7079                                                                             TRUNC(  SYSDATE
7080                                                                                   - 1)),
7081                                                                       TRUNC(start_date_time), 1,
7082                                                                       0
7083                                                                      ),
7084                                        0
7085                                       )) curr_count_value,
7086 --*** started value is added to count value
7087 /*
7088                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
7089                                                 TRUNC(SYSDATE - 8)),
7090                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
7091                                                                             TRUNC(  SYSDATE
7092                                                                                   - 1)),
7093                                                                       TRUNC(start_date_time), 1,
7094                                                                       0
7095                                                                      ),
7096                                        0
7097                                       )) curr_started_value,
7098                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
7099                                                 TRUNC(SYSDATE - 8)),
7100                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
7101                                                                             TRUNC(  SYSDATE
7102                                                                                   - 1)),
7103                                                                       trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
7104                                                                       0
7105                                                                      ),
7106                                        0
7107                                       )) curr_ended_value,
7108                             SUM(DECODE(TRUNC(start_date_time),
7109                                        TRUNC(SYSDATE - 15), 0,
7110                                        DECODE(LEAST(TRUNC(start_date_time),
7111                                                     TRUNC(SYSDATE - 15)),
7112                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
7113                                                                                       TRUNC(  SYSDATE
7114                                                                                             - 15)),
7115                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
7116                                                                              0
7117                                                                             ),
7118                                               0
7119                                              )
7120                                       )) prev_count_value,
7121                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
7122                                                 TRUNC(SYSDATE - 15)),
7123                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
7124                                                                             TRUNC(  SYSDATE
7125                                                                                   - 8)),
7126                                                                       TRUNC(start_date_time), 1,
7127                                                                       0
7128                                                                      ),
7129                                        0
7130                                       )) prev_started_value,
7131                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
7132                                                 TRUNC(SYSDATE - 15)),
7133                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
7134                                                                           TRUNC(  SYSDATE
7135                                                                                 - 8)),
7136                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
7137                                                                     0
7138                                                                    ),
7139                                        0
7140                                       )) prev_ended_value,
7141 */
7142 			    0 curr_started_value,
7143 			    0 curr_ended_value,
7144 			    0 prev_count_value,
7145 			    0 prev_started_value,
7146                             0 prev_ended_value,
7147                             0 campaign_count,
7148                             0 forecasted_responses,
7149                             0 targeted_customers,
7150                             0 prev_targeted_customers,
7151                             0 budget_approved
7152                        FROM ams_campaign_schedules_b a,
7153                             jtf_loc_hierarchies_b c,
7154                             ams_campaigns_all_b b
7155                       WHERE a.status_code IN
7156                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
7157                         AND trunc(a.start_date_time) >= l_min_start_date
7158 			and trunc(a.start_date_time) <= sysdate_1
7159                         AND b.city_id = c.location_hierarchy_id
7160                         AND a.campaign_id = b.campaign_id
7161                    GROUP BY c.area2_code,
7162                             b.city_id,
7163                             NVL(b.business_unit_id, 0),
7164                             a.campaign_id
7165 ------------------------------------------------------------------------------------------------
7166                    UNION ALL
7167 ------------------------------------------------------------------------------------------------
7168                    SELECT  /*+ use_hash(A)    use_hash(B) use_hash(C)  */
7169 		            c.area2_code schedule_area2,
7170                             b.city_id schedule_country,
7171                             NVL(b.business_unit_id, 0) business_unit_id,
7172                             a.campaign_id campaign_id,
7173                             'INCREMENT' aggregate_by,
7174                             'MONTH' display_type,
7175                             SUM(DECODE(TRUNC(start_date_time),
7176                                        l_fiscal_month_start, 0,
7177                                        DECODE(LEAST(TRUNC(start_date_time),
7178                                                     l_fiscal_month_start),
7179                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
7180                                                                                       l_fiscal_month_start),
7181                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
7182                                                                              0
7183                                                                             ),
7184                                               0
7185                                              )
7186                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date_time),
7187                                                 l_fiscal_month_start),
7188                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
7189                                                                               SYSDATE
7190                                                                             - 1),
7191                                                                       TRUNC(start_date_time), 1,
7192                                                                       0
7193                                                                      ),
7194                                        0
7195                                       ))  curr_count_value,
7196 /*
7197                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
7198                                                 l_fiscal_month_start),
7199                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
7200                                                                               SYSDATE
7201                                                                             - 1),
7202                                                                       TRUNC(start_date_time), 1,
7203                                                                       0
7204                                                                      ),
7205                                        0
7206                                       )) curr_started_value,
7207                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
7208                                                 l_fiscal_month_start),
7209                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
7210                                                                             SYSDATE
7211                                                                           - 1),
7212                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
7213                                                                     0
7214                                                                    ),
7215                                        0
7216                                       )) curr_ended_value,
7217                             SUM(DECODE(TRUNC(start_date_time),
7218                                        l_pre_fiscal_month_start, 0,
7219                                        DECODE(LEAST(TRUNC(start_date_time),
7220                                                     l_pre_fiscal_month_start),
7221                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
7222                                                                                       l_pre_fiscal_month_start),
7223                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
7224                                                                              0
7225                                                                             ),
7226                                               0
7227                                              )
7228                                       )) prev_count_value,
7229                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
7230                                                 l_pre_fiscal_month_start),
7231                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
7232                                                                             l_pre_fiscal_month_end),
7233                                                                       TRUNC(start_date_time), 1,
7234                                                                       0
7235                                                                      ),
7236                                        0
7237                                       )) prev_started_value,
7238                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
7239                                                 l_pre_fiscal_month_start),
7240                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
7241                                                                           l_pre_fiscal_month_end),
7242                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
7243                                                                     0
7244                                                                    ),
7245                                        0
7246                                       )) prev_ended_value,
7247 */
7248 			    0 curr_started_value,
7249 			    0 curr_ended_value,
7250 			    0 prev_count_value,
7251 			    0 prev_started_value,
7252                             0 prev_ended_value,
7253                             0 campaign_count,
7254                             0 forecasted_responses,
7255                             0 targeted_customers,
7256                             0 prev_targeted_customers,
7257                             0 budget_approved
7258                        FROM ams_campaign_schedules_b a,
7259                             jtf_loc_hierarchies_b c,
7260                             ams_campaigns_all_b b
7261                       WHERE a.status_code IN
7262                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
7263                         AND trunc(a.start_date_time) >= l_min_start_date
7264 			and trunc(a.start_date_time) <= sysdate_1
7265                         AND b.city_id = c.location_hierarchy_id
7266                         AND a.campaign_id = b.campaign_id
7267                    GROUP BY c.area2_code,
7268                             b.city_id,
7269                             NVL(b.business_unit_id, 0),
7270                             a.campaign_id
7271 ------------------------------------------------------------------------------------------------
7272                    UNION ALL
7273 ------------------------------------------------------------------------------------------------
7274                    SELECT  /*+ use_hash(A)    use_hash(B) use_hash(C)  */
7275 		            c.area2_code schedule_area2,
7276                             b.city_id schedule_country,
7277                             NVL(b.business_unit_id, 0) business_unit_id,
7278                             a.campaign_id campaign_id,
7279                             'INCREMENT' aggregate_by,
7280                             'QUARTER' display_type,
7281                             SUM(DECODE(TRUNC(start_date_time),
7282                                        l_fiscal_qtr_start, 0,
7283                                        DECODE(LEAST(TRUNC(start_date_time),
7284                                                     l_fiscal_qtr_start),
7285                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
7286                                                                                       l_fiscal_qtr_start),
7287                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
7288                                                                              0
7289                                                                             ),
7290                                               0
7291                                              )
7292                                       )) +  SUM(DECODE(GREATEST(TRUNC(start_date_time),
7293                                                 l_fiscal_qtr_start),
7294                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
7295                                                                               SYSDATE
7296                                                                             - 1),
7297                                                                       TRUNC(start_date_time), 1,
7298                                                                       0
7299                                                                      ),
7300                                        0
7301                                       )) curr_count_value,
7302 --***
7303 /*
7304                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
7305                                                 l_fiscal_qtr_start),
7306                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
7307                                                                               SYSDATE
7308                                                                             - 1),
7309                                                                       TRUNC(start_date_time), 1,
7310                                                                       0
7311                                                                      ),
7312                                        0
7313                                       )) curr_started_value,
7314                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
7315                                                 l_fiscal_qtr_start),
7316                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
7317                                                                             SYSDATE
7318                                                                           - 1),
7319                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
7320                                                                     0
7321                                                                    ),
7322                                        0
7323                                       )) curr_ended_value,
7324                             SUM(DECODE(TRUNC(start_date_time),
7325                                        l_pre_fiscal_qtr_start, 0,
7326                                        DECODE(LEAST(TRUNC(start_date_time),
7327                                                     l_pre_fiscal_qtr_start),
7328                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
7329                                                                                       l_pre_fiscal_qtr_start),
7330                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
7331                                                                              0
7332                                                                             ),
7333                                               0
7334                                              )
7335                                       )) prev_count_value,
7336                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
7337                                                 l_pre_fiscal_qtr_start),
7338                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
7339                                                                             l_pre_fiscal_qtr_end),
7340                                                                       TRUNC(start_date_time), 1,
7341                                                                       0
7342                                                                      ),
7343                                        0
7344                                       )) prev_started_value,
7345                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
7346                                                 l_pre_fiscal_qtr_start),
7347                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
7348                                                                           l_pre_fiscal_qtr_end),
7349                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
7350                                                                     0
7351                                                                    ),
7352                                        0
7353                                       )) prev_ended_value,
7354 */
7355 			    0 curr_started_value,
7356 			    0 curr_ended_value,
7357 			    0 prev_count_value,
7358 			    0 prev_started_value,
7359                             0 prev_ended_value,
7360                             0 campaign_count,
7361                             0 forecasted_responses,
7362                             0 targeted_customers,
7363                             0 prev_targeted_customers,
7364                             0 budget_approved
7365                        FROM ams_campaign_schedules_b a,
7366                             jtf_loc_hierarchies_b c,
7367                             ams_campaigns_all_b b
7368                       WHERE a.status_code IN
7369                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
7370                         AND trunc(a.start_date_time) >= l_min_start_date
7371 			and trunc(a.start_date_time) <= sysdate_1
7372                         AND b.city_id = c.location_hierarchy_id
7373                         AND a.campaign_id = b.campaign_id
7374                    GROUP BY c.area2_code,
7375                             b.city_id,
7376                             NVL(b.business_unit_id, 0),
7377                             a.campaign_id
7378 ------------------------------------------------------------------------------------------------
7379                    UNION ALL
7380 ------------------------------------------------------------------------------------------------
7381                    SELECT   /*+ use_hash(A)    use_hash(B) use_hash(C)  */
7382 		            c.area2_code schedule_area2,
7383                             b.city_id schedule_country,
7384                             NVL(b.business_unit_id, 0) business_unit_id,
7385                             a.campaign_id campaign_id,
7386                             'INCREMENT' aggregate_by,
7387                             'YEAR' display_type,
7388                             SUM(DECODE(TRUNC(start_date_time),
7389                                        l_fiscal_roll_year_start, 0,
7390                                        DECODE(LEAST(TRUNC(start_date_time),
7391                                                     l_fiscal_roll_year_start),
7392                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
7393                                                                                       l_fiscal_roll_year_start),
7394                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
7395                                                                              0
7396                                                                             ),
7397                                               0
7398                                              )
7399                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date_time),
7400                                                 l_fiscal_roll_year_start),
7401                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
7402                                                                               SYSDATE
7403                                                                             - 1),
7404                                                                       TRUNC(start_date_time), 1,
7405                                                                       0
7406                                                                      ),
7407                                        0
7408                                       ))  curr_count_value,
7409 --***
7410 /*
7411                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
7412                                                 l_fiscal_roll_year_start),
7413                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
7414                                                                               SYSDATE
7415                                                                             - 1),
7416                                                                       TRUNC(start_date_time), 1,
7417                                                                       0
7418                                                                      ),
7419                                        0
7420                                       )) curr_started_value,
7421                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
7422                                                 l_fiscal_roll_year_start),
7423                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
7424                                                                             SYSDATE
7425                                                                           - 1),
7426                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
7427                                                                     0
7428                                                                    ),
7429                                        0
7430                                       )) curr_ended_value,
7431                             SUM(DECODE(TRUNC(start_date_time),
7432                                        l_pre_fiscal_roll_year_start, 0,
7433                                        DECODE(LEAST(TRUNC(start_date_time),
7434                                                     l_pre_fiscal_roll_year_start),
7435                                               TRUNC(start_date_time), DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
7436                                                                                       l_pre_fiscal_roll_year_start),
7437                                                                              trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
7438                                                                              0
7439                                                                             ),
7440                                               0
7441                                              )
7442                                       )) prev_count_value,
7443                             SUM(DECODE(GREATEST(TRUNC(start_date_time),
7444                                                 l_pre_fiscal_roll_year_start),
7445                                        TRUNC(start_date_time), DECODE(LEAST(TRUNC(start_date_time),
7446                                                                               l_fiscal_roll_year_start
7447                                                                             - 1),
7448                                                                       TRUNC(start_date_time), 1,
7449                                                                       0
7450                                                                      ),
7451                                        0
7452                                       )) prev_started_value,
7453                             SUM(DECODE(GREATEST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
7454                                                 l_pre_fiscal_roll_year_start),
7455                                        trunc(NVL(a.end_date_time, b.actual_exec_end_date)), DECODE(LEAST(trunc(NVL(a.end_date_time, b.actual_exec_end_date)),
7456                                                                             l_fiscal_roll_year_start
7457                                                                           - 1),
7458                                                                     trunc(NVL(a.end_date_time, b.actual_exec_end_date)), 1,
7459                                                                     0
7460                                                                    ),
7461                                        0
7462                                       )) prev_ended_value,
7463 */
7464 			    0 curr_started_value,
7465 			    0 curr_ended_value,
7466 			    0 prev_count_value,
7467 			    0 prev_started_value,
7468                             0 prev_ended_value,
7469                             0 campaign_count,
7470                             0 forecasted_responses,
7471                             0 targeted_customers,
7472                             0 prev_targeted_customers,
7473                             0 budget_approved
7474                        FROM ams_campaign_schedules_b a,
7475                             jtf_loc_hierarchies_b c,
7476                             ams_campaigns_all_b b
7477                       WHERE a.status_code IN
7478                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
7479                         AND trunc(a.start_date_time) >= l_min_start_date
7480 			and trunc(a.start_date_time) <= sysdate_1
7481                         AND b.city_id = c.location_hierarchy_id
7482                         AND a.campaign_id = b.campaign_id
7483                    GROUP BY c.area2_code,
7484                             b.city_id,
7485                             NVL(b.business_unit_id, 0),
7486                             a.campaign_id
7487 -----------------------------------------------------------------------------------------------
7488 ---
7489 ---START OF METRICS CODE FOR FRESP, TCUST, BAPPROVED
7490 ---
7491 -----------------------------------------------------------------------------------------------
7492 ------------------------------------------------------------------------------------------------
7493                    UNION ALL
7494 ------------------------------------------------------------------------------------------------
7495                    SELECT  /*+ use_hash(A)   use_hash(C)  */
7496 		           c.area2_code schedule_area2,
7497                             a.campaign_country schedule_country,
7498                             NVL(a.business_unit_id, 0) business_unit_id,
7499                             a.campaign_id campaign_id,
7500                             'INCREMENT' aggregate_by,
7501                             'DAY' display_type,
7502                             0 curr_count_value,
7503                             0 curr_started_value,
7504                             0 curr_ended_value,
7505                             0 prev_count_value,
7506                             0 prev_started_value,
7507                             0 prev_ended_value,
7508                             0 campaign_count,
7509                             SUM(DECODE(TRUNC(start_date),
7510                                        TRUNC(SYSDATE - 1), 0,
7511                                        DECODE(LEAST(TRUNC(start_date),
7512                                                     TRUNC(SYSDATE - 1)),
7513                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
7514                                                                                  TRUNC(  SYSDATE
7515                                                                                        - 1)),
7516                                                                         TRUNC(end_date), forecasted_responses,
7517                                                                         0
7518                                                                        ),
7519                                               0
7520                                              )
7521                                       )) + SUM(DECODE(TRUNC(start_date),
7522                                        TRUNC(SYSDATE - 1), forecasted_responses,
7523                                        0
7524                                       ))  forecasted_responses,
7525                             SUM(DECODE(TRUNC(start_date),
7526                                        TRUNC(SYSDATE - 1), 0,
7527                                        DECODE(LEAST(TRUNC(start_date),
7528                                                     TRUNC(SYSDATE - 1)),
7529                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
7530                                                                                  TRUNC(  SYSDATE
7531                                                                                        - 1)),
7532                                                                         TRUNC(end_date), targeted_customers,
7533                                                                         0
7534                                                                        ),
7535                                               0
7536                                              )
7537                                       )) + SUM(DECODE(TRUNC(start_date),
7538                                        TRUNC(SYSDATE - 1), targeted_customers,
7539                                        0
7540                                       ))   targeted_customers,
7541 --***
7542 /*
7543                             SUM(DECODE(TRUNC(start_date),
7544                                        TRUNC(SYSDATE - 2), 0,
7545                                        DECODE(LEAST(TRUNC(start_date),
7546                                                     TRUNC(SYSDATE - 2)),
7547                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
7548                                                                                  TRUNC(  SYSDATE
7549                                                                                        - 2)),
7550                                                                         TRUNC(end_date), targeted_customers,
7551                                                                         0
7552                                                                        ),
7553                                               0
7554                                              )
7555                                       )) prev_targeted_customers,
7556 */
7557                              0 prev_targeted_customers,
7558                             SUM(DECODE(TRUNC(start_date),
7559                                        TRUNC(SYSDATE - 1), 0,
7560                                        DECODE(LEAST(TRUNC(start_date),
7561                                                     TRUNC(SYSDATE - 1)),
7562                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
7563                                                                                  TRUNC(  SYSDATE
7564                                                                                        - 1)),
7565                                                                         TRUNC(end_date), budget_approved,
7566                                                                         0
7567                                                                        ),
7568                                               0
7569                                              )
7570                                       )) + SUM(DECODE(TRUNC(start_date),
7571                                        TRUNC(SYSDATE - 1), budget_approved,
7572                                        0
7573                                       )) budget_approved
7574 
7575                        FROM bim_r_camp_daily_facts a,
7576                             jtf_loc_hierarchies_b c
7577                       WHERE a.campaign_country = c.location_hierarchy_id
7578 		       and trunc(a.start_date) <=  sysdate_1
7579                    GROUP BY c.area2_code,
7580                             a.campaign_country,
7581                             NVL(a.business_unit_id, 0),
7582                             a.campaign_id
7583 ------------------------------------------------------------------------------------------------
7584                    UNION ALL
7585 ------------------------------------------------------------------------------------------------
7586                    SELECT   /*+ use_hash(A)   use_hash(C)  */
7587 		           c.area2_code schedule_area2,
7588                             a.campaign_country schedule_country,
7589                             NVL(a.business_unit_id, 0) business_unit_id,
7590                             a.campaign_id campaign_id,
7591                             'INCREMENT' aggregate_by,
7592                             'WEEK' display_type,
7593                             0 curr_count_value,
7594                             0 curr_started_value,
7595                             0 curr_ended_value,
7596                             0 prev_count_value,
7597                             0 prev_started_value,
7598                             0 prev_ended_value,
7599                             0 campaign_count,
7600                             SUM(DECODE(TRUNC(start_date),
7601                                        TRUNC(SYSDATE - 8), 0,
7602                                        DECODE(LEAST(TRUNC(start_date),
7603                                                     TRUNC(SYSDATE - 8)),
7604                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
7605                                                                                  TRUNC(  SYSDATE
7606                                                                                        - 8)),
7607                                                                         TRUNC(end_date), forecasted_responses,
7608                                                                         0
7609                                                                        ),
7610                                               0
7611                                              )
7612                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date),
7613                                                 TRUNC(SYSDATE - 8)),
7614                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
7615                                                                             TRUNC(  SYSDATE
7616                                                                                   - 1)),
7617                                                                       TRUNC(start_date), forecasted_responses,
7618                                                                       0
7619                                                                      )))  forecasted_responses,
7620                             SUM(DECODE(TRUNC(start_date),
7621                                        TRUNC(SYSDATE - 8), 0,
7622                                        DECODE(LEAST(TRUNC(start_date),
7623                                                     TRUNC(SYSDATE - 8)),
7624                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
7625                                                                                  TRUNC(  SYSDATE
7626                                                                                        - 8)),
7627                                                                         TRUNC(end_date), targeted_customers,
7628                                                                         0
7629                                                                        ),
7630                                               0
7631                                              )
7632                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date),
7633                                                 TRUNC(SYSDATE - 8)),
7634                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
7635                                                                             TRUNC(  SYSDATE
7636                                                                                   - 1)),
7637                                                                       TRUNC(start_date), targeted_customers,
7638                                                                       0
7639                                                                      )))  targeted_customers,
7640 --***
7641 /*
7642                             SUM(DECODE(TRUNC(start_date),
7643                                        TRUNC(SYSDATE - 15), 0,
7644                                        DECODE(LEAST(TRUNC(start_date),
7645                                                     TRUNC(SYSDATE - 15)),
7646                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
7647                                                                                  TRUNC(  SYSDATE
7648                                                                                        - 15)),
7649                                                                         TRUNC(end_date), targeted_customers,
7650                                                                         0
7651                                                                        ),
7652                                               0
7653                                              )
7654                                       )) prev_targeted_customers,
7655 */
7656                          0  prev_targeted_customers,
7657                             SUM(DECODE(TRUNC(start_date),
7658                                        TRUNC(SYSDATE - 8), 0,
7659                                        DECODE(LEAST(TRUNC(start_date),
7660                                                     TRUNC(SYSDATE - 8)),
7661                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
7662                                                                                  TRUNC(  SYSDATE
7663                                                                                        - 8)),
7664                                                                         TRUNC(end_date), budget_approved,
7665                                                                         0
7666                                                                        ),
7667                                               0
7668                                              )
7669                                       )) +  SUM(DECODE(GREATEST(TRUNC(start_date),
7670                                                 TRUNC(SYSDATE - 8)),
7671                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
7672                                                                             TRUNC(  SYSDATE
7673                                                                                   - 1)),
7674                                                                       TRUNC(start_date), budget_approved,
7675                                                                       0
7676                                                                      ))) budget_approved
7677                        FROM bim_r_camp_daily_facts a,
7678                             jtf_loc_hierarchies_b c
7679                       WHERE a.campaign_country = c.location_hierarchy_id
7680 		       and trunc(a.start_date) <=  sysdate_1
7681                    GROUP BY c.area2_code,
7682                             a.campaign_country,
7683                             NVL(a.business_unit_id, 0),
7684                             a.campaign_id
7685 ------------------------------------------------------------------------------------------------
7686                    UNION ALL
7687 ------------------------------------------------------------------------------------------------
7688                    SELECT   /*+ use_hash(A)   use_hash(C)  */
7689 		            c.area2_code schedule_area2,
7690                             a.campaign_country schedule_country,
7691                             NVL(a.business_unit_id, 0) business_unit_id,
7692                             a.campaign_id campaign_id,
7693                             'INCREMENT' aggregate_by,
7694                             'MONTH' display_type,
7695                             0 curr_count_value,
7696                             0 curr_started_value,
7697                             0 curr_ended_value,
7698                             0 prev_count_value,
7699                             0 prev_started_value,
7700                             0 prev_ended_value,
7701                             0 campaign_count,
7702                             SUM(DECODE(TRUNC(start_date),
7703                                        l_fiscal_month_start, 0,
7704                                        DECODE(LEAST(TRUNC(start_date),
7705                                                     l_fiscal_month_start),
7706                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
7707                                                                                  l_fiscal_month_start),
7708                                                                         TRUNC(end_date), forecasted_responses,
7709                                                                         0
7710                                                                        ),
7711                                               0
7712                                              )
7713                                       )) +
7714 				      SUM(DECODE(GREATEST(TRUNC(start_date),
7715                                                 l_fiscal_month_start),
7716                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
7717                                                                               SYSDATE
7718                                                                             - 1),
7719                                                                       TRUNC(start_date), forecasted_responses,
7720                                                                       0
7721                                                                      ),
7722                                        0
7723                                       ))  forecasted_responses,
7724                             SUM(DECODE(TRUNC(start_date),
7725                                        l_fiscal_month_start, 0,
7726                                        DECODE(LEAST(TRUNC(start_date),
7727                                                     l_fiscal_month_start),
7728                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
7729                                                                                  l_fiscal_month_start),
7730                                                                         TRUNC(end_date), targeted_customers,
7731                                                                         0
7732                                                                        ),
7733                                               0
7734                                              )
7735                                       )) +
7736 				      SUM(DECODE(GREATEST(TRUNC(start_date),
7737                                                 l_fiscal_month_start),
7738                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
7739                                                                               SYSDATE
7740                                                                             - 1),
7741                                                                       TRUNC(start_date), targeted_customers,
7742                                                                       0
7743                                                                      ),
7744                                        0
7745                                       )) targeted_customers,
7746 --***
7747 /*
7748                             SUM(DECODE(TRUNC(start_date),
7749                                        l_pre_fiscal_month_start, 0,
7750                                        DECODE(LEAST(TRUNC(start_date),
7751                                                     l_pre_fiscal_month_start),
7752                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
7753                                                                                  l_pre_fiscal_month_start),
7754                                                                         TRUNC(end_date), targeted_customers,
7755                                                                         0
7756                                                                        ),
7757                                               0
7758                                              )
7759                                       )) prev_targeted_customers,
7760 */
7761                            0 prev_targeted_customers,
7762                             SUM(DECODE(TRUNC(start_date),
7763                                        l_fiscal_month_start, 0,
7764                                        DECODE(LEAST(TRUNC(start_date),
7765                                                     l_fiscal_month_start),
7766                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
7767                                                                                  l_fiscal_month_start),
7768                                                                         TRUNC(end_date), budget_approved,
7769                                                                         0
7770                                                                        ),
7771                                               0
7772                                              )
7773                                       )) +
7774 				      SUM(DECODE(GREATEST(TRUNC(start_date),
7775                                                 l_fiscal_month_start),
7776                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
7777                                                                               SYSDATE
7778                                                                             - 1),
7779                                                                       TRUNC(start_date), budget_approved,
7780                                                                       0
7781                                                                      ),
7782                                        0
7783                                       ))  budget_approved
7784 
7785                        FROM bim_r_camp_daily_facts a,
7786                             jtf_loc_hierarchies_b c
7787                       WHERE a.campaign_country = c.location_hierarchy_id
7788 		       and trunc(a.start_date) <=  sysdate_1
7789                    GROUP BY c.area2_code,
7790                             a.campaign_country,
7791                             NVL(a.business_unit_id, 0),
7792                             a.campaign_id
7793 ------------------------------------------------------------------------------------------------
7794                    UNION ALL
7795 ------------------------------------------------------------------------------------------------
7796                    SELECT    /*+ use_hash(A)   use_hash(C)  */
7797 		            c.area2_code schedule_area2,
7798                             a.campaign_country schedule_country,
7799                             NVL(a.business_unit_id, 0) business_unit_id,
7800                             a.campaign_id campaign_id,
7801                             'INCREMENT' aggregate_by,
7802                             'QUARTER' display_type,
7803                             0 curr_count_value,
7804                             0 curr_started_value,
7805                             0 curr_ended_value,
7806                             0 prev_count_value,
7807                             0 prev_started_value,
7808                             0 prev_ended_value,
7809                             0 campaign_count,
7810                             SUM(DECODE(TRUNC(start_date),
7811                                        l_fiscal_qtr_start, 0,
7812                                        DECODE(LEAST(TRUNC(start_date),
7813                                                     l_fiscal_qtr_start),
7814                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
7815                                                                                  l_fiscal_qtr_start),
7816                                                                         TRUNC(end_date), forecasted_responses,
7817                                                                         0
7818                                                                        ),
7819                                               0
7820                                              )
7821                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date),
7822                                                 l_fiscal_qtr_start),
7823                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
7824                                                                               SYSDATE
7825                                                                             - 1),
7826                                                                       TRUNC(start_date), forecasted_responses,
7827                                                                       0
7828                                                                      ),
7829                                        0
7830                                       ))  forecasted_responses,
7831                             SUM(DECODE(TRUNC(start_date),
7832                                        l_fiscal_qtr_start, 0,
7833                                        DECODE(LEAST(TRUNC(start_date),
7834                                                     l_fiscal_qtr_start),
7835                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
7836                                                                                  l_fiscal_qtr_start),
7837                                                                         TRUNC(end_date), targeted_customers,
7838                                                                         0
7839                                                                        ),
7840                                               0
7841                                              )
7842                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date),
7843                                                 l_fiscal_qtr_start),
7844                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
7845                                                                               SYSDATE
7846                                                                             - 1),
7847                                                                       TRUNC(start_date), targeted_customers,
7848                                                                       0
7849                                                                      ),
7850                                        0
7851                                       ))  targeted_customers,
7852 --***
7853 /*
7854                             SUM(DECODE(TRUNC(start_date),
7855                                        l_pre_fiscal_qtr_start, 0,
7856                                        DECODE(LEAST(TRUNC(start_date),
7857                                                     l_pre_fiscal_qtr_start),
7858                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
7859                                                                                  l_pre_fiscal_qtr_start),
7860                                                                         TRUNC(end_date), targeted_customers,
7861                                                                         0
7862                                                                        ),
7863                                               0
7864                                              )
7865                                       )) prev_targeted_customers,
7866 */
7867                             0 prev_targeted_customers,
7868                             SUM(DECODE(TRUNC(start_date),
7869                                        l_fiscal_qtr_start, 0,
7870                                        DECODE(LEAST(TRUNC(start_date),
7871                                                     l_fiscal_qtr_start),
7872                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
7873                                                                                  l_fiscal_qtr_start),
7874                                                                         TRUNC(end_date), budget_approved,
7875                                                                         0
7876                                                                        ),
7877                                               0
7878                                              )
7879                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date),
7880                                                 l_fiscal_qtr_start),
7881                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
7882                                                                               SYSDATE
7883                                                                             - 1),
7884                                                                       TRUNC(start_date), budget_approved,
7885                                                                       0
7886                                                                      ),
7887                                        0
7888                                       )) budget_approved
7889 
7890                        FROM bim_r_camp_daily_facts a,
7891                             jtf_loc_hierarchies_b c
7892                       WHERE a.campaign_country = c.location_hierarchy_id
7893 		       and trunc(a.start_date) <=  sysdate_1
7894                    GROUP BY c.area2_code,
7895                             a.campaign_country,
7896                             NVL(a.business_unit_id, 0),
7897                             a.campaign_id
7898 ------------------------------------------------------------------------------------------------
7899                    UNION ALL
7900 ------------------------------------------------------------------------------------------------
7901                    SELECT   /*+ use_hash(A)   use_hash(C)  */
7902 		            c.area2_code schedule_area2,
7903                             a.campaign_country schedule_country,
7904                             NVL(a.business_unit_id, 0) business_unit_id,
7905                             a.campaign_id campaign_id,
7906                             'INCREMENT' aggregate_by,
7907                             'YEAR' display_type,
7908                             0 curr_count_value,
7909                             0 curr_started_value,
7910                             0 curr_ended_value,
7911                             0 prev_count_value,
7912                             0 prev_started_value,
7913                             0 prev_ended_value,
7914                             0 campaign_count,
7915                             SUM(DECODE(TRUNC(start_date),
7916                                        l_fiscal_roll_year_start, 0,
7917                                        DECODE(LEAST(TRUNC(start_date),
7918                                                     l_fiscal_roll_year_start),
7919                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
7920                                                                                  l_fiscal_roll_year_start),
7921                                                                         TRUNC(end_date), forecasted_responses,
7922                                                                         0
7923                                                                        ),
7924                                               0
7925                                              )
7926                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date),
7927                                                 l_fiscal_roll_year_start),
7928                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
7929                                                                               SYSDATE
7930                                                                             - 1),
7931                                                                       TRUNC(start_date), forecasted_responses,
7932                                                                       0
7933                                                                      ),
7934                                        0
7935                                       ))  forecasted_responses,
7936                             SUM(DECODE(TRUNC(start_date),
7937                                        l_fiscal_roll_year_start, 0,
7938                                        DECODE(LEAST(TRUNC(start_date),
7939                                                     l_fiscal_roll_year_start),
7940                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
7941                                                                                  l_fiscal_roll_year_start),
7942                                                                         TRUNC(end_date), targeted_customers,
7943                                                                         0
7944                                                                        ),
7945                                               0
7946                                              )
7947                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date),
7948                                                 l_fiscal_roll_year_start),
7949                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
7950                                                                               SYSDATE
7951                                                                             - 1),
7952                                                                       TRUNC(start_date), targeted_customers,
7953                                                                       0
7954                                                                      ),
7955                                        0
7956                                       ))  targeted_customers,
7957 --***
7958 /*
7959                             SUM(DECODE(TRUNC(start_date),
7960                                        l_pre_fiscal_roll_year_start, 0,
7961                                        DECODE(LEAST(TRUNC(start_date),
7962                                                     l_pre_fiscal_roll_year_start),
7963                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
7964                                                                                  l_pre_fiscal_roll_year_start),
7965                                                                         TRUNC(end_date), targeted_customers,
7966                                                                         0
7967                                                                        ),
7968                                               0
7969                                              )
7970                                       )) prev_targeted_customers,
7971 */
7972                             0 prev_targeted_customers,
7973                             SUM(DECODE(TRUNC(start_date),
7974                                        l_fiscal_roll_year_start, 0,
7975                                        DECODE(LEAST(TRUNC(start_date),
7976                                                     l_fiscal_roll_year_start),
7977                                               TRUNC(start_date), DECODE(GREATEST(TRUNC(end_date),
7978                                                                                  l_fiscal_roll_year_start),
7979                                                                         TRUNC(end_date), budget_approved,
7980                                                                         0
7981                                                                        ),
7982                                               0
7983                                              )
7984                                       )) + SUM(DECODE(GREATEST(TRUNC(start_date),
7985                                                 l_fiscal_roll_year_start),
7986                                        TRUNC(start_date), DECODE(LEAST(TRUNC(start_date),
7987                                                                               SYSDATE
7988                                                                             - 1),
7989                                                                       TRUNC(start_date), budget_approved,
7990                                                                       0
7991                                                                      ),
7992                                        0
7993                                       )) budget_approved
7994 
7995                        FROM bim_r_camp_daily_facts a,
7996                             jtf_loc_hierarchies_b c
7997                       WHERE a.campaign_country = c.location_hierarchy_id
7998 		       and trunc(a.start_date) <=  sysdate_1
7999                    GROUP BY c.area2_code,
8000                             a.campaign_country,
8001                             NVL(a.business_unit_id, 0),
8002                             a.campaign_id
8003                   )
8004          GROUP BY schedule_area2,
8005                   schedule_country,
8006                   business_unit_id,
8007    --***               resource_id,
8008                   campaign_id,
8009                   aggregate_by,
8010                   display_type
8011          HAVING   SUM(curr_count_value) > 0
8012                OR SUM(curr_started_value) > 0
8013                OR SUM(curr_ended_value) > 0
8014                OR SUM(prev_count_value) > 0
8015                OR SUM(prev_started_value) > 0
8016                OR SUM(prev_ended_value) > 0
8017                OR SUM(targeted_customers) > 0
8018                OR SUM(prev_targeted_customers) > 0
8019                OR SUM(forecasted_responses) > 0
8020                OR SUM(budget_approved) <> 0;
8021 
8022 				  ---------union all admin end
8023 
8024 
8025 	       --commit;
8026 
8027 /* END OF THE INCREMENT DATA SECOND INSERT */
8028 /* START OF THE MONTH, QUARTER ,YEAR DATA INSERT */
8029       ams_utility_pvt.write_conc_log('BIM_CAMP_COLLECTION: SUMMARY ACT MONTH INSERT START');
8030       INSERT INTO  /*+ append parallel(i,5) */
8031             bim_r_camp_collection i
8032                   (campaign_area1,
8033                    campaign_area2,
8034                    schedule_area1,
8035                    schedule_area2,
8036                    campaign_country,
8037                    schedule_country,
8038                    business_unit_id,
8039                    resource_id,
8040                    campaign_type,
8041                    schedule_activity,
8042                    schedule_activity_type,
8043                    campaign_status,
8044                    schedule_status,
8045                    aggregate_by,
8046                    display_type,
8047                    YEAR,
8048                    qtr,
8049                    MONTH,
8050                    report_type,
8051                    current_count_value,
8052                    current_started_value,
8053                    current_ended_value,
8054                    previous_count_value,
8055                    previous_started_value,
8056                    previous_ended_value,
8057                    campaign_count,
8058                    forecasted_responses,
8059                    targeted_customers,
8060                    prev_targeted_customers,
8061                    budget_approved
8062                   )
8063          SELECT   /* +INDEX_FFS(R,BIM_DBI_U2) */
8064 	         'N' campaign_area1,
8065                   'N' campaign_area2,
8066                   'N' schedule_area1,
8067                   schedule_area2,
8068                   'N' campaign_country,
8069                   schedule_country,
8070                   business_unit_id,
8071                   resource_id,
8072                   'N' campaign_type,
8073                   schedule_activity_id,
8074                   'N' schedule_activity_type,
8075                   'N' campaign_status,
8076                   'N' schedule_status,
8077                   aggregate_by,
8078                   display_type,
8079                   fiscal_year YEAR,
8080                   fiscal_qtr qtr,
8081                   fiscal_month MONTH,
8082                   'ACT' report_type,
8083                   SUM(curr_count) current_count_value,
8084                   SUM(curr_started) current_started_value,
8085                   SUM(curr_ended) current_ended_value,
8086                   SUM(pre_count) previous_count_value,
8087                   SUM(pre_started) previous_started_value,
8088                   SUM(pre_ended) previous_ended_value,
8089                   SUM(campaign_count) campaign_count,
8090                   SUM(forecasted_responses) forecasted_responses,
8091                   SUM(targeted_customers) targeted_customers,
8092                   SUM(prev_targeted_customers) prev_targeted_customers,
8093                   SUM(budget_approved) budget_approved
8094              FROM (
8095 -- FOR THE PERIOD TYPE MONTH
8096                    SELECT    /*+ use_hash(a)   use_hash(b)  use_hash(c) use_hash(d) */
8097 		             a.campaign_id campaign_id,
8098                              c.area2_code schedule_area2,
8099                              a.country_id schedule_country,
8100                              NVL(d.business_unit_id, 0) business_unit_id,
8101                              DECODE(a.activity_type_code,
8102                                     'EVENTS', -9999,
8103                                     a.activity_id
8104                                    ) schedule_activity_id,
8105                              'MONTH' aggregate_by,
8106                              'Z' display_type,
8107                              b.fiscal_year fiscal_year,
8108                              b.fiscal_qtr fiscal_qtr,
8109                              b.fiscal_month fiscal_month,
8110                              COUNT(a.schedule_id) curr_count,
8111                              0 pre_count,
8112                              0 curr_started,
8113                              0 pre_started,
8114                              0 curr_ended,
8115                              0 pre_ended,
8116                              0 campaign_count,
8117                              0 forecasted_responses,
8118                              0 targeted_customers,
8119                              0 prev_targeted_customers,
8120                              0 budget_approved
8121                        FROM ams_campaign_schedules_b a,
8122                             bim_dates b,
8123                             jtf_loc_hierarchies_b c,
8124                             ams_campaigns_all_b d
8125                       WHERE a.status_code IN
8126                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
8127               --***
8128 	                AND (trunc(a.start_date_time) < b.fiscal_month_start
8129 			     or
8130 			     trunc(a.start_date_time) BETWEEN b.fiscal_month_start
8131                                                   AND b.fiscal_month_end
8132 			     )
8133                         AND a.country_id = c.location_hierarchy_id
8134                         AND a.campaign_id = d.campaign_id
8135                         AND b.period_type = 'MONTH'
8136                         AND trunc(a.start_date_time) >= l_min_start_date
8137                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date)) >=
8138                                                           b.fiscal_month_start
8139                    GROUP BY a.campaign_id,
8140 		            c.area2_code,
8141                             a.country_id,
8142                             NVL(d.business_unit_id, 0),
8143                             DECODE(a.activity_type_code,
8144                                    'EVENTS', -9999,
8145                                    a.activity_id
8146                                   ),
8147                             b.fiscal_year,
8148                             b.fiscal_qtr,
8149                             b.fiscal_month
8150 ------------------------------------------------------------------------------------------
8151                    UNION ALL
8152 ------------------------------------------------------------------------------------------
8153                    SELECT    /*+ use_hash(a)  use_hash(b)  use_hash(c) use_hash(d) */
8154 		             a.campaign_id campaign_id,
8155                              c.area2_code schedule_area2,
8156                              a.country_id schedule_country,
8157                              NVL(d.business_unit_id, 0) business_unit_id,
8158                              DECODE(a.activity_type_code,
8159                                     'EVENTS', -9999,
8160                                     a.activity_id
8161                                    ) schedule_activity_id,
8162                              'MONTH' aggregate_by,
8163                              'Z' display_type,
8164                              b.fiscal_year fiscal_year,
8165                              b.fiscal_qtr fiscal_qtr,
8166                              b.fiscal_month fiscal_month,
8167                              0 curr_count,
8168                              COUNT(a.schedule_id) pre_count,
8169                              0 curr_started,
8170                              0 pre_started,
8171                              0 curr_ended,
8172                              0 pre_ended,
8173                              0 campaign_count,
8174                              0 forecasted_responses,
8175                              0 targeted_customers,
8176                              0 prev_targeted_customers,
8177                              0 budget_approved
8178                        FROM ams_campaign_schedules_b a,
8179                             bim_dates b,
8180                             jtf_loc_hierarchies_b c,
8181                             ams_campaigns_all_b d
8182                       WHERE a.status_code IN
8183                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
8184          --***
8185 	                AND (trunc(a.start_date_time) < b.pre_fiscal_month_start
8186 			     or
8187                              trunc(a.start_date_time) BETWEEN b.pre_fiscal_month_start
8188 			                     AND (b.fiscal_month_start - 1  )
8189 		             )
8190                         AND a.country_id = c.location_hierarchy_id
8191                         AND a.campaign_id = d.campaign_id
8192                         AND trunc(a.start_date_time) >= l_min_start_date
8193                         AND b.period_type = 'MONTH'
8194                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date)) >=
8195                                                       b.pre_fiscal_month_start
8196                    GROUP BY a.campaign_id ,
8197 		            c.area2_code,
8198                             a.country_id,
8199                             NVL(d.business_unit_id, 0),
8200                             DECODE(a.activity_type_code,
8201                                    'EVENTS', -9999,
8202                                    a.activity_id
8203                                   ),
8204                             b.fiscal_year,
8205                             b.fiscal_qtr,
8206                             b.fiscal_month
8207 ------------------------------------------------------------------------------------------
8208                    UNION ALL
8209 ------------------------------------------------------------------------------------------
8210                    SELECT    /*+ use_hash(a)  use_hash(b)  use_hash(c) use_hash(d) */
8211 		             a.campaign_id campaign_id,
8212                              c.area2_code schedule_area2,
8213                              a.country_id schedule_country,
8214                              NVL(d.business_unit_id, 0) business_unit_id,
8215                              DECODE(a.activity_type_code,
8216                                     'EVENTS', -9999,
8217                                     a.activity_id
8218                                    ) schedule_activity_id,
8219                              'MONTH' aggregate_by,
8220                              'Z' display_type,
8221                              b.fiscal_year fiscal_year,
8222                              b.fiscal_qtr fiscal_qtr,
8223                              b.fiscal_month fiscal_month,
8224                              0 curr_count,
8225                              0 pre_count,
8226                              0 curr_started,
8227                              0 pre_started,
8228                              0 curr_ended,
8229                              0 pre_ended,
8230                              COUNT(DISTINCT a.campaign_id) campaign_count,
8231                              0 forecasted_responses,
8232                              0 targeted_customers,
8233                              0 prev_targeted_customers,
8234                              0 budget_approved
8235                        FROM ams_campaign_schedules_b a,
8236                             bim_dates b,
8237                             jtf_loc_hierarchies_b c,
8238                             ams_campaigns_all_b d
8239                       WHERE a.status_code IN
8240                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
8241        --***
8242                   AND (trunc(a.start_date_time) < b.fiscal_month_start
8243 			     or
8244 			     trunc(a.start_date_time) BETWEEN b.fiscal_month_start
8245                                                   AND b.fiscal_month_end
8246 			     )
8247                         AND a.country_id = c.location_hierarchy_id
8248                         AND a.campaign_id = d.campaign_id
8249                         AND b.period_type = 'MONTH'
8250                         AND trunc(a.start_date_time) >= l_min_start_date
8251                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date)) >=
8252                                                           b.fiscal_month_start
8253                    GROUP BY a.campaign_id ,
8254 		             c.area2_code,
8255                             a.country_id,
8256                             NVL(d.business_unit_id, 0),
8257                             DECODE(a.activity_type_code,
8258                                    'EVENTS', -9999,
8259                                    a.activity_id
8260                                   ),
8261                             b.fiscal_year,
8262                             b.fiscal_qtr,
8263                             b.fiscal_month
8264 ------------------------------------------------------------------------------------------
8265                    UNION ALL
8266 ------------------------------------------------------------------------------------------
8267                    SELECT    /*+ use_hash(a)  use_hash(b)  use_hash(c) use_hash(d) */
8268 		            a.campaign_id campaign_id,
8269                              c.area2_code schedule_area2,
8270                              a.schedule_country schedule_country,
8271                              NVL(a.business_unit_id, 0) business_unit_id,
8272                              a.schedule_activity_id schedule_activity_id,
8273                              'MONTH' aggregate_by,
8274                              'Z' display_type,
8275                              b.fiscal_year fiscal_year,
8276                              b.fiscal_qtr fiscal_qtr,
8277                              b.fiscal_month fiscal_month,
8278                              0 curr_count,
8279                              0 pre_count,
8280                              0 curr_started,
8281                              0 pre_started,
8282                              0 curr_ended,
8283                              0 pre_ended,
8284                              0 campaign_count,
8285                              SUM(forecasted_responses) forecasted_responses,
8286                              SUM(targeted_customers) targeted_customers,
8287                              0 prev_targeted_customers,
8288                              SUM(budget_approved) budget_approved
8289                        FROM bim_r_camp_daily_facts a,
8290                             bim_dates b,
8291                             jtf_loc_hierarchies_b c
8292                       WHERE a.schedule_country = c.location_hierarchy_id
8293         --***
8294 	               AND ( trunc(a.start_date) < b.fiscal_month_start
8295 		                  or
8296 			    trunc(a.start_date)  between b.fiscal_month_start and b.fiscal_month_end
8297                            )
8298                         AND trunc(a.end_date) >= b.fiscal_month_start
8299                         AND b.period_type = 'MONTH'
8300                    GROUP BY a.campaign_id ,c.area2_code,
8301                             a.schedule_country,
8302                             NVL(a.business_unit_id, 0),
8303                             a.schedule_activity_id,
8304                             b.fiscal_year,
8305                             b.fiscal_qtr,
8306                             b.fiscal_month
8307 ------------------------------------------------------------------------------------------
8308                    UNION ALL
8309 ------------------------------------------------------------------------------------------
8310                    SELECT    /*+ use_hash(a)  use_hash(b)  use_hash(c) use_hash(d) */
8311 		             a.campaign_id campaign_id,
8312                              c.area2_code schedule_area2,
8313                              a.schedule_country schedule_country,
8314                              NVL(a.business_unit_id, 0) business_unit_id,
8315                              a.schedule_activity_id schedule_activity_id,
8316                              'MONTH' aggregate_by,
8317                              'Z' display_type,
8318                              b.fiscal_year fiscal_year,
8319                              b.fiscal_qtr fiscal_qtr,
8320                              b.fiscal_month fiscal_month,
8321                              0 curr_count,
8322                              0 pre_count,
8323                              0 curr_started,
8324                              0 pre_started,
8325                              0 curr_ended,
8326                              0 pre_ended,
8327                              0 campaign_count,
8328                              0 forecasted_responses,
8329                              0 targeted_customers,
8330                              SUM(targeted_customers) prev_targeted_customers,
8331                              0 budget_approved
8332                        FROM bim_r_camp_daily_facts a,
8333                             bim_dates b,
8334                             jtf_loc_hierarchies_b c
8335                       WHERE a.schedule_country = c.location_hierarchy_id
8336        --***
8337                        AND (trunc(a.start_date) < b.pre_fiscal_month_start
8338 		            or
8339                            trunc(a.start_date) BETWEEN b.pre_fiscal_month_start
8340 			                       and (b.fiscal_month_start - 1  )
8341 		            )
8342                         AND trunc(a.end_date) >= b.pre_fiscal_month_start
8343                         AND b.period_type = 'MONTH'
8344                    GROUP BY a.campaign_id ,c.area2_code,
8345                             a.schedule_country,
8346                             NVL(a.business_unit_id, 0),
8347                             a.schedule_activity_id,
8348                             b.fiscal_year,
8349                             b.fiscal_qtr,
8350                             b.fiscal_month),ams_act_access_denorm R
8351 			    where campaign_id=r.object_id
8352 			    and r.object_type='CAMP'
8353          GROUP BY schedule_area2,
8354                   schedule_country,
8355                   business_unit_id,
8356                   resource_id,
8357                   schedule_activity_id,
8358                   aggregate_by,
8359                   display_type,
8360                   fiscal_year,
8361                   fiscal_qtr,
8362                   fiscal_month
8363 
8364 		  ---------- for admin
8365 				  union all
8366 				  -------------
8367 				  SELECT
8368 	         'N' campaign_area1,
8369                   'N' campaign_area2,
8370                   'N' schedule_area1,
8371                   schedule_area2,
8372                   'N' campaign_country,
8373                   schedule_country,
8374                   business_unit_id,
8375                   -1 resource_id,
8376                   'N' campaign_type,
8377                   schedule_activity_id,
8378                   'N' schedule_activity_type,
8379                   'N' campaign_status,
8380                   'N' schedule_status,
8381                   aggregate_by,
8382                   display_type,
8383                   fiscal_year YEAR,
8384                   fiscal_qtr qtr,
8385                   fiscal_month MONTH,
8386                   'ACT' report_type,
8387                   SUM(curr_count) current_count_value,
8388                   SUM(curr_started) current_started_value,
8389                   SUM(curr_ended) current_ended_value,
8390                   SUM(pre_count) previous_count_value,
8391                   SUM(pre_started) previous_started_value,
8392                   SUM(pre_ended) previous_ended_value,
8393                   SUM(campaign_count) campaign_count,
8394                   SUM(forecasted_responses) forecasted_responses,
8395                   SUM(targeted_customers) targeted_customers,
8396                   SUM(prev_targeted_customers) prev_targeted_customers,
8397                   SUM(budget_approved) budget_approved
8398              FROM (
8399 -- FOR THE PERIOD TYPE MONTH
8400                    SELECT    /*+ use_hash(a)   use_hash(b)  use_hash(c) use_hash(d) */
8401 		             		 a.campaign_id campaign_id,
8402                              c.area2_code schedule_area2,
8403                              a.country_id schedule_country,
8404                              NVL(d.business_unit_id, 0) business_unit_id,
8405                              DECODE(a.activity_type_code,
8406                                     'EVENTS', -9999,
8407                                     a.activity_id
8408                                    ) schedule_activity_id,
8409                              'MONTH' aggregate_by,
8410                              'Z' display_type,
8411                              b.fiscal_year fiscal_year,
8412                              b.fiscal_qtr fiscal_qtr,
8413                              b.fiscal_month fiscal_month,
8414                              COUNT(a.schedule_id) curr_count,
8415                              0 pre_count,
8416                              0 curr_started,
8417                              0 pre_started,
8418                              0 curr_ended,
8419                              0 pre_ended,
8420                              0 campaign_count,
8421                              0 forecasted_responses,
8422                              0 targeted_customers,
8423                              0 prev_targeted_customers,
8424                              0 budget_approved
8425                        FROM ams_campaign_schedules_b a,
8426                             bim_dates b,
8427                             jtf_loc_hierarchies_b c,
8428                             ams_campaigns_all_b d
8429                       WHERE a.status_code IN
8430                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
8431         --***
8432 	                AND (trunc(a.start_date_time) < b.fiscal_month_start
8433 			     or
8434 			     trunc(a.start_date_time) BETWEEN b.fiscal_month_start
8435                                                   AND b.fiscal_month_end
8436 			     )
8437 
8438                         AND a.country_id = c.location_hierarchy_id
8439                         AND a.campaign_id = d.campaign_id
8440                         AND b.period_type = 'MONTH'
8441                         AND trunc(a.start_date_time) >= l_min_start_date
8442                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date)) >=
8443                                                           b.fiscal_month_start
8444                    GROUP BY a.campaign_id,
8445 		            c.area2_code,
8446                             a.country_id,
8447                             NVL(d.business_unit_id, 0),
8448                             DECODE(a.activity_type_code,
8449                                    'EVENTS', -9999,
8450                                    a.activity_id
8451                                   ),
8452                             b.fiscal_year,
8453                             b.fiscal_qtr,
8454                             b.fiscal_month
8455 ------------------------------------------------------------------------------------------
8456                    UNION ALL
8457 ------------------------------------------------------------------------------------------
8458                    SELECT    /*+ use_hash(a)  use_hash(b)  use_hash(c) use_hash(d) */
8459 		             a.campaign_id campaign_id,
8460                              c.area2_code schedule_area2,
8461                              a.country_id schedule_country,
8462                              NVL(d.business_unit_id, 0) business_unit_id,
8463                              DECODE(a.activity_type_code,
8464                                     'EVENTS', -9999,
8465                                     a.activity_id
8466                                    ) schedule_activity_id,
8467                              'MONTH' aggregate_by,
8468                              'Z' display_type,
8469                              b.fiscal_year fiscal_year,
8470                              b.fiscal_qtr fiscal_qtr,
8471                              b.fiscal_month fiscal_month,
8472                              0 curr_count,
8473                              COUNT(a.schedule_id) pre_count,
8474                              0 curr_started,
8475                              0 pre_started,
8476                              0 curr_ended,
8477                              0 pre_ended,
8478                              0 campaign_count,
8479                              0 forecasted_responses,
8480                              0 targeted_customers,
8481                              0 prev_targeted_customers,
8482                              0 budget_approved
8483                        FROM ams_campaign_schedules_b a,
8484                             bim_dates b,
8485                             jtf_loc_hierarchies_b c,
8486                             ams_campaigns_all_b d
8487                       WHERE a.status_code IN
8488                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
8489           --***
8490 	                AND (trunc(a.start_date_time) < b.pre_fiscal_month_start
8491 			     or
8492                              trunc(a.start_date_time) BETWEEN b.pre_fiscal_month_start
8493 			                     AND (b.fiscal_month_start - 1  )
8494 		             )
8495                         AND a.country_id = c.location_hierarchy_id
8496                         AND a.campaign_id = d.campaign_id
8497                         AND trunc(a.start_date_time) >= l_min_start_date
8498                         AND b.period_type = 'MONTH'
8499                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date)) >=
8500                                                       b.pre_fiscal_month_start
8501                    GROUP BY a.campaign_id ,
8502 		            c.area2_code,
8503                             a.country_id,
8504                             NVL(d.business_unit_id, 0),
8505                             DECODE(a.activity_type_code,
8506                                    'EVENTS', -9999,
8507                                    a.activity_id
8508                                   ),
8509                             b.fiscal_year,
8510                             b.fiscal_qtr,
8511                             b.fiscal_month
8512 ------------------------------------------------------------------------------------------
8513                    UNION ALL
8514 ------------------------------------------------------------------------------------------
8515                    SELECT    /*+ use_hash(a)  use_hash(b)  use_hash(c) use_hash(d) */
8516 		             a.campaign_id campaign_id,
8517                              c.area2_code schedule_area2,
8518                              a.country_id schedule_country,
8519                              NVL(d.business_unit_id, 0) business_unit_id,
8520                              DECODE(a.activity_type_code,
8521                                     'EVENTS', -9999,
8522                                     a.activity_id
8523                                    ) schedule_activity_id,
8524                              'MONTH' aggregate_by,
8525                              'Z' display_type,
8526                              b.fiscal_year fiscal_year,
8527                              b.fiscal_qtr fiscal_qtr,
8528                              b.fiscal_month fiscal_month,
8529                              0 curr_count,
8530                              0 pre_count,
8531                              0 curr_started,
8532                              0 pre_started,
8533                              0 curr_ended,
8534                              0 pre_ended,
8535                              COUNT(DISTINCT a.campaign_id) campaign_count,
8536                              0 forecasted_responses,
8537                              0 targeted_customers,
8538                              0 prev_targeted_customers,
8539                              0 budget_approved
8540                        FROM ams_campaign_schedules_b a,
8541                             bim_dates b,
8542                             jtf_loc_hierarchies_b c,
8543                             ams_campaigns_all_b d
8544                       WHERE a.status_code IN
8545                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
8546          --***
8547 	                AND (trunc(a.start_date_time) < b.fiscal_month_start
8548 			     or
8549 			     trunc(a.start_date_time) BETWEEN b.fiscal_month_start
8550                                                   AND b.fiscal_month_end
8551 			     )
8552                         AND a.country_id = c.location_hierarchy_id
8553                         AND a.campaign_id = d.campaign_id
8554                         AND b.period_type = 'MONTH'
8555                         AND trunc(a.start_date_time) >= l_min_start_date
8556                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date)) >=
8557                                                           b.fiscal_month_start
8558                    GROUP BY a.campaign_id ,
8559 		             c.area2_code,
8560                             a.country_id,
8561                             NVL(d.business_unit_id, 0),
8562                             DECODE(a.activity_type_code,
8563                                    'EVENTS', -9999,
8564                                    a.activity_id
8565                                   ),
8566                             b.fiscal_year,
8567                             b.fiscal_qtr,
8568                             b.fiscal_month
8569 ------------------------------------------------------------------------------------------
8570                    UNION ALL
8571 ------------------------------------------------------------------------------------------
8572                    SELECT    /*+ use_hash(a)  use_hash(b)  use_hash(c) use_hash(d) */
8573 		            a.campaign_id campaign_id,
8574                              c.area2_code schedule_area2,
8575                              a.schedule_country schedule_country,
8576                              NVL(a.business_unit_id, 0) business_unit_id,
8577                              a.schedule_activity_id schedule_activity_id,
8578                              'MONTH' aggregate_by,
8579                              'Z' display_type,
8580                              b.fiscal_year fiscal_year,
8581                              b.fiscal_qtr fiscal_qtr,
8582                              b.fiscal_month fiscal_month,
8583                              0 curr_count,
8584                              0 pre_count,
8585                              0 curr_started,
8586                              0 pre_started,
8587                              0 curr_ended,
8588                              0 pre_ended,
8589                              0 campaign_count,
8590                              SUM(forecasted_responses) forecasted_responses,
8591                              SUM(targeted_customers) targeted_customers,
8592                              0 prev_targeted_customers,
8593                              SUM(budget_approved) budget_approved
8594                        FROM bim_r_camp_daily_facts a,
8595                             bim_dates b,
8596                             jtf_loc_hierarchies_b c
8597                       WHERE a.schedule_country = c.location_hierarchy_id
8598         --***
8599 	               AND ( trunc(a.start_date) < b.fiscal_month_start
8600 		                  or
8601 			    trunc(a.start_date)  between b.fiscal_month_start and b.fiscal_month_end
8602                            )
8603                         AND trunc(a.end_date) >= b.fiscal_month_start
8604                         AND b.period_type = 'MONTH'
8605                    GROUP BY a.campaign_id ,c.area2_code,
8606                             a.schedule_country,
8607                             NVL(a.business_unit_id, 0),
8608                             a.schedule_activity_id,
8609                             b.fiscal_year,
8610                             b.fiscal_qtr,
8611                             b.fiscal_month
8612 ------------------------------------------------------------------------------------------
8613                    UNION ALL
8614 ------------------------------------------------------------------------------------------
8615                    SELECT    /*+ use_hash(a)  use_hash(b)  use_hash(c) use_hash(d) */
8616 		             a.campaign_id campaign_id,
8617                              c.area2_code schedule_area2,
8618                              a.schedule_country schedule_country,
8619                              NVL(a.business_unit_id, 0) business_unit_id,
8620                              a.schedule_activity_id schedule_activity_id,
8621                              'MONTH' aggregate_by,
8622                              'Z' display_type,
8623                              b.fiscal_year fiscal_year,
8624                              b.fiscal_qtr fiscal_qtr,
8625                              b.fiscal_month fiscal_month,
8626                              0 curr_count,
8627                              0 pre_count,
8628                              0 curr_started,
8629                              0 pre_started,
8630                              0 curr_ended,
8631                              0 pre_ended,
8632                              0 campaign_count,
8633                              0 forecasted_responses,
8634                              0 targeted_customers,
8635                              SUM(targeted_customers) prev_targeted_customers,
8636                              0 budget_approved
8637                        FROM bim_r_camp_daily_facts a,
8638                             bim_dates b,
8639                             jtf_loc_hierarchies_b c
8640                       WHERE a.schedule_country = c.location_hierarchy_id
8641         --***
8642                          AND (trunc(a.start_date) < b.pre_fiscal_month_start
8643 		            or
8644                            trunc(a.start_date) BETWEEN b.pre_fiscal_month_start
8645 			                       and ( b.fiscal_month_start - 1  )
8646 		            )
8647 
8648                         AND trunc(a.end_date) >= b.pre_fiscal_month_start
8649                         AND b.period_type = 'MONTH'
8650                    GROUP BY a.campaign_id ,c.area2_code,
8651                             a.schedule_country,
8652                             NVL(a.business_unit_id, 0),
8653                             a.schedule_activity_id,
8654                             b.fiscal_year,
8655                             b.fiscal_qtr,
8656                             b.fiscal_month)
8657          GROUP BY schedule_area2,
8658                   schedule_country,
8659                   business_unit_id,
8660                   schedule_activity_id,
8661                   aggregate_by,
8662                   display_type,
8663                   fiscal_year,
8664                   fiscal_qtr,
8665                   fiscal_month;
8666 
8667 				  ---------union all admin end
8668 
8669 
8670 --commit;
8671 
8672 
8673       ams_utility_pvt.write_conc_log('BIM_CAMP_COLLECTION: SUMMARY ACT MONTH INSERT END');
8674       ams_utility_pvt.write_conc_log('BIM_CAMP_COLLECTION: SUMMARY ACT QTR INSERT START');
8675       INSERT INTO  /*+ append parallel(i,5) */
8676         bim_r_camp_collection i
8677                   (campaign_area1,
8678                    campaign_area2,
8679                    schedule_area1,
8680                    schedule_area2,
8681                    campaign_country,
8682                    schedule_country,
8683                    business_unit_id,
8684                    resource_id,
8685                    campaign_type,
8686                    schedule_activity,
8687                    schedule_activity_type,
8688                    campaign_status,
8689                    schedule_status,
8690                    aggregate_by,
8691                    display_type,
8692                    YEAR,
8693                    qtr,
8694                    MONTH,
8695                    report_type,
8696                    current_count_value,
8697                    current_started_value,
8698                    current_ended_value,
8699                    previous_count_value,
8700                    previous_started_value,
8701                    previous_ended_value,
8702                    campaign_count,
8703                    forecasted_responses,
8704                    targeted_customers,
8705                    prev_targeted_customers,
8706                    budget_approved
8707                   )
8708          SELECT   /* +INDEX_FFS(R,BIM_DBI_U2) */
8709 	          'N' campaign_area1,
8710                   'N' campaign_area2,
8711                   'N' schedule_area1,
8712                   schedule_area2,
8713                   'N' campaign_country,
8714                   schedule_country,
8715                   business_unit_id,
8716                   resource_id,
8717                   'N' campaign_type,
8718                   schedule_activity_id,
8719                   'N' schedule_activity_type,
8720                   'N' campaign_status,
8721                   'N' schedule_status,
8722                   aggregate_by,
8723                   display_type,
8724                   fiscal_year YEAR,
8725                   fiscal_qtr qtr,
8726                   fiscal_month MONTH,
8727                   'ACT' report_type,
8728                   SUM(curr_count) current_count_value,
8729                   SUM(curr_started) current_started_value,
8730                   SUM(curr_ended) current_ended_value,
8731                   SUM(pre_count) previous_count_value,
8732                   SUM(pre_started) previous_started_value,
8733                   SUM(pre_ended) previous_ended_value,
8734                   SUM(campaign_count) campaign_count,
8735                   SUM(forecasted_responses) forecasted_responses,
8736                   SUM(targeted_customers) targeted_customers,
8737                   SUM(prev_targeted_customers) prev_targeted_customers,
8738                   SUM(budget_approved) budget_approved
8739              FROM (
8740 -- FOR THE PERIOD TYPE QTR
8741                    SELECT
8742                         /*+ use_hash(a)   use_hash(b)  use_hash(c) use_hash(d) */
8743 			a.campaign_id campaign_id,
8744                              c.area2_code schedule_area2,
8745                              a.country_id schedule_country,
8746                              NVL(d.business_unit_id, 0) business_unit_id,
8747                              DECODE(a.activity_type_code,
8748                                     'EVENTS', -9999,
8749                                     a.activity_id
8750                                    ) schedule_activity_id,
8751                              'QTR' aggregate_by,
8752                              'Z' display_type,
8753                              b.fiscal_year fiscal_year,
8754                              b.fiscal_qtr fiscal_qtr,
8755                              'N' fiscal_month,
8756                              COUNT(a.schedule_id) curr_count,
8757                              0 pre_count,
8758                              0 curr_started,
8759                              0 pre_started,
8760                              0 curr_ended,
8761                              0 pre_ended,
8762                              0 campaign_count,
8763                              0 forecasted_responses,
8764                              0 targeted_customers,
8765                              0 prev_targeted_customers,
8766                              0 budget_approved
8767                        FROM ams_campaign_schedules_b a,
8768                             bim_dates b,
8769                             jtf_loc_hierarchies_b c,
8770                             ams_campaigns_all_b d
8771                       WHERE a.status_code IN
8772                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
8773         --***
8774 	                 AND (trunc(a.start_date_time) < b.fiscal_qtr_start
8775 			      or
8776 			      trunc(a.start_date_time) BETWEEN b.fiscal_qtr_start
8777 			                             and b.fiscal_qtr_end
8778 			      )
8779                         AND a.country_id = c.location_hierarchy_id
8780                         AND a.campaign_id = d.campaign_id
8781                         AND b.period_type = 'QUARTER'
8782                         AND trunc(a.start_date_time) >= l_min_start_date
8783                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date)) >=
8784                                                             b.fiscal_qtr_start
8785                    GROUP BY a.campaign_id,
8786 			    c.area2_code,
8787                             a.country_id,
8788                             NVL(d.business_unit_id, 0),
8789                             DECODE(a.activity_type_code,
8790                                    'EVENTS', -9999,
8791                                    a.activity_id
8792                                   ),
8793                             b.fiscal_year,
8794                             b.fiscal_qtr
8795 ------------------------------------------------------------------------------------------
8796                    UNION ALL
8797 ------------------------------------------------------------------------------------------
8798                    SELECT
8799                         /*+ use_hash(a)  use_hash(b)  use_hash(c) use_hash(d) */
8800 			    a.campaign_id campaign_id,
8801                              c.area2_code schedule_area2,
8802                              a.country_id schedule_country,
8803                              NVL(d.business_unit_id, 0) business_unit_id,
8804                              DECODE(a.activity_type_code,
8805                                     'EVENTS', -9999,
8806                                     a.activity_id
8807                                    ) schedule_activity_id,
8808                              'QTR' aggregate_by,
8809                              'Z' display_type,
8810                              b.fiscal_year fiscal_year,
8811                              b.fiscal_qtr fiscal_qtr,
8812                              'N' fiscal_month,
8813                              0 curr_count,
8814                              COUNT(a.schedule_id) pre_count,
8815                              0 curr_started,
8816                              0 pre_started,
8817                              0 curr_ended,
8818                              0 pre_ended,
8819                              0 campaign_count,
8820                              0 forecasted_responses,
8821                              0 targeted_customers,
8822                              0 prev_targeted_customers,
8823                              0 budget_approved
8824                        FROM ams_campaign_schedules_b a,
8825                             bim_dates b,
8826                             jtf_loc_hierarchies_b c,
8827                             ams_campaigns_all_b d
8828                       WHERE a.status_code IN
8829                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
8830        --***
8831                         AND ( trunc(a.start_date_time) < b.pre_fiscal_qtr_start
8832 			     or
8833 			     trunc(a.start_date_time) BETWEEN b.pre_fiscal_qtr_start
8834 			                        and (b.fiscal_qtr_start - 1)
8835 			     )
8836                         AND a.country_id = c.location_hierarchy_id
8837                         AND a.campaign_id = d.campaign_id
8838                         AND trunc(a.start_date_time) >= l_min_start_date
8839                         AND b.period_type = 'QUARTER'
8840                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date)) >=
8841                                                         b.pre_fiscal_qtr_start
8842                    GROUP BY a.campaign_id,c.area2_code,
8843                             a.country_id,
8844                             NVL(d.business_unit_id, 0),
8845                             DECODE(a.activity_type_code,
8846                                    'EVENTS', -9999,
8847                                    a.activity_id
8848                                   ),
8849                             b.fiscal_year,
8850                             b.fiscal_qtr
8851 ------------------------------------------------------------------------------------------
8852                    UNION ALL
8853 ------------------------------------------------------------------------------------------
8854                    SELECT
8855                         /*+ use_hash(a)  use_hash(b)  use_hash(c) use_hash(d) */
8856 			     a.campaign_id campaign_id,
8857                              c.area2_code schedule_area2,
8858                              a.country_id schedule_country,
8859                              NVL(d.business_unit_id, 0) business_unit_id,
8860                              DECODE(a.activity_type_code,
8861                                     'EVENTS', -9999,
8862                                     a.activity_id
8863                                    ) schedule_activity_id,
8864                              'QTR' aggregate_by,
8865                              'Z' display_type,
8866                              b.fiscal_year fiscal_year,
8867                              b.fiscal_qtr fiscal_qtr,
8868                              'N' fiscal_month,
8869                              0 curr_count,
8870                              0 pre_count,
8871                              0 curr_started,
8872                              0 pre_started,
8873                              0 curr_ended,
8874                              0 pre_ended,
8875                              COUNT(DISTINCT a.campaign_id) campaign_count,
8876                              0 forecasted_responses,
8877                              0 targeted_customers,
8878                              0 prev_targeted_customers,
8879                              0 budget_approved
8880                        FROM ams_campaign_schedules_b a,
8881                             bim_dates b,
8882                             jtf_loc_hierarchies_b c,
8883                             ams_campaigns_all_b d
8884                       WHERE a.status_code IN
8885                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
8886             --***
8887 	                 AND (trunc(a.start_date_time) < b.fiscal_qtr_start
8888 			      or
8889 			      trunc(a.start_date_time) BETWEEN b.fiscal_qtr_start
8890 			                             and b.fiscal_qtr_end
8891 			      )
8892                         AND a.country_id = c.location_hierarchy_id
8893                         AND a.campaign_id = d.campaign_id
8894                         AND b.period_type = 'QUARTER'
8895                         AND trunc(a.start_date_time) >= l_min_start_date
8896                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date)) >=
8897                                                             b.fiscal_qtr_start
8898                    GROUP BY a.campaign_id ,
8899 		            c.area2_code,
8900                             a.country_id,
8901                             NVL(d.business_unit_id, 0),
8902                             DECODE(a.activity_type_code,
8903                                    'EVENTS', -9999,
8904                                    a.activity_id
8905                                   ),
8906                             b.fiscal_year,
8907                             b.fiscal_qtr
8908 ------------------------------------------------------------------------------------------
8909                    UNION ALL
8910 ------------------------------------------------------------------------------------------
8911                    SELECT
8912                         /*+ use_hash(a)  use_hash(b)  use_hash(c) use_hash(d) */
8913 			    a.campaign_id campaign_id,
8914                              c.area2_code schedule_area2,
8915                              a.schedule_country schedule_country,
8916                              NVL(a.business_unit_id, 0) business_unit_id,
8917                              a.schedule_activity_id schedule_activity_id,
8918                              'QTR' aggregate_by,
8919                              'Z' display_type,
8920                              b.fiscal_year fiscal_year,
8921                              b.fiscal_qtr fiscal_qtr,
8922                              'N' fiscal_month,
8923                              0 curr_count,
8924                              0 pre_count,
8925                              0 curr_started,
8926                              0 pre_started,
8927                              0 curr_ended,
8928                              0 pre_ended,
8929                              0 campaign_count,
8930                              SUM(forecasted_responses) forecasted_responses,
8931                              SUM(targeted_customers) targeted_customers,
8932                              0 prev_targeted_customers,
8933                              SUM(budget_approved) budget_approved
8934                        FROM bim_r_camp_daily_facts a,
8935                             bim_dates b,
8936                             jtf_loc_hierarchies_b c
8937                       WHERE a.schedule_country = c.location_hierarchy_id
8938      --***
8939                            AND (trunc(a.start_date) < b.fiscal_qtr_start
8940 			        or
8941                                 trunc(a.start_date) BETWEEN b.fiscal_qtr_start
8942 			                             and b.fiscal_qtr_end
8943 			      )
8944                         AND trunc(a.end_date) >= b.fiscal_qtr_start
8945                         AND b.period_type = 'QUARTER'
8946                    GROUP BY a.campaign_id ,c.area2_code,
8947                             a.schedule_country,
8948                             NVL(a.business_unit_id, 0),
8949                             a.schedule_activity_id,
8950                             b.fiscal_year,
8951                             b.fiscal_qtr
8952 ------------------------------------------------------------------------------------------
8953                    UNION ALL
8954 ------------------------------------------------------------------------------------------
8955                    SELECT
8956                         /*+ use_hash(a)  use_hash(b)  use_hash(c) use_hash(d) */
8957                              a.campaign_id campaign_id,
8958 			     c.area2_code schedule_area2,
8959                              a.schedule_country schedule_country,
8960                              NVL(a.business_unit_id, 0) business_unit_id,
8961                              a.schedule_activity_id schedule_activity_id,
8962                              'QTR' aggregate_by,
8963                              'Z' display_type,
8964                              b.fiscal_year fiscal_year,
8965                              b.fiscal_qtr fiscal_qtr,
8966                              'N' fiscal_month,
8967                              0 curr_count,
8968                              0 pre_count,
8969                              0 curr_started,
8970                              0 pre_started,
8971                              0 curr_ended,
8972                              0 pre_ended,
8973                              0 campaign_count,
8974                              0 forecasted_responses,
8975                              0 targeted_customers,
8976                              SUM(targeted_customers) prev_targeted_customers,
8977                              0 budget_approved
8978                        FROM bim_r_camp_daily_facts a,
8979                             bim_dates b,
8980                             jtf_loc_hierarchies_b c
8981                       WHERE a.schedule_country = c.location_hierarchy_id
8982        --***
8983                           AND (trunc(a.start_date) < b.pre_fiscal_qtr_start
8984 			         or
8985 			      trunc(a.start_date)  BETWEEN b.pre_fiscal_qtr_start
8986 			                        and (b.fiscal_qtr_start - 1)
8987 			     )
8988                         AND trunc(a.end_date) >= b.pre_fiscal_qtr_start
8989                         AND b.period_type = 'QUARTER'
8990                    GROUP BY a.campaign_id ,
8991 		            c.area2_code,
8992                             a.schedule_country,
8993                             NVL(a.business_unit_id, 0),
8994                             a.schedule_activity_id,
8995                             b.fiscal_year,
8996                             b.fiscal_qtr) ,ams_act_access_denorm R
8997 			    where campaign_id=r.object_id
8998 			    and r.object_type='CAMP'
8999          GROUP BY schedule_area2,
9000                   schedule_country,
9001                   business_unit_id,
9002                   resource_id,
9003                   schedule_activity_id,
9004                   aggregate_by,
9005                   display_type,
9006                   fiscal_year,
9007                   fiscal_qtr,
9008                   fiscal_month
9009 
9010   ---------- for admin
9011 				  union all
9012 				  -------------
9013 
9014 
9015 				  SELECT   /* +INDEX_FFS(R,BIM_DBI_U2) */
9016 	          'N' campaign_area1,
9017                   'N' campaign_area2,
9018                   'N' schedule_area1,
9019                   schedule_area2,
9020                   'N' campaign_country,
9021                   schedule_country,
9022                   business_unit_id,
9023                  -1 resource_id,
9024                   'N' campaign_type,
9025                   schedule_activity_id,
9026                   'N' schedule_activity_type,
9027                   'N' campaign_status,
9028                   'N' schedule_status,
9029                   aggregate_by,
9030                   display_type,
9031                   fiscal_year YEAR,
9032                   fiscal_qtr qtr,
9033                   fiscal_month MONTH,
9034                   'ACT' report_type,
9035                   SUM(curr_count) current_count_value,
9036                   SUM(curr_started) current_started_value,
9037                   SUM(curr_ended) current_ended_value,
9038                   SUM(pre_count) previous_count_value,
9039                   SUM(pre_started) previous_started_value,
9040                   SUM(pre_ended) previous_ended_value,
9041                   SUM(campaign_count) campaign_count,
9042                   SUM(forecasted_responses) forecasted_responses,
9043                   SUM(targeted_customers) targeted_customers,
9044                   SUM(prev_targeted_customers) prev_targeted_customers,
9045                   SUM(budget_approved) budget_approved
9046              FROM (
9047 -- FOR THE PERIOD TYPE QTR
9048                    SELECT
9049                         /*+ use_hash(a)   use_hash(b)  use_hash(c) use_hash(d) */
9050 			a.campaign_id campaign_id,
9051                              c.area2_code schedule_area2,
9052                              a.country_id schedule_country,
9053                              NVL(d.business_unit_id, 0) business_unit_id,
9054                              DECODE(a.activity_type_code,
9055                                     'EVENTS', -9999,
9056                                     a.activity_id
9057                                    ) schedule_activity_id,
9058                              'QTR' aggregate_by,
9059                              'Z' display_type,
9060                              b.fiscal_year fiscal_year,
9061                              b.fiscal_qtr fiscal_qtr,
9062                              'N' fiscal_month,
9063                              COUNT(a.schedule_id) curr_count,
9064                              0 pre_count,
9065                              0 curr_started,
9066                              0 pre_started,
9067                              0 curr_ended,
9068                              0 pre_ended,
9069                              0 campaign_count,
9070                              0 forecasted_responses,
9071                              0 targeted_customers,
9072                              0 prev_targeted_customers,
9073                              0 budget_approved
9074                        FROM ams_campaign_schedules_b a,
9075                             bim_dates b,
9076                             jtf_loc_hierarchies_b c,
9077                             ams_campaigns_all_b d
9078                       WHERE a.status_code IN
9079                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
9080         --***
9081 	                 AND (trunc(a.start_date_time) < b.fiscal_qtr_start
9082 			      or
9083 			      trunc(a.start_date_time) BETWEEN b.fiscal_qtr_start
9084 			                             and b.fiscal_qtr_end
9085 			      )
9086                         AND a.country_id = c.location_hierarchy_id
9087                         AND a.campaign_id = d.campaign_id
9088                         AND b.period_type = 'QUARTER'
9089                         AND trunc(a.start_date_time) >= l_min_start_date
9090                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date)) >=
9091                                                             b.fiscal_qtr_start
9092                    GROUP BY a.campaign_id,
9093 			    c.area2_code,
9094                             a.country_id,
9095                             NVL(d.business_unit_id, 0),
9096                             DECODE(a.activity_type_code,
9097                                    'EVENTS', -9999,
9098                                    a.activity_id
9099                                   ),
9100                             b.fiscal_year,
9101                             b.fiscal_qtr
9102 ------------------------------------------------------------------------------------------
9103                    UNION ALL
9104 ------------------------------------------------------------------------------------------
9105                    SELECT
9106                         /*+ use_hash(a)  use_hash(b)  use_hash(c) use_hash(d) */
9107 			    a.campaign_id campaign_id,
9108                              c.area2_code schedule_area2,
9109                              a.country_id schedule_country,
9110                              NVL(d.business_unit_id, 0) business_unit_id,
9111                              DECODE(a.activity_type_code,
9112                                     'EVENTS', -9999,
9113                                     a.activity_id
9114                                    ) schedule_activity_id,
9115                              'QTR' aggregate_by,
9116                              'Z' display_type,
9117                              b.fiscal_year fiscal_year,
9118                              b.fiscal_qtr fiscal_qtr,
9119                              'N' fiscal_month,
9120                              0 curr_count,
9121                              COUNT(a.schedule_id) pre_count,
9122                              0 curr_started,
9123                              0 pre_started,
9124                              0 curr_ended,
9125                              0 pre_ended,
9126                              0 campaign_count,
9127                              0 forecasted_responses,
9128                              0 targeted_customers,
9129                              0 prev_targeted_customers,
9130                              0 budget_approved
9131                        FROM ams_campaign_schedules_b a,
9132                             bim_dates b,
9133                             jtf_loc_hierarchies_b c,
9134                             ams_campaigns_all_b d
9135                       WHERE a.status_code IN
9136                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
9137          --***
9138                         AND ( trunc(a.start_date_time) < b.pre_fiscal_qtr_start
9139 			     or
9140 			     trunc(a.start_date_time) BETWEEN b.pre_fiscal_qtr_start
9141 			                        and (b.fiscal_qtr_start - 1)
9142 			     )
9143                         AND a.country_id = c.location_hierarchy_id
9144                         AND a.campaign_id = d.campaign_id
9145                         AND trunc(a.start_date_time) >= l_min_start_date
9146                         AND b.period_type = 'QUARTER'
9147                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date)) >=
9148                                                         b.pre_fiscal_qtr_start
9149                    GROUP BY a.campaign_id,c.area2_code,
9150                             a.country_id,
9151                             NVL(d.business_unit_id, 0),
9152                             DECODE(a.activity_type_code,
9153                                    'EVENTS', -9999,
9154                                    a.activity_id
9155                                   ),
9156                             b.fiscal_year,
9157                             b.fiscal_qtr
9158 ------------------------------------------------------------------------------------------
9159                    UNION ALL
9160 ------------------------------------------------------------------------------------------
9161                    SELECT
9162                         /*+ use_hash(a)  use_hash(b)  use_hash(c) use_hash(d) */
9163 			     a.campaign_id campaign_id,
9164                              c.area2_code schedule_area2,
9165                              a.country_id schedule_country,
9166                              NVL(d.business_unit_id, 0) business_unit_id,
9167                              DECODE(a.activity_type_code,
9168                                     'EVENTS', -9999,
9169                                     a.activity_id
9170                                    ) schedule_activity_id,
9171                              'QTR' aggregate_by,
9172                              'Z' display_type,
9173                              b.fiscal_year fiscal_year,
9174                              b.fiscal_qtr fiscal_qtr,
9175                              'N' fiscal_month,
9176                              0 curr_count,
9177                              0 pre_count,
9178                              0 curr_started,
9179                              0 pre_started,
9180                              0 curr_ended,
9181                              0 pre_ended,
9182                              COUNT(DISTINCT a.campaign_id) campaign_count,
9183                              0 forecasted_responses,
9184                              0 targeted_customers,
9185                              0 prev_targeted_customers,
9186                              0 budget_approved
9187                        FROM ams_campaign_schedules_b a,
9188                             bim_dates b,
9189                             jtf_loc_hierarchies_b c,
9190                             ams_campaigns_all_b d
9191                       WHERE a.status_code IN
9192                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
9193     --***
9194 	                 AND (trunc(a.start_date_time) < b.fiscal_qtr_start
9195 			      or
9196 			      trunc(a.start_date_time) BETWEEN b.fiscal_qtr_start
9197 			                             and b.fiscal_qtr_end
9198 			      )
9199                         AND a.country_id = c.location_hierarchy_id
9200                         AND a.campaign_id = d.campaign_id
9201                         AND b.period_type = 'QUARTER'
9202                         AND trunc(a.start_date_time) >= l_min_start_date
9203                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date)) >=
9204                                                             b.fiscal_qtr_start
9205                    GROUP BY a.campaign_id ,
9206 		            c.area2_code,
9207                             a.country_id,
9208                             NVL(d.business_unit_id, 0),
9209                             DECODE(a.activity_type_code,
9210                                    'EVENTS', -9999,
9211                                    a.activity_id
9212                                   ),
9213                             b.fiscal_year,
9214                             b.fiscal_qtr
9215 ------------------------------------------------------------------------------------------
9216                    UNION ALL
9217 ------------------------------------------------------------------------------------------
9218                    SELECT
9219                         /*+ use_hash(a)  use_hash(b)  use_hash(c) use_hash(d) */
9220 			    a.campaign_id campaign_id,
9221                              c.area2_code schedule_area2,
9222                              a.schedule_country schedule_country,
9223                              NVL(a.business_unit_id, 0) business_unit_id,
9224                              a.schedule_activity_id schedule_activity_id,
9225                              'QTR' aggregate_by,
9226                              'Z' display_type,
9227                              b.fiscal_year fiscal_year,
9228                              b.fiscal_qtr fiscal_qtr,
9229                              'N' fiscal_month,
9230                              0 curr_count,
9231                              0 pre_count,
9232                              0 curr_started,
9233                              0 pre_started,
9234                              0 curr_ended,
9235                              0 pre_ended,
9236                              0 campaign_count,
9237                              SUM(forecasted_responses) forecasted_responses,
9238                              SUM(targeted_customers) targeted_customers,
9239                              0 prev_targeted_customers,
9240                              SUM(budget_approved) budget_approved
9241                        FROM bim_r_camp_daily_facts a,
9242                             bim_dates b,
9243                             jtf_loc_hierarchies_b c
9244                       WHERE a.schedule_country = c.location_hierarchy_id
9245         --***
9246                            AND (trunc(a.start_date) < b.fiscal_qtr_start
9247 			        or
9248                                 trunc(a.start_date) BETWEEN b.fiscal_qtr_start
9249 			                             and b.fiscal_qtr_end
9250 			      )
9251                         AND trunc(a.end_date) >= b.fiscal_qtr_start
9252                         AND b.period_type = 'QUARTER'
9253                    GROUP BY a.campaign_id ,c.area2_code,
9254                             a.schedule_country,
9255                             NVL(a.business_unit_id, 0),
9256                             a.schedule_activity_id,
9257                             b.fiscal_year,
9258                             b.fiscal_qtr
9259 ------------------------------------------------------------------------------------------
9260                    UNION ALL
9261 ------------------------------------------------------------------------------------------
9262                    SELECT
9263                         /*+ use_hash(a)  use_hash(b)  use_hash(c) use_hash(d) */
9264                              a.campaign_id campaign_id,
9265 			     c.area2_code schedule_area2,
9266                              a.schedule_country schedule_country,
9267                              NVL(a.business_unit_id, 0) business_unit_id,
9268                              a.schedule_activity_id schedule_activity_id,
9269                              'QTR' aggregate_by,
9270                              'Z' display_type,
9271                              b.fiscal_year fiscal_year,
9272                              b.fiscal_qtr fiscal_qtr,
9273                              'N' fiscal_month,
9274                              0 curr_count,
9275                              0 pre_count,
9276                              0 curr_started,
9277                              0 pre_started,
9278                              0 curr_ended,
9279                              0 pre_ended,
9280                              0 campaign_count,
9281                              0 forecasted_responses,
9282                              0 targeted_customers,
9283                              SUM(targeted_customers) prev_targeted_customers,
9284                              0 budget_approved
9285                        FROM bim_r_camp_daily_facts a,
9286                             bim_dates b,
9287                             jtf_loc_hierarchies_b c
9288                       WHERE a.schedule_country = c.location_hierarchy_id
9289       --***
9290                           AND (trunc(a.start_date) < b.pre_fiscal_qtr_start
9291 			         or
9292 			      trunc(a.start_date)  BETWEEN b.pre_fiscal_qtr_start
9293 			                        and (b.fiscal_qtr_start - 1)
9294 			     )
9295                         AND trunc(a.end_date) >= b.pre_fiscal_qtr_start
9296                         AND b.period_type = 'QUARTER'
9297                    GROUP BY a.campaign_id ,
9298 		            c.area2_code,
9299                             a.schedule_country,
9300                             NVL(a.business_unit_id, 0),
9301                             a.schedule_activity_id,
9302                             b.fiscal_year,
9303                             b.fiscal_qtr)
9304          GROUP BY schedule_area2,
9305                   schedule_country,
9306                   business_unit_id,
9307                   schedule_activity_id,
9308                   aggregate_by,
9309                   display_type,
9310                   fiscal_year,
9311                   fiscal_qtr,
9312                   fiscal_month;
9313 
9314 
9315 
9316 
9317 
9318 
9319 --------union all admin end
9320 
9321 
9322  --commit;
9323 
9324 
9325       ams_utility_pvt.write_conc_log('BIM_CAMP_COLLECTION: SUMMARY ACT QTR INSERT END');
9326       ams_utility_pvt.write_conc_log('BIM_CAMP_COLLECTION: SUMMARY ACT YEAR INSERT START');
9327       INSERT INTO  /*+ append parallel(i,5) */
9328 	bim_r_camp_collection i
9329                   (campaign_area1,
9330                    campaign_area2,
9331                    schedule_area1,
9332                    schedule_area2,
9333                    campaign_country,
9334                    schedule_country,
9335                    business_unit_id,
9336                    resource_id,
9337                    campaign_type,
9338                    schedule_activity,
9339                    schedule_activity_type,
9340                    campaign_status,
9341                    schedule_status,
9342                    aggregate_by,
9343                    display_type,
9344                    YEAR,
9345                    qtr,
9346                    MONTH,
9347                    report_type,
9348                    current_count_value,
9349                    current_started_value,
9350                    current_ended_value,
9351                    previous_count_value,
9352                    previous_started_value,
9353                    previous_ended_value,
9354                    campaign_count,
9355                    forecasted_responses,
9356                    targeted_customers,
9357                    prev_targeted_customers,
9358                    budget_approved
9359                   )
9360          SELECT  /* +INDEX_FFS(R,BIM_DBI_U2) */
9361 	           'N' campaign_area1,
9362                   'N' campaign_area2,
9363                   'N' schedule_area1,
9364                   schedule_area2,
9365                   'N' campaign_country,
9366                   schedule_country,
9367                   business_unit_id,
9368                   resource_id,
9369                   'N' campaign_type,
9370                   schedule_activity_id,
9371                   'N' schedule_activity_type,
9372                   'N' campaign_status,
9373                   'N' schedule_status,
9374                   aggregate_by,
9375                   display_type,
9376                   fiscal_year YEAR,
9377                   fiscal_qtr qtr,
9378                   fiscal_month MONTH,
9379                   'ACT' report_type,
9380                   SUM(curr_count) current_count_value,
9381                   SUM(curr_started) current_started_value,
9382                   SUM(curr_ended) current_ended_value,
9383                   SUM(pre_count) previous_count_value,
9384                   SUM(pre_started) previous_started_value,
9385                   SUM(pre_ended) previous_ended_value,
9386                   SUM(campaign_count) campaign_count,
9387                   SUM(forecasted_responses) forecasted_responses,
9388                   SUM(targeted_customers) targeted_customers,
9389                   SUM(prev_targeted_customers) prev_targeted_customers,
9390                   SUM(budget_approved) budget_approved
9391              FROM (
9392 -- FOR THE PERIOD TYPE YEAR
9393                    SELECT
9394                         /*+ use_hash(a)   use_hash(b)  use_hash(c) use_hash(d) */
9395                              a.campaign_id campaign_id,
9396 			     c.area2_code schedule_area2,
9397                              a.country_id schedule_country,
9398                              NVL(d.business_unit_id, 0) business_unit_id,
9399                              DECODE(a.activity_type_code,
9400                                     'EVENTS', -9999,
9401                                     a.activity_id
9402                                    ) schedule_activity_id,
9403                              'YEAR' aggregate_by,
9404                              'Z' display_type,
9405                              b.fiscal_year fiscal_year,
9406                              'N' fiscal_qtr,
9407                              'N' fiscal_month,
9408                              COUNT(a.schedule_id) curr_count,
9409                              0 pre_count,
9410                              0 curr_started,
9411                              0 pre_started,
9412                              0 curr_ended,
9413                              0 pre_ended,
9414                              0 campaign_count,
9415                              0 forecasted_responses,
9416                              0 targeted_customers,
9417                              0 prev_targeted_customers,
9418                              0 budget_approved
9419                        FROM ams_campaign_schedules_b a,
9420                             bim_dates b,
9421                             jtf_loc_hierarchies_b c,
9422                             ams_campaigns_all_b d
9423                       WHERE a.status_code IN
9424                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
9425                 --***
9426                          AND (trunc(a.start_date_time) < b.fiscal_year_start
9427 			     or
9428 			     trunc(a.start_date_time) BETWEEN b.fiscal_year_start
9429                                                   AND b.fiscal_year_end
9430 			      )
9431                         AND a.country_id = c.location_hierarchy_id
9432                         AND a.campaign_id = d.campaign_id
9433                         AND b.period_type = 'YEAR'
9434                         AND trunc(a.start_date_time) >= l_min_start_date
9435                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date)) >=
9436                                                            b.fiscal_year_start
9437                    GROUP BY a.campaign_id ,
9438 			   c.area2_code,
9439                             a.country_id,
9440                             NVL(d.business_unit_id, 0),
9441                             DECODE(a.activity_type_code,
9442                                    'EVENTS', -9999,
9443                                    a.activity_id
9444                                   ),
9445                             b.fiscal_year
9446 ------------------------------------------------------------------------------------------
9447                    UNION ALL
9448 ------------------------------------------------------------------------------------------
9449                    SELECT
9450                         /*+ use_hash(a)   use_hash(b)  use_hash(c) use_hash(d) */
9451 			     a.campaign_id campaign_id,
9452                              c.area2_code schedule_area2,
9453                              a.country_id schedule_country,
9454                              NVL(d.business_unit_id, 0) business_unit_id,
9455                              DECODE(a.activity_type_code,
9456                                     'EVENTS', -9999,
9457                                     a.activity_id
9458                                    ) schedule_activity_id,
9459                              'YEAR' aggregate_by,
9460                              'Z' display_type,
9461                              b.fiscal_year fiscal_year,
9462                              'N' fiscal_qtr,
9463                              'N' fiscal_month,
9464                              0 curr_count,
9465                              COUNT(a.schedule_id) pre_count,
9466                              0 curr_started,
9467                              0 pre_started,
9468                              0 curr_ended,
9469                              0 pre_ended,
9470                              0 campaign_count,
9471                              0 forecasted_responses,
9472                              0 targeted_customers,
9473                              0 prev_targeted_customers,
9474                              0 budget_approved
9475                        FROM ams_campaign_schedules_b a,
9476                             bim_dates b,
9477                             jtf_loc_hierarchies_b c,
9478                             ams_campaigns_all_b d
9479                       WHERE a.status_code IN
9480                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
9481             --***
9482 	               AND (trunc(a.start_date_time) < b.pre_fiscal_year_start
9483 		            or
9484 			    trunc(a.start_date_time) BETWEEN b.pre_fiscal_year_start
9485                                                   AND(b.fiscal_year_start - 1
9486                                                      )
9487 			    )
9488                         AND a.country_id = c.location_hierarchy_id
9489                         AND a.campaign_id = d.campaign_id
9490                         AND trunc(a.start_date_time) >= l_min_start_date
9491                         AND b.period_type = 'YEAR'
9492                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date)) >=
9493                                                        b.pre_fiscal_year_start
9494                    GROUP BY a.campaign_id,
9495 		            c.area2_code,
9496                             a.country_id,
9497                             NVL(d.business_unit_id, 0),
9498                             DECODE(a.activity_type_code,
9499                                    'EVENTS', -9999,
9500                                    a.activity_id
9501                                   ),
9502                             b.fiscal_year
9503 ------------------------------------------------------------------------------------------
9504                    UNION ALL
9505 ------------------------------------------------------------------------------------------
9506                    SELECT
9507                         /*+ use_hash(a)   use_hash(b)  use_hash(c) use_hash(d) */
9508 			    a.campaign_id campaign_id,
9509                              c.area2_code schedule_area2,
9510                              a.country_id schedule_country,
9511                              NVL(d.business_unit_id, 0) business_unit_id,
9512                              DECODE(a.activity_type_code,
9513                                     'EVENTS', -9999,
9514                                     a.activity_id
9515                                    ) schedule_activity_id,
9516                              'YEAR' aggregate_by,
9517                              'Z' display_type,
9518                              b.fiscal_year fiscal_year,
9519                              'N' fiscal_qtr,
9520                              'N' fiscal_month,
9521                              0 curr_count,
9522                              0 pre_count,
9523                              0 curr_started,
9524                              0 pre_started,
9525                              0 curr_ended,
9526                              0 pre_ended,
9527                              COUNT(DISTINCT a.campaign_id) campaign_count,
9528                              0 forecasted_responses,
9529                              0 targeted_customers,
9530                              0 prev_targeted_customers,
9531                              0 budget_approved
9532                        FROM ams_campaign_schedules_b a,
9533                             bim_dates b,
9534                             jtf_loc_hierarchies_b c,
9535                             ams_campaigns_all_b d
9536                       WHERE a.status_code IN
9537                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
9538           --***
9539                          AND (trunc(a.start_date_time) < b.fiscal_year_start
9540 			     or
9541 			     trunc(a.start_date_time) BETWEEN b.fiscal_year_start
9542                                                   AND b.fiscal_year_end
9543 			      )
9544 
9545                         AND a.country_id = c.location_hierarchy_id
9546                         AND a.campaign_id = d.campaign_id
9547                         AND b.period_type = 'YEAR'
9548                         AND trunc(a.start_date_time) >= l_min_start_date
9549                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date)) >=
9550                                                            b.fiscal_year_start
9551                    GROUP BY a.campaign_id ,c.area2_code,
9552                             a.country_id,
9553                             NVL(d.business_unit_id, 0),
9554                             DECODE(a.activity_type_code,
9555                                    'EVENTS', -9999,
9556                                    a.activity_id
9557                                   ),
9558                             b.fiscal_year
9559 ------------------------------------------------------------------------------------------
9560                    UNION ALL
9561 ------------------------------------------------------------------------------------------
9562                    SELECT
9563                         /*+ use_hash(a)   use_hash(b)  use_hash(c)  */
9564                              a.campaign_id campaign_id,
9565 			     c.area2_code schedule_area2,
9566                              a.schedule_country schedule_country,
9567                              NVL(a.business_unit_id, 0) business_unit_id,
9568                              a.schedule_activity_id schedule_activity_id,
9569                              'YEAR' aggregate_by,
9570                              'Z' display_type,
9571                              b.fiscal_year fiscal_year,
9572                              'N' fiscal_qtr,
9573                              'N' fiscal_month,
9574                              0 curr_count,
9575                              0 pre_count,
9576                              0 curr_started,
9577                              0 pre_started,
9578                              0 curr_ended,
9579                              0 pre_ended,
9580                              0 campaign_count,
9581                              SUM(forecasted_responses) forecasted_responses,
9582                              SUM(targeted_customers) targeted_customers,
9583                              0 prev_targeted_customers,
9584                              SUM(budget_approved) budget_approved
9585                        FROM bim_r_camp_daily_facts a,
9586                             bim_dates b,
9587                             jtf_loc_hierarchies_b c
9588                       WHERE a.schedule_country = c.location_hierarchy_id
9589        --***
9590                      AND ( trunc(a.start_date) < b.fiscal_year_start
9591 		          or
9592 			   trunc(a.start_date) between b.fiscal_year_start
9593                                                   AND b.fiscal_year_end
9594 			  )
9595                         AND trunc(a.end_date) >= b.fiscal_year_start
9596                         AND b.period_type = 'YEAR'
9597                    GROUP BY a.campaign_id ,c.area2_code,
9598                             a.schedule_country,
9599                             NVL(a.business_unit_id, 0),
9600                             a.schedule_activity_id,
9601                             b.fiscal_year
9602 ------------------------------------------------------------------------------------------
9603                    UNION ALL
9604 ------------------------------------------------------------------------------------------
9605                    SELECT
9606                         /*+ use_hash(a)   use_hash(b)  use_hash(c)  */
9607                              a.campaign_id campaign_id,
9608 			     c.area2_code schedule_area2,
9609                              a.schedule_country schedule_country,
9610                              NVL(a.business_unit_id, 0) business_unit_id,
9611                              a.schedule_activity_id schedule_activity_id,
9612                              'YEAR' aggregate_by,
9613                              'Z' display_type,
9614                              b.fiscal_year fiscal_year,
9615                              'N' fiscal_qtr,
9616                              'N' fiscal_month,
9617                              0 curr_count,
9618                              0 pre_count,
9619                              0 curr_started,
9620                              0 pre_started,
9621                              0 curr_ended,
9622                              0 pre_ended,
9623                              0 campaign_count,
9624                              0 forecasted_responses,
9625                              0 targeted_customers,
9626                              SUM(targeted_customers) prev_targeted_customers,
9627                              0 budget_approved
9628                        FROM bim_r_camp_daily_facts a,
9629                             bim_dates b,
9630                             jtf_loc_hierarchies_b c
9631                       WHERE a.schedule_country = c.location_hierarchy_id
9632        --***
9633                         AND (trunc(a.start_date) < b.pre_fiscal_year_start
9634 			     or
9635 			     trunc(a.start_date) between b.pre_fiscal_year_start
9636                                                   AND(b.fiscal_year_start - 1
9637                                                      )
9638 			    )
9639                         AND trunc(a.end_date) >= b.pre_fiscal_year_start
9640                         AND b.period_type = 'YEAR'
9641                    GROUP BY a.campaign_id ,
9642 		            c.area2_code,
9643                             a.schedule_country,
9644                             NVL(a.business_unit_id, 0),
9645                             a.schedule_activity_id,
9646                             b.fiscal_year),ams_act_access_denorm R
9647 			    where campaign_id=r.object_id
9648 			    and r.object_type='CAMP'
9649          GROUP BY schedule_area2,
9650                   schedule_country,
9651                   business_unit_id,
9652                   resource_id,
9653                   schedule_activity_id,
9654                   aggregate_by,
9655                   display_type,
9656                   fiscal_year,
9657                   fiscal_qtr,
9658                   fiscal_month
9659 	  ---------- for admin
9660 				  union all
9661 				  -------------
9662 
9663 
9664 
9665 	SELECT
9666 	           'N' campaign_area1,
9667                   'N' campaign_area2,
9668                   'N' schedule_area1,
9669                   schedule_area2,
9670                   'N' campaign_country,
9671                   schedule_country,
9672                   business_unit_id,
9673                   -1 resource_id,
9674                   'N' campaign_type,
9675                   schedule_activity_id,
9676                   'N' schedule_activity_type,
9677                   'N' campaign_status,
9678                   'N' schedule_status,
9679                   aggregate_by,
9680                   display_type,
9681                   fiscal_year YEAR,
9682                   fiscal_qtr qtr,
9683                   fiscal_month MONTH,
9684                   'ACT' report_type,
9685                   SUM(curr_count) current_count_value,
9686                   SUM(curr_started) current_started_value,
9687                   SUM(curr_ended) current_ended_value,
9688                   SUM(pre_count) previous_count_value,
9689                   SUM(pre_started) previous_started_value,
9690                   SUM(pre_ended) previous_ended_value,
9691                   SUM(campaign_count) campaign_count,
9692                   SUM(forecasted_responses) forecasted_responses,
9693                   SUM(targeted_customers) targeted_customers,
9694                   SUM(prev_targeted_customers) prev_targeted_customers,
9695                   SUM(budget_approved) budget_approved
9696              FROM (
9697 -- FOR THE PERIOD TYPE YEAR
9698                    SELECT
9699                         /*+ use_hash(a)   use_hash(b)  use_hash(c) use_hash(d) */
9700                              a.campaign_id campaign_id,
9701 			     c.area2_code schedule_area2,
9702                              a.country_id schedule_country,
9703                              NVL(d.business_unit_id, 0) business_unit_id,
9704                              DECODE(a.activity_type_code,
9705                                     'EVENTS', -9999,
9706                                     a.activity_id
9707                                    ) schedule_activity_id,
9708                              'YEAR' aggregate_by,
9709                              'Z' display_type,
9710                              b.fiscal_year fiscal_year,
9711                              'N' fiscal_qtr,
9712                              'N' fiscal_month,
9713                              COUNT(a.schedule_id) curr_count,
9714                              0 pre_count,
9715                              0 curr_started,
9716                              0 pre_started,
9717                              0 curr_ended,
9718                              0 pre_ended,
9719                              0 campaign_count,
9720                              0 forecasted_responses,
9721                              0 targeted_customers,
9722                              0 prev_targeted_customers,
9723                              0 budget_approved
9724                        FROM ams_campaign_schedules_b a,
9725                             bim_dates b,
9726                             jtf_loc_hierarchies_b c,
9727                             ams_campaigns_all_b d
9728                       WHERE a.status_code IN
9729                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
9730           --***
9731                          AND (trunc(a.start_date_time) < b.fiscal_year_start
9732 			     or
9733 			     trunc(a.start_date_time) BETWEEN b.fiscal_year_start
9734                                                   AND b.fiscal_year_end
9735 			      )
9736                         AND a.country_id = c.location_hierarchy_id
9737                         AND a.campaign_id = d.campaign_id
9738                         AND b.period_type = 'YEAR'
9739                         AND trunc(a.start_date_time) >= l_min_start_date
9740                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date)) >=
9741                                                            b.fiscal_year_start
9742                    GROUP BY a.campaign_id ,
9743 			   c.area2_code,
9744                             a.country_id,
9745                             NVL(d.business_unit_id, 0),
9746                             DECODE(a.activity_type_code,
9747                                    'EVENTS', -9999,
9748                                    a.activity_id
9749                                   ),
9750                             b.fiscal_year
9751 ------------------------------------------------------------------------------------------
9752                    UNION ALL
9753 ------------------------------------------------------------------------------------------
9754                    SELECT
9755                         /*+ use_hash(a)   use_hash(b)  use_hash(c) use_hash(d) */
9756 			     a.campaign_id campaign_id,
9757                              c.area2_code schedule_area2,
9758                              a.country_id schedule_country,
9759                              NVL(d.business_unit_id, 0) business_unit_id,
9760                              DECODE(a.activity_type_code,
9761                                     'EVENTS', -9999,
9762                                     a.activity_id
9763                                    ) schedule_activity_id,
9764                              'YEAR' aggregate_by,
9765                              'Z' display_type,
9766                              b.fiscal_year fiscal_year,
9767                              'N' fiscal_qtr,
9768                              'N' fiscal_month,
9769                              0 curr_count,
9770                              COUNT(a.schedule_id) pre_count,
9771                              0 curr_started,
9772                              0 pre_started,
9773                              0 curr_ended,
9774                              0 pre_ended,
9775                              0 campaign_count,
9776                              0 forecasted_responses,
9777                              0 targeted_customers,
9778                              0 prev_targeted_customers,
9779                              0 budget_approved
9780                        FROM ams_campaign_schedules_b a,
9781                             bim_dates b,
9782                             jtf_loc_hierarchies_b c,
9783                             ams_campaigns_all_b d
9784                       WHERE a.status_code IN
9785                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
9786           --***
9787 	               AND (trunc(a.start_date_time) < b.pre_fiscal_year_start
9788 		            or
9789 			    trunc(a.start_date_time) BETWEEN b.pre_fiscal_year_start
9790                                                   AND(b.fiscal_year_start - 1
9791                                                      )
9792 			    )
9793                         AND a.country_id = c.location_hierarchy_id
9794                         AND a.campaign_id = d.campaign_id
9795                         AND trunc(a.start_date_time) >= l_min_start_date
9796                         AND b.period_type = 'YEAR'
9797                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date)) >=
9798                                                        b.pre_fiscal_year_start
9799                    GROUP BY a.campaign_id,
9800 		            c.area2_code,
9801                             a.country_id,
9802                             NVL(d.business_unit_id, 0),
9803                             DECODE(a.activity_type_code,
9804                                    'EVENTS', -9999,
9805                                    a.activity_id
9806                                   ),
9807                             b.fiscal_year
9808 ------------------------------------------------------------------------------------------
9809                    UNION ALL
9810 ------------------------------------------------------------------------------------------
9811                    SELECT
9812                         /*+ use_hash(a)   use_hash(b)  use_hash(c) use_hash(d) */
9813 			    a.campaign_id campaign_id,
9814                              c.area2_code schedule_area2,
9815                              a.country_id schedule_country,
9816                              NVL(d.business_unit_id, 0) business_unit_id,
9817                              DECODE(a.activity_type_code,
9818                                     'EVENTS', -9999,
9819                                     a.activity_id
9820                                    ) schedule_activity_id,
9821                              'YEAR' aggregate_by,
9822                              'Z' display_type,
9823                              b.fiscal_year fiscal_year,
9824                              'N' fiscal_qtr,
9825                              'N' fiscal_month,
9826                              0 curr_count,
9827                              0 pre_count,
9828                              0 curr_started,
9829                              0 pre_started,
9830                              0 curr_ended,
9831                              0 pre_ended,
9832                              COUNT(DISTINCT a.campaign_id) campaign_count,
9833                              0 forecasted_responses,
9834                              0 targeted_customers,
9835                              0 prev_targeted_customers,
9836                              0 budget_approved
9837                        FROM ams_campaign_schedules_b a,
9838                             bim_dates b,
9839                             jtf_loc_hierarchies_b c,
9840                             ams_campaigns_all_b d
9841                       WHERE a.status_code IN
9842                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
9843            --***
9844                          AND (trunc(a.start_date_time) < b.fiscal_year_start
9845 			     or
9846 			     trunc(a.start_date_time) BETWEEN b.fiscal_year_start
9847                                                   AND b.fiscal_year_end
9848 			      )
9849                         AND a.country_id = c.location_hierarchy_id
9850                         AND a.campaign_id = d.campaign_id
9851                         AND b.period_type = 'YEAR'
9852                         AND trunc(a.start_date_time) >= l_min_start_date
9853                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date)) >=
9854                                                            b.fiscal_year_start
9855                    GROUP BY a.campaign_id ,c.area2_code,
9856                             a.country_id,
9857                             NVL(d.business_unit_id, 0),
9858                             DECODE(a.activity_type_code,
9859                                    'EVENTS', -9999,
9860                                    a.activity_id
9861                                   ),
9862                             b.fiscal_year
9863 ------------------------------------------------------------------------------------------
9864                    UNION ALL
9865 ------------------------------------------------------------------------------------------
9866                    SELECT
9867                         /*+ use_hash(a)   use_hash(b)  use_hash(c)  */
9868                              a.campaign_id campaign_id,
9869 			     c.area2_code schedule_area2,
9870                              a.schedule_country schedule_country,
9871                              NVL(a.business_unit_id, 0) business_unit_id,
9872                              a.schedule_activity_id schedule_activity_id,
9873                              'YEAR' aggregate_by,
9874                              'Z' display_type,
9875                              b.fiscal_year fiscal_year,
9876                              'N' fiscal_qtr,
9877                              'N' fiscal_month,
9878                              0 curr_count,
9879                              0 pre_count,
9880                              0 curr_started,
9881                              0 pre_started,
9882                              0 curr_ended,
9883                              0 pre_ended,
9884                              0 campaign_count,
9885                              SUM(forecasted_responses) forecasted_responses,
9886                              SUM(targeted_customers) targeted_customers,
9887                              0 prev_targeted_customers,
9888                              SUM(budget_approved) budget_approved
9889                        FROM bim_r_camp_daily_facts a,
9890                             bim_dates b,
9891                             jtf_loc_hierarchies_b c
9892                       WHERE a.schedule_country = c.location_hierarchy_id
9893         --***
9894                      AND ( trunc(a.start_date) < b.fiscal_year_start
9895 		          or
9896 			  trunc(a.start_date) between b.fiscal_year_start
9897                                                   AND b.fiscal_year_end
9898 			  )
9899 
9900                         AND trunc(a.end_date) >= b.fiscal_year_start
9901                         AND b.period_type = 'YEAR'
9902                    GROUP BY a.campaign_id ,c.area2_code,
9903                             a.schedule_country,
9904                             NVL(a.business_unit_id, 0),
9905                             a.schedule_activity_id,
9906                             b.fiscal_year
9907 ------------------------------------------------------------------------------------------
9908                    UNION ALL
9909 ------------------------------------------------------------------------------------------
9910                    SELECT
9911                         /*+ use_hash(a)   use_hash(b)  use_hash(c)  */
9912                              a.campaign_id campaign_id,
9913 			     c.area2_code schedule_area2,
9914                              a.schedule_country schedule_country,
9915                              NVL(a.business_unit_id, 0) business_unit_id,
9916                              a.schedule_activity_id schedule_activity_id,
9917                              'YEAR' aggregate_by,
9918                              'Z' display_type,
9919                              b.fiscal_year fiscal_year,
9920                              'N' fiscal_qtr,
9921                              'N' fiscal_month,
9922                              0 curr_count,
9923                              0 pre_count,
9924                              0 curr_started,
9925                              0 pre_started,
9926                              0 curr_ended,
9927                              0 pre_ended,
9928                              0 campaign_count,
9929                              0 forecasted_responses,
9930                              0 targeted_customers,
9931                              SUM(targeted_customers) prev_targeted_customers,
9932                              0 budget_approved
9933                        FROM bim_r_camp_daily_facts a,
9934                             bim_dates b,
9935                             jtf_loc_hierarchies_b c
9936                       WHERE a.schedule_country = c.location_hierarchy_id
9937          --***
9938 	              AND (trunc(a.start_date) < b.pre_fiscal_year_start
9939 		            or
9940 			   trunc(a.start_date) BETWEEN b.pre_fiscal_year_start
9941                                                   AND(b.fiscal_year_start - 1
9942                                                      )
9943 			    )
9944                         AND trunc(a.end_date) >= b.pre_fiscal_year_start
9945                         AND b.period_type = 'YEAR'
9946                    GROUP BY a.campaign_id ,
9947 		            c.area2_code,
9948                             a.schedule_country,
9949                             NVL(a.business_unit_id, 0),
9950                             a.schedule_activity_id,
9951                             b.fiscal_year)
9952          GROUP BY schedule_area2,
9953                   schedule_country,
9954                   business_unit_id,
9955                   schedule_activity_id,
9956                   aggregate_by,
9957                   display_type,
9958                   fiscal_year,
9959                   fiscal_qtr,
9960                   fiscal_month;
9961 
9962 	---------union all admin end
9963 
9964 
9965 
9966 
9967 
9968  --commit;
9969 
9970 
9971       ams_utility_pvt.write_conc_log('BIM_CAMP_COLLECTION: SUMMARY ACT QTR INSERT END');
9972 ---------------------------------------------------------------------------------------
9973 -- START OF CAMP SUMMARY DATA
9974 --------------------------------------------------------------------------------------
9975       ams_utility_pvt.write_conc_log('BIM_CAMP_COLLECTION: SUMMARY CAMP MONTH INSERT START');
9976       INSERT INTO  /*+ append parallel(i,5) */
9977            bim_r_camp_collection i
9978                   (campaign_area1,
9979                    campaign_area2,
9980                    schedule_area1,
9981                    schedule_area2,
9982                    campaign_country,
9983                    schedule_country,
9984                    business_unit_id,
9985                    resource_id,
9986                    campaign_type,
9987                    campaign_id,
9988                    schedule_activity_type,
9989                    campaign_status,
9990                    schedule_status,
9991                    aggregate_by,
9992                    display_type,
9993                    YEAR,
9994                    qtr,
9995                    MONTH,
9996                    report_type,
9997                    current_count_value,
9998                    current_started_value,
9999                    current_ended_value,
10000                    previous_count_value,
10001                    previous_started_value,
10002                    previous_ended_value,
10003                    campaign_count,
10004                    forecasted_responses,
10005                    targeted_customers,
10006                    prev_targeted_customers,
10007                    budget_approved
10008                   )
10009          SELECT   /* +INDEX_FFS(R,BIM_DBI_U2) */
10010 	          'N' campaign_area1,
10011                   'N' campaign_area2,
10012                   'N' schedule_area1,
10013                   schedule_area2,
10014                   'N' campaign_country,
10015                   schedule_country,
10016                   business_unit_id,
10017                   resource_id,
10018                   'N' campaign_type,
10019                   campaign_id,
10020                   'N' schedule_activity_type,
10021                   'N' campaign_status,
10022                   'N' schedule_status,
10023                   aggregate_by,
10024                   display_type,
10025                   fiscal_year YEAR,
10026                   fiscal_qtr qtr,
10027                   fiscal_month MONTH,
10028                   'CAMP' report_type,
10029                   SUM(curr_count) current_count_value,
10030                   SUM(curr_started) current_started_value,
10031                   SUM(curr_ended) current_ended_value,
10032                   SUM(pre_count) previous_count_value,
10033                   SUM(pre_started) previous_started_value,
10034                   SUM(pre_ended) previous_ended_value,
10035                   SUM(campaign_count) campaign_count,
10036                   SUM(forecasted_responses) forecasted_responses,
10037                   SUM(targeted_customers) targeted_customers,
10038                   SUM(prev_targeted_customers) prev_targeted_customers,
10039                   SUM(budget_approved) budget_approved
10040              FROM (
10041 -- FOR THE PERIOD TYPE MONTH
10042                    SELECT
10043                         /*+ use_hash(a)  use_hash(b)  use_hash(c) use_hash(d) */
10044                              c.area2_code schedule_area2,
10045                              d.city_id schedule_country,
10046                              NVL(d.business_unit_id, 0) business_unit_id,
10047                              a.campaign_id campaign_id,
10048                              'MONTH' aggregate_by,
10049                              'Z' display_type,
10050                              b.fiscal_year fiscal_year,
10051                              b.fiscal_qtr fiscal_qtr,
10052                              b.fiscal_month fiscal_month,
10053                              COUNT(a.schedule_id) curr_count,
10054                              0 pre_count,
10055                              0 curr_started,
10056                              0 pre_started,
10057                              0 curr_ended,
10058                              0 pre_ended,
10059                              0 campaign_count,
10060                              0 forecasted_responses,
10061                              0 targeted_customers,
10062                              0 prev_targeted_customers,
10063                              0 budget_approved
10064                        FROM ams_campaign_schedules_b a,
10065                             bim_dates b,
10066                             jtf_loc_hierarchies_b c,
10067                             ams_campaigns_all_b d
10068                       WHERE a.status_code IN
10069                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
10070     --***
10071 	                AND (trunc(a.start_date_time) < b.fiscal_month_start
10072 			     or
10073 			     trunc(a.start_date_time) BETWEEN b.fiscal_month_start
10074                                                   AND b.fiscal_month_end
10075 			     )
10076                         AND d.city_id = c.location_hierarchy_id
10077                         AND a.campaign_id = d.campaign_id
10078                         AND b.period_type = 'MONTH'
10079                         AND trunc(a.start_date_time) >= l_min_start_date
10080                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date)) >=
10081                                                           b.fiscal_month_start
10082                    GROUP BY c.area2_code,
10083                             d.city_id,
10084                             NVL(d.business_unit_id, 0),
10085                             a.campaign_id,
10086                             b.fiscal_year,
10087                             b.fiscal_qtr,
10088                             b.fiscal_month
10089 ------------------------------------------------------------------------------------------
10090                    UNION ALL
10091 ------------------------------------------------------------------------------------------
10092                    SELECT
10093                         /*+ use_hash(a)   use_hash(b)  use_hash(c)  */
10094                              c.area2_code schedule_area2,
10095                              a.campaign_country schedule_country,
10096                              NVL(a.business_unit_id, 0) business_unit_id,
10097                              a.campaign_id campaign_id,
10098                              'MONTH' aggregate_by,
10099                              'Z' display_type,
10100                              b.fiscal_year fiscal_year,
10101                              b.fiscal_qtr fiscal_qtr,
10102                              b.fiscal_month fiscal_month,
10103                              0 curr_count,
10104                              0 pre_count,
10105                              0 curr_started,
10106                              0 pre_started,
10107                              0 curr_ended,
10108                              0 pre_ended,
10109                              0 campaign_count,
10110                              SUM(forecasted_responses) forecasted_responses,
10111                              SUM(targeted_customers) targeted_customers,
10112                              0 prev_targeted_customers,
10113                              SUM(budget_approved) budget_approved
10114                        FROM bim_r_camp_daily_facts a,
10115                             bim_dates b,
10116                             jtf_loc_hierarchies_b c
10117                       WHERE a.campaign_country = c.location_hierarchy_id
10118      --***
10119 	               AND ( trunc(a.start_date) < b.fiscal_month_start
10120 		                  or
10121 			    trunc(a.start_date)  between b.fiscal_month_start and b.fiscal_month_end
10122                            )
10123                         AND trunc(a.end_date) >= b.fiscal_month_start
10124                         AND b.period_type = 'MONTH'
10125                    GROUP BY c.area2_code,
10126                             a.campaign_country,
10127                             NVL(a.business_unit_id, 0),
10128                             a.campaign_id,
10129                             b.fiscal_year,
10130                             b.fiscal_qtr,
10131                             b.fiscal_month)
10132  ,ams_act_access_denorm R
10133 			    where campaign_id=r.object_id
10134 			    and r.object_type='CAMP'
10135          GROUP BY schedule_area2,
10136                   schedule_country,
10137                   business_unit_id,
10138                   resource_id,
10139                   campaign_id,
10140                   aggregate_by,
10141                   display_type,
10142                   fiscal_year,
10143                   fiscal_qtr,
10144                   fiscal_month
10145            HAVING SUM(curr_count) > 0
10146                OR SUM(curr_started) > 0
10147                OR SUM(curr_ended) > 0
10148                OR SUM(pre_count) > 0
10149                OR SUM(pre_started) > 0
10150                OR SUM(pre_ended) > 0
10151                OR SUM(targeted_customers) > 0
10152                OR SUM(prev_targeted_customers) > 0
10153                OR SUM(forecasted_responses) > 0
10154                OR SUM(budget_approved) <> 0
10155 
10156 			  ---------- for admin
10157 				  union all
10158 				  -------------
10159 
10160 
10161 				  SELECT
10162 	          'N' campaign_area1,
10163                   'N' campaign_area2,
10164                   'N' schedule_area1,
10165                   schedule_area2,
10166                   'N' campaign_country,
10167                   schedule_country,
10168                   business_unit_id,
10169                   -1 resource_id,
10170                   'N' campaign_type,
10171                   campaign_id,
10172                   'N' schedule_activity_type,
10173                   'N' campaign_status,
10174                   'N' schedule_status,
10175                   aggregate_by,
10176                   display_type,
10177                   fiscal_year YEAR,
10178                   fiscal_qtr qtr,
10179                   fiscal_month MONTH,
10180                   'CAMP' report_type,
10181                   SUM(curr_count) current_count_value,
10182                   SUM(curr_started) current_started_value,
10183                   SUM(curr_ended) current_ended_value,
10184                   SUM(pre_count) previous_count_value,
10185                   SUM(pre_started) previous_started_value,
10186                   SUM(pre_ended) previous_ended_value,
10187                   SUM(campaign_count) campaign_count,
10188                   SUM(forecasted_responses) forecasted_responses,
10189                   SUM(targeted_customers) targeted_customers,
10190                   SUM(prev_targeted_customers) prev_targeted_customers,
10191                   SUM(budget_approved) budget_approved
10192              FROM (
10193 -- FOR THE PERIOD TYPE MONTH
10194                    SELECT
10195                         /*+ use_hash(a)  use_hash(b)  use_hash(c) use_hash(d) */
10196                              c.area2_code schedule_area2,
10197                              d.city_id schedule_country,
10198                              NVL(d.business_unit_id, 0) business_unit_id,
10199                              a.campaign_id campaign_id,
10200                              'MONTH' aggregate_by,
10201                              'Z' display_type,
10202                              b.fiscal_year fiscal_year,
10203                              b.fiscal_qtr fiscal_qtr,
10204                              b.fiscal_month fiscal_month,
10205                              COUNT(a.schedule_id) curr_count,
10206                              0 pre_count,
10207                              0 curr_started,
10208                              0 pre_started,
10209                              0 curr_ended,
10210                              0 pre_ended,
10211                              0 campaign_count,
10212                              0 forecasted_responses,
10213                              0 targeted_customers,
10214                              0 prev_targeted_customers,
10215                              0 budget_approved
10216                        FROM ams_campaign_schedules_b a,
10217                             bim_dates b,
10218                             jtf_loc_hierarchies_b c,
10219                             ams_campaigns_all_b d
10220                       WHERE a.status_code IN
10221                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
10222 
10223  --***
10224 	                AND (trunc(a.start_date_time) < b.fiscal_month_start
10225 			     or
10226 			     trunc(a.start_date_time) BETWEEN b.fiscal_month_start
10227                                                   AND b.fiscal_month_end
10228 			     )
10229                         AND d.city_id = c.location_hierarchy_id
10230                         AND a.campaign_id = d.campaign_id
10231                         AND b.period_type = 'MONTH'
10232                         AND trunc(a.start_date_time) >= l_min_start_date
10233                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date)) >=
10234                                                           b.fiscal_month_start
10235                    GROUP BY c.area2_code,
10236                             d.city_id,
10237                             NVL(d.business_unit_id, 0),
10238                             a.campaign_id,
10239                             b.fiscal_year,
10240                             b.fiscal_qtr,
10241                             b.fiscal_month
10242 ------------------------------------------------------------------------------------------
10243                    UNION ALL
10244 ------------------------------------------------------------------------------------------
10245                    SELECT
10246                         /*+ use_hash(a)   use_hash(b)  use_hash(c)  */
10247                              c.area2_code schedule_area2,
10248                              a.campaign_country schedule_country,
10249                              NVL(a.business_unit_id, 0) business_unit_id,
10250                              a.campaign_id campaign_id,
10251                              'MONTH' aggregate_by,
10252                              'Z' display_type,
10253                              b.fiscal_year fiscal_year,
10254                              b.fiscal_qtr fiscal_qtr,
10255                              b.fiscal_month fiscal_month,
10256                              0 curr_count,
10257                              0 pre_count,
10258                              0 curr_started,
10259                              0 pre_started,
10260                              0 curr_ended,
10261                              0 pre_ended,
10262                              0 campaign_count,
10263                              SUM(forecasted_responses) forecasted_responses,
10264                              SUM(targeted_customers) targeted_customers,
10265                              0 prev_targeted_customers,
10266                              SUM(budget_approved) budget_approved
10267                        FROM bim_r_camp_daily_facts a,
10268                             bim_dates b,
10269                             jtf_loc_hierarchies_b c
10270                       WHERE a.campaign_country = c.location_hierarchy_id
10271             --***
10272 	               AND ( trunc(a.start_date) < b.fiscal_month_start
10273 		                  or
10274 			    trunc(a.start_date)  between b.fiscal_month_start and b.fiscal_month_end
10275                            )
10276                         AND trunc(a.end_date) >= b.fiscal_month_start
10277                         AND b.period_type = 'MONTH'
10278                    GROUP BY c.area2_code,
10279                             a.campaign_country,
10280                             NVL(a.business_unit_id, 0),
10281                             a.campaign_id,
10282                             b.fiscal_year,
10283                             b.fiscal_qtr,
10284                             b.fiscal_month)
10285          GROUP BY schedule_area2,
10286                   schedule_country,
10287                   business_unit_id,
10288                   campaign_id,
10289                   aggregate_by,
10290                   display_type,
10291                   fiscal_year,
10292                   fiscal_qtr,
10293                   fiscal_month
10294            HAVING SUM(curr_count) > 0
10295                OR SUM(curr_started) > 0
10296                OR SUM(curr_ended) > 0
10297                OR SUM(pre_count) > 0
10298                OR SUM(pre_started) > 0
10299                OR SUM(pre_ended) > 0
10300                OR SUM(targeted_customers) > 0
10301                OR SUM(prev_targeted_customers) > 0
10302                OR SUM(forecasted_responses) > 0
10303                OR SUM(budget_approved) <> 0;
10304 
10305 			   ---------union all admin end
10306 
10307 
10308  --commit;
10309 
10310       ams_utility_pvt.write_conc_log('BIM_CAMP_COLLECTION: SUMMARY CAMP QTR INSERT START');
10311       INSERT INTO  /*+ append parallel(i,5) */
10312              bim_r_camp_collection i
10313                   (campaign_area1,
10314                    campaign_area2,
10315                    schedule_area1,
10316                    schedule_area2,
10317                    campaign_country,
10318                    schedule_country,
10319                    business_unit_id,
10320                    resource_id,
10321                    campaign_type,
10322                    campaign_id,
10323                    schedule_activity_type,
10324                    campaign_status,
10325                    schedule_status,
10326                    aggregate_by,
10327                    display_type,
10328                    YEAR,
10329                    qtr,
10330                    MONTH,
10331                    report_type,
10332                    current_count_value,
10333                    current_started_value,
10334                    current_ended_value,
10335                    previous_count_value,
10336                    previous_started_value,
10337                    previous_ended_value,
10338                    campaign_count,
10339                    forecasted_responses,
10340                    targeted_customers,
10341                    prev_targeted_customers,
10342                    budget_approved
10343                   )
10344          SELECT
10345 /* +INDEX_FFS(R,BIM_DBI_U2) */
10346                   'N' campaign_area1,
10347                   'N' campaign_area2,
10348                   'N' schedule_area1,
10349                   schedule_area2,
10350                   'N' campaign_country,
10351                   schedule_country,
10352                   business_unit_id,
10353                   resource_id,
10354                   'N' campaign_type,
10355                   campaign_id,
10356                   'N' schedule_activity_type,
10357                   'N' campaign_status,
10358                   'N' schedule_status,
10359                   aggregate_by,
10360                   display_type,
10361                   fiscal_year YEAR,
10362                   fiscal_qtr qtr,
10363                   fiscal_month MONTH,
10364                   'CAMP' report_type,
10365                   SUM(curr_count) current_count_value,
10366                   SUM(curr_started) current_started_value,
10367                   SUM(curr_ended) current_ended_value,
10368                   SUM(pre_count) previous_count_value,
10369                   SUM(pre_started) previous_started_value,
10370                   SUM(pre_ended) previous_ended_value,
10371                   SUM(campaign_count) campaign_count,
10372                   SUM(forecasted_responses) forecasted_responses,
10373                   SUM(targeted_customers) targeted_customers,
10374                   SUM(prev_targeted_customers) prev_targeted_customers,
10375                   SUM(budget_approved) budget_approved
10376              FROM (
10377 -- FOR THE PERIOD TYPE QUARTER
10378                    SELECT
10379                         /*+ use_hash(a)   use_hash(b)  use_hash(c) use_hash(d) */
10380                              c.area2_code schedule_area2,
10381                              d.city_id schedule_country,
10382                              NVL(d.business_unit_id, 0) business_unit_id,
10383                              a.campaign_id campaign_id,
10384                              'QTR' aggregate_by,
10385                              'Z' display_type,
10386                              b.fiscal_year fiscal_year,
10387                              b.fiscal_qtr fiscal_qtr,
10388                              'N' fiscal_month,
10389                              COUNT(a.schedule_id) curr_count,
10390                              0 pre_count,
10391                              0 curr_started,
10392                              0 pre_started,
10393                              0 curr_ended,
10394                              0 pre_ended,
10395                              0 campaign_count,
10396                              0 forecasted_responses,
10397                              0 targeted_customers,
10398                              0 prev_targeted_customers,
10399                              0 budget_approved
10400                        FROM ams_campaign_schedules_b a,
10401                             bim_dates b,
10402                             jtf_loc_hierarchies_b c,
10403                             ams_campaigns_all_b d
10404                       WHERE a.status_code IN
10405                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
10406       --***
10407 	                 AND (trunc(a.start_date_time) < b.fiscal_qtr_start
10408 			      or
10409 			      trunc(a.start_date_time) BETWEEN b.fiscal_qtr_start
10410 			                               and b.fiscal_qtr_end
10411 			      )
10412                         AND d.city_id = c.location_hierarchy_id
10413                         AND a.campaign_id = d.campaign_id
10414                         AND b.period_type = 'QUARTER'
10415                         AND trunc(a.start_date_time) >= l_min_start_date
10416                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date)) >=
10417                                                             b.fiscal_qtr_start
10418                    GROUP BY c.area2_code,
10419                             d.city_id,
10420                             NVL(d.business_unit_id, 0),
10421                             a.campaign_id,
10422                             b.fiscal_year,
10423                             b.fiscal_qtr
10424 ------------------------------------------------------------------------------------------
10425                    UNION ALL
10426 ------------------------------------------------------------------------------------------
10427                    SELECT
10428                         /*+ use_hash(a)  use_hash(b)  use_hash(c) */
10429                              c.area2_code schedule_area2,
10430                              a.campaign_country schedule_country,
10431                              NVL(a.business_unit_id, 0) business_unit_id,
10432                              a.campaign_id campaign_id,
10433                              'QTR' aggregate_by,
10434                              'Z' display_type,
10435                              b.fiscal_year fiscal_year,
10436                              b.fiscal_qtr fiscal_qtr,
10437                              'N' fiscal_month,
10438                              0 curr_count,
10439                              0 pre_count,
10440                              0 curr_started,
10441                              0 pre_started,
10442                              0 curr_ended,
10443                              0 pre_ended,
10444                              0 campaign_count,
10445                              SUM(forecasted_responses) forecasted_responses,
10446                              SUM(targeted_customers) targeted_customers,
10447                              0 prev_targeted_customers,
10448                              SUM(budget_approved) budget_approved
10449                        FROM bim_r_camp_daily_facts a,
10450                             bim_dates b,
10451                             jtf_loc_hierarchies_b c
10452                       WHERE a.campaign_country = c.location_hierarchy_id
10453      --***
10454                            AND (trunc(a.start_date) < b.fiscal_qtr_start
10455 			        or
10456                                 trunc(a.start_date) BETWEEN b.fiscal_qtr_start
10457 			                             and b.fiscal_qtr_end
10458 			      )
10459                         AND trunc(a.end_date) >= b.fiscal_qtr_start
10460                         AND b.period_type = 'QUARTER'
10461                    GROUP BY c.area2_code,
10462                             a.campaign_country,
10463                             NVL(a.business_unit_id, 0),
10464                             a.campaign_id,
10465                             b.fiscal_year,
10466                             b.fiscal_qtr) ,ams_act_access_denorm R
10467 			    where campaign_id=r.object_id
10468 			    and r.object_type='CAMP'
10469          GROUP BY schedule_area2,
10470                   schedule_country,
10471                   business_unit_id,
10472                   resource_id,
10473                   campaign_id,
10474                   aggregate_by,
10475                   display_type,
10476                   fiscal_year,
10477                   fiscal_qtr,
10478                   fiscal_month
10479            HAVING SUM(curr_count) > 0
10480                OR SUM(curr_started) > 0
10481                OR SUM(curr_ended) > 0
10482                OR SUM(pre_count) > 0
10483                OR SUM(pre_started) > 0
10484                OR SUM(pre_ended) > 0
10485                OR SUM(targeted_customers) > 0
10486                OR SUM(prev_targeted_customers) > 0
10487                OR SUM(forecasted_responses) > 0
10488                OR SUM(budget_approved) <> 0
10489 
10490   ---------- for admin
10491 				  union all
10492 				  -------------
10493 
10494 				SELECT
10495 
10496                   'N' campaign_area1,
10497                   'N' campaign_area2,
10498                   'N' schedule_area1,
10499                   schedule_area2,
10500                   'N' campaign_country,
10501                   schedule_country,
10502                   business_unit_id,
10503                   -1 resource_id,
10504                   'N' campaign_type,
10505                   campaign_id,
10506                   'N' schedule_activity_type,
10507                   'N' campaign_status,
10508                   'N' schedule_status,
10509                   aggregate_by,
10510                   display_type,
10511                   fiscal_year YEAR,
10512                   fiscal_qtr qtr,
10513                   fiscal_month MONTH,
10514                   'CAMP' report_type,
10515                   SUM(curr_count) current_count_value,
10516                   SUM(curr_started) current_started_value,
10517                   SUM(curr_ended) current_ended_value,
10518                   SUM(pre_count) previous_count_value,
10519                   SUM(pre_started) previous_started_value,
10520                   SUM(pre_ended) previous_ended_value,
10521                   SUM(campaign_count) campaign_count,
10522                   SUM(forecasted_responses) forecasted_responses,
10523                   SUM(targeted_customers) targeted_customers,
10524                   SUM(prev_targeted_customers) prev_targeted_customers,
10525                   SUM(budget_approved) budget_approved
10526              FROM (
10527 -- FOR THE PERIOD TYPE QUARTER
10528                    SELECT
10529                         /*+ use_hash(a)   use_hash(b)  use_hash(c) use_hash(d) */
10530                              c.area2_code schedule_area2,
10531                              d.city_id schedule_country,
10532                              NVL(d.business_unit_id, 0) business_unit_id,
10533                              a.campaign_id campaign_id,
10534                              'QTR' aggregate_by,
10535                              'Z' display_type,
10536                              b.fiscal_year fiscal_year,
10537                              b.fiscal_qtr fiscal_qtr,
10538                              'N' fiscal_month,
10539                              COUNT(a.schedule_id) curr_count,
10540                              0 pre_count,
10541                              0 curr_started,
10542                              0 pre_started,
10543                              0 curr_ended,
10544                              0 pre_ended,
10545                              0 campaign_count,
10546                              0 forecasted_responses,
10547                              0 targeted_customers,
10548                              0 prev_targeted_customers,
10549                              0 budget_approved
10550                        FROM ams_campaign_schedules_b a,
10551                             bim_dates b,
10552                             jtf_loc_hierarchies_b c,
10553                             ams_campaigns_all_b d
10554                       WHERE a.status_code IN
10555                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
10556      --***
10557 	                 AND (trunc(a.start_date_time) < b.fiscal_qtr_start
10558 			      or
10559 			      trunc(a.start_date_time) BETWEEN b.fiscal_qtr_start
10560 			                             and b.fiscal_qtr_end
10561 			      )
10562                         AND d.city_id = c.location_hierarchy_id
10563                         AND a.campaign_id = d.campaign_id
10564                         AND b.period_type = 'QUARTER'
10565                         AND trunc(a.start_date_time) >= l_min_start_date
10566                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date)) >=
10567                                                             b.fiscal_qtr_start
10568                    GROUP BY c.area2_code,
10569                             d.city_id,
10570                             NVL(d.business_unit_id, 0),
10571                             a.campaign_id,
10572                             b.fiscal_year,
10573                             b.fiscal_qtr
10574 ------------------------------------------------------------------------------------------
10575                    UNION ALL
10576 ------------------------------------------------------------------------------------------
10577                    SELECT
10578                         /*+ use_hash(a)  use_hash(b)  use_hash(c) */
10579                              c.area2_code schedule_area2,
10580                              a.campaign_country schedule_country,
10581                              NVL(a.business_unit_id, 0) business_unit_id,
10582                              a.campaign_id campaign_id,
10583                              'QTR' aggregate_by,
10584                              'Z' display_type,
10585                              b.fiscal_year fiscal_year,
10586                              b.fiscal_qtr fiscal_qtr,
10587                              'N' fiscal_month,
10588                              0 curr_count,
10589                              0 pre_count,
10590                              0 curr_started,
10591                              0 pre_started,
10592                              0 curr_ended,
10593                              0 pre_ended,
10594                              0 campaign_count,
10595                              SUM(forecasted_responses) forecasted_responses,
10596                              SUM(targeted_customers) targeted_customers,
10597                              0 prev_targeted_customers,
10598                              SUM(budget_approved) budget_approved
10599                        FROM bim_r_camp_daily_facts a,
10600                             bim_dates b,
10601                             jtf_loc_hierarchies_b c
10602                       WHERE a.campaign_country = c.location_hierarchy_id
10603     --***
10604                            AND (trunc(a.start_date) < b.fiscal_qtr_start
10605 			        or
10606                                 trunc(a.start_date) BETWEEN b.fiscal_qtr_start
10607 			                             and b.fiscal_qtr_end
10608 			      )
10609                         AND trunc(a.end_date) >= b.fiscal_qtr_start
10610                         AND b.period_type = 'QUARTER'
10611                    GROUP BY c.area2_code,
10612                             a.campaign_country,
10613                             NVL(a.business_unit_id, 0),
10614                             a.campaign_id,
10615                             b.fiscal_year,
10616                             b.fiscal_qtr)
10617 			      GROUP BY schedule_area2,
10618                   schedule_country,
10619                   business_unit_id,
10620                   campaign_id,
10621                   aggregate_by,
10622                   display_type,
10623                   fiscal_year,
10624                   fiscal_qtr,
10625                   fiscal_month
10626            HAVING SUM(curr_count) > 0
10627                OR SUM(curr_started) > 0
10628                OR SUM(curr_ended) > 0
10629                OR SUM(pre_count) > 0
10630                OR SUM(pre_started) > 0
10631                OR SUM(pre_ended) > 0
10632                OR SUM(targeted_customers) > 0
10633                OR SUM(prev_targeted_customers) > 0
10634                OR SUM(forecasted_responses) > 0
10635                OR SUM(budget_approved) <> 0;
10636 
10637 
10638 ---------union all admin end
10639 
10640 
10641 --commit;
10642 
10643       ams_utility_pvt.write_conc_log('BIM_CAMP_COLLECTION: SUMMARY CAMP QTR INSERT END');
10644       ams_utility_pvt.write_conc_log('BIM_CAMP_COLLECTION: SUMMARY CAMP YEAR INSERT START');
10645       INSERT INTO  /*+ append parallel(i,5) */
10646            bim_r_camp_collection i
10647                   (campaign_area1,
10648                    campaign_area2,
10649                    schedule_area1,
10650                    schedule_area2,
10651                    campaign_country,
10652                    schedule_country,
10653                    business_unit_id,
10654                    resource_id,
10655                    campaign_type,
10656                    campaign_id,
10657                    schedule_activity_type,
10658                    campaign_status,
10659                    schedule_status,
10660                    aggregate_by,
10661                    display_type,
10662                    YEAR,
10663                    qtr,
10664                    MONTH,
10665                    report_type,
10666                    current_count_value,
10667                    current_started_value,
10668                    current_ended_value,
10669                    previous_count_value,
10670                    previous_started_value,
10671                    previous_ended_value,
10672                    campaign_count,
10673                    forecasted_responses,
10674                    targeted_customers,
10675                    prev_targeted_customers,
10676                    budget_approved
10677                   )
10678          SELECT  /* +INDEX_FFS(R,BIM_DBI_U2) */
10679 	          'N' campaign_area1,
10680                   'N' campaign_area2,
10681                   'N' schedule_area1,
10682                   schedule_area2,
10683                   'N' campaign_country,
10684                   schedule_country,
10685                   business_unit_id,
10686                   resource_id,
10687                   'N' campaign_type,
10688                   campaign_id,
10689                   'N' schedule_activity_type,
10690                   'N' campaign_status,
10691                   'N' schedule_status,
10692                   aggregate_by,
10693                   display_type,
10694                   fiscal_year YEAR,
10695                   fiscal_qtr qtr,
10696                   fiscal_month MONTH,
10697                   'CAMP' report_type,
10698                   SUM(curr_count) current_count_value,
10699                   SUM(curr_started) current_started_value,
10700                   SUM(curr_ended) current_ended_value,
10701                   SUM(pre_count) previous_count_value,
10702                   SUM(pre_started) previous_started_value,
10703                   SUM(pre_ended) previous_ended_value,
10704                   SUM(campaign_count) campaign_count,
10705                   SUM(forecasted_responses) forecasted_responses,
10706                   SUM(targeted_customers) targeted_customers,
10707                   SUM(prev_targeted_customers) prev_targeted_customers,
10708                   SUM(budget_approved) budget_approved
10709              FROM (
10710 -- FOR THE PERIOD TYPE YEAR
10711                    SELECT
10712                         /*+ use_hash(a)  use_hash(b)  use_hash(c) use_hash(d) */
10713                              c.area2_code schedule_area2,
10714                              d.city_id schedule_country,
10715                              NVL(d.business_unit_id, 0) business_unit_id,
10716                              a.campaign_id campaign_id,
10717                              'YEAR' aggregate_by,
10718                              'Z' display_type,
10719                              b.fiscal_year fiscal_year,
10720                              'N' fiscal_qtr,
10721                              'N' fiscal_month,
10722                              COUNT(a.schedule_id) curr_count,
10723                              0 pre_count,
10724                              0 curr_started,
10725                              0 pre_started,
10726                              0 curr_ended,
10727                              0 pre_ended,
10728                              0 campaign_count,
10729                              0 forecasted_responses,
10730                              0 targeted_customers,
10731                              0 prev_targeted_customers,
10732                              0 budget_approved
10733                        FROM ams_campaign_schedules_b a,
10734                             bim_dates b,
10735                             jtf_loc_hierarchies_b c,
10736                             ams_campaigns_all_b d
10737                       WHERE a.status_code IN
10738                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
10739          --***
10740                          AND (trunc(a.start_date_time) < b.fiscal_year_start
10741 			     or
10742 			     trunc(a.start_date_time) BETWEEN b.fiscal_year_start
10743                                                   AND b.fiscal_year_end
10744 			      )
10745                         AND d.city_id = c.location_hierarchy_id
10746                         AND a.campaign_id = d.campaign_id
10747                         AND b.period_type = 'YEAR'
10748                         AND trunc(a.start_date_time) >= l_min_start_date
10749                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date)) >=
10750                                                            b.fiscal_year_start
10751                    GROUP BY c.area2_code,
10752                             d.city_id,
10753                             NVL(d.business_unit_id, 0),
10754                             a.campaign_id,
10755                             b.fiscal_year
10756 ------------------------------------------------------------------------------------------
10757                    UNION ALL
10758 ------------------------------------------------------------------------------------------
10759                    SELECT
10760                         /*+ use_hash(a)  use_hash(b)  use_hash(c)  */
10761                              c.area2_code schedule_area2,
10762                              a.campaign_country schedule_country,
10763                              NVL(a.business_unit_id, 0) business_unit_id,
10764                              a.campaign_id campaign_id,
10765                              'YEAR' aggregate_by,
10766                              'Z' display_type,
10767                              b.fiscal_year fiscal_year,
10768                              'N' fiscal_qtr,
10769                              'N' fiscal_month,
10770                              0 curr_count,
10771                              0 pre_count,
10772                              0 curr_started,
10773                              0 pre_started,
10774                              0 curr_ended,
10775                              0 pre_ended,
10776                              0 campaign_count,
10777                              SUM(forecasted_responses) forecasted_responses,
10778                              SUM(targeted_customers) targeted_customers,
10779                              0 prev_targeted_customers,
10780                              SUM(budget_approved) budget_approved
10781                        FROM bim_r_camp_daily_facts a,
10782                             bim_dates b,
10783                             jtf_loc_hierarchies_b c
10784                       WHERE a.campaign_country = c.location_hierarchy_id
10785           --***
10786                      AND ( trunc(a.start_date) < b.fiscal_year_start
10787 		          or
10788 			  trunc(a.start_date) between b.fiscal_year_start
10789                                                   AND b.fiscal_year_end
10790 			  )
10791                         AND trunc(a.end_date) >= b.fiscal_year_start
10792                         AND b.period_type = 'YEAR'
10793                    GROUP BY c.area2_code,
10794                             a.campaign_country,
10795                             NVL(a.business_unit_id, 0),
10796                             a.campaign_id,
10797                             b.fiscal_year),ams_act_access_denorm R
10798 			    where campaign_id=r.object_id
10799 			    and r.object_type='CAMP'
10800          GROUP BY schedule_area2,
10801                   schedule_country,
10802                   business_unit_id,
10803                   resource_id,
10804                   campaign_id,
10805                   aggregate_by,
10806                   display_type,
10807                   fiscal_year,
10808                   fiscal_qtr,
10809                   fiscal_month
10810            HAVING SUM(curr_count) > 0
10811                OR SUM(curr_started) > 0
10812                OR SUM(curr_ended) > 0
10813                OR SUM(pre_count) > 0
10814                OR SUM(pre_started) > 0
10815                OR SUM(pre_ended) > 0
10816                OR SUM(targeted_customers) > 0
10817                OR SUM(prev_targeted_customers) > 0
10818                OR SUM(forecasted_responses) > 0
10819                OR SUM(budget_approved) <> 0
10820 
10821 
10822 
10823 			     ---------- for admin
10824 				  union all
10825 				  -------------
10826 
10827 				  SELECT
10828 	          'N' campaign_area1,
10829                   'N' campaign_area2,
10830                   'N' schedule_area1,
10831                   schedule_area2,
10832                   'N' campaign_country,
10833                   schedule_country,
10834                   business_unit_id,
10835                   -1 resource_id,
10836                   'N' campaign_type,
10837                   campaign_id,
10838                   'N' schedule_activity_type,
10839                   'N' campaign_status,
10840                   'N' schedule_status,
10841                   aggregate_by,
10842                   display_type,
10843                   fiscal_year YEAR,
10844                   fiscal_qtr qtr,
10845                   fiscal_month MONTH,
10846                   'CAMP' report_type,
10847                   SUM(curr_count) current_count_value,
10848                   SUM(curr_started) current_started_value,
10849                   SUM(curr_ended) current_ended_value,
10850                   SUM(pre_count) previous_count_value,
10851                   SUM(pre_started) previous_started_value,
10852                   SUM(pre_ended) previous_ended_value,
10853                   SUM(campaign_count) campaign_count,
10854                   SUM(forecasted_responses) forecasted_responses,
10855                   SUM(targeted_customers) targeted_customers,
10856                   SUM(prev_targeted_customers) prev_targeted_customers,
10857                   SUM(budget_approved) budget_approved
10858              FROM (
10859 -- FOR THE PERIOD TYPE YEAR
10860                    SELECT
10861                         /*+ use_hash(a)  use_hash(b)  use_hash(c) use_hash(d) */
10862                              c.area2_code schedule_area2,
10863                              d.city_id schedule_country,
10864                              NVL(d.business_unit_id, 0) business_unit_id,
10865                              a.campaign_id campaign_id,
10866                              'YEAR' aggregate_by,
10867                              'Z' display_type,
10868                              b.fiscal_year fiscal_year,
10869                              'N' fiscal_qtr,
10870                              'N' fiscal_month,
10871                              COUNT(a.schedule_id) curr_count,
10872                              0 pre_count,
10873                              0 curr_started,
10874                              0 pre_started,
10875                              0 curr_ended,
10876                              0 pre_ended,
10877                              0 campaign_count,
10878                              0 forecasted_responses,
10879                              0 targeted_customers,
10880                              0 prev_targeted_customers,
10881                              0 budget_approved
10882                        FROM ams_campaign_schedules_b a,
10883                             bim_dates b,
10884                             jtf_loc_hierarchies_b c,
10885                             ams_campaigns_all_b d
10886                       WHERE a.status_code IN
10887                                 ('ACTIVE', 'CANCELLED', 'COMPLETED', 'CLOSED')
10888          --***
10889                          AND (trunc(a.start_date_time) < b.fiscal_year_start
10890 			     or
10891 			     trunc(a.start_date_time) BETWEEN b.fiscal_year_start
10892                                                   AND b.fiscal_year_end
10893 			      )
10894                         AND d.city_id = c.location_hierarchy_id
10895                         AND a.campaign_id = d.campaign_id
10896                         AND b.period_type = 'YEAR'
10897                         AND trunc(a.start_date_time) >= l_min_start_date
10898                         AND trunc(NVL(a.end_date_time, d.actual_exec_end_date)) >=
10899                                                            b.fiscal_year_start
10900                    GROUP BY c.area2_code,
10901                             d.city_id,
10902                             NVL(d.business_unit_id, 0),
10903                             a.campaign_id,
10904                             b.fiscal_year
10905 ------------------------------------------------------------------------------------------
10906                    UNION ALL
10907 ------------------------------------------------------------------------------------------
10908                    SELECT
10909                         /*+ use_hash(a)  use_hash(b)  use_hash(c)  */
10910                              c.area2_code schedule_area2,
10911                              a.campaign_country schedule_country,
10912                              NVL(a.business_unit_id, 0) business_unit_id,
10913                              a.campaign_id campaign_id,
10914                              'YEAR' aggregate_by,
10915                              'Z' display_type,
10916                              b.fiscal_year fiscal_year,
10917                              'N' fiscal_qtr,
10918                              'N' fiscal_month,
10919                              0 curr_count,
10920                              0 pre_count,
10921                              0 curr_started,
10922                              0 pre_started,
10923                              0 curr_ended,
10924                              0 pre_ended,
10925                              0 campaign_count,
10926                              SUM(forecasted_responses) forecasted_responses,
10927                              SUM(targeted_customers) targeted_customers,
10928                              0 prev_targeted_customers,
10929                              SUM(budget_approved) budget_approved
10930                        FROM bim_r_camp_daily_facts a,
10931                             bim_dates b,
10932                             jtf_loc_hierarchies_b c
10933                       WHERE a.campaign_country = c.location_hierarchy_id
10934         --***
10935                      AND (trunc(a.start_date) < b.fiscal_year_start
10936 		          or
10937 			  trunc(a.start_date) between b.fiscal_year_start
10938                                                   AND b.fiscal_year_end
10939 			  )
10940                         AND trunc(a.end_date) >= b.fiscal_year_start
10941                         AND b.period_type = 'YEAR'
10942                    GROUP BY c.area2_code,
10943                             a.campaign_country,
10944                             NVL(a.business_unit_id, 0),
10945                             a.campaign_id,
10946                             b.fiscal_year)
10947          GROUP BY schedule_area2,
10948                   schedule_country,
10949                   business_unit_id,
10950                   campaign_id,
10951                   aggregate_by,
10952                   display_type,
10953                   fiscal_year,
10954                   fiscal_qtr,
10955                   fiscal_month
10956            HAVING SUM(curr_count) > 0
10957                OR SUM(curr_started) > 0
10958                OR SUM(curr_ended) > 0
10959                OR SUM(pre_count) > 0
10960                OR SUM(pre_started) > 0
10961                OR SUM(pre_ended) > 0
10962                OR SUM(targeted_customers) > 0
10963                OR SUM(prev_targeted_customers) > 0
10964                OR SUM(forecasted_responses) > 0
10965                OR SUM(budget_approved) <> 0;
10966 
10967 
10968 				  ---------union all admin end
10969 
10970 
10971  --commit;
10972 
10973       ams_utility_pvt.write_conc_log('BIM_CAMP_COLLECTION: SUMMARY CAMP QTR INSERT END');
10974    EXCEPTION
10975       WHEN OTHERS
10976       THEN
10977          ams_utility_pvt.write_conc_log(   'BIM_CAMP_COLLECTION:IN OTHERS EXCEPTION '|| SQLERRM(SQLCODE));
10978    END get_summary_data;
10979 END bim_camp_collection;