Foodmart consists of an application capable of managing sales, costs and profits yearly and monthly.

Moreover, Foodmart comprises different tables that allow the user to file transaction information about products, customers, stores, warehouses, promotions and employees. Moving through these fields allows to configure different setups to display useful boards or graphics.

1 Data model

The following sections will follow the displayed data model in order to create the application and its transaction forms, graphs and lists.

To represent the sales invoices we will have a main table sales_fact from which other tables such as the sales_fact_month_customer, sales_fact_month_product, sales_fact_month_store and sales_fact_yearmonth_customer, sales_fact_yearmonth_product, sales_fact_yearmonth_store will grab the needed data to compute monthly and yearly sales, costs, profit among other values.

This tables can be found in more detail in the product, customer and store sections.

This is the structure the sales invoices tables follow:

This design allows the user to only transaction with one big table and obtain personalized results automatically without transactioning with the other related tables.

As of now only sales_fact_month and sales_fact_yearmonth are implemented. The other parts of the pyramid could be developed in the future. More detailed information about how the tables are filled can be found in the triggers and procedures sections.

2 Tables

Following the previous data model, the table definitions and their column meanings will be explained briefly below.

2.1 Time

The table time_by_day gives a time and date an identification number in order to avoid extra information on invoice entries.

<table name='time_by_day'>
    
    <!-- COLUMNS -->
    <column name='time_id'              type='serial'                                                   required='y'/>
    <column name='the_date'             type='date'         size='year to day'      default='current'   required='y'/>
    <column name='the_datetime'         type='datetime'     size='year to second'                       required='y'/>
    <column name='the_day'              type='varchar'      size='30'                                               />
    <column name='the_month'            type='varchar'      size='30'                                               />
    <column name='the_year'             type='smallint'                                                             />
    <column name='day_of_month'         type='smallint'                                                             />
    <column name='week_of_year'         type='integer'                                                              />
    <column name='month_of_year'        type='smallint'                                                             />
    <column name='quarter'              type='varchar'      size='30'                                               />
    <column name='fiscal_period'        type='varchar'      size='30'                                               />
    
    <primary name='p_time_by_day'   columns='time_id'   />
    
    <index name='i_time_by_day1'    columns='the_date'  />
    <index name='i_time_by_day2'    columns='the_year'  />
    
</table>
Copy
-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri Jun 21 16:28:33 CEST 2019 Engine: informix
-- time_by_day
-- **************************************************************************
CREATE TABLE time_by_day (
    time_id serial not null,
    the_date date default current not null,
    the_datetime datetime year to second not null,
    the_day varchar(30),
    the_month varchar(30),
    the_year smallint,
    day_of_month smallint,
    week_of_year integer,
    month_of_year smallint,
    quarter varchar(30),
    fiscal_period varchar(30)
);
ALTER TABLE time_by_day LOCK MODE (ROW);

CREATE INDEX i_time_by_day1
 ON time_by_day(the_date);

CREATE INDEX i_time_by_day2
 ON time_by_day(the_year);

-- Index to avoid automatic named by database (named with pk name).
CREATE UNIQUE INDEX p_time_by_day ON time_by_day(time_id);
ALTER TABLE time_by_day
 ADD CONSTRAINT 
 PRIMARY KEY (time_id)
 CONSTRAINT p_time_by_day;
Copy
-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri Jun 21 16:28:48 CEST 2019 Engine: postgres
-- time_by_day
-- **************************************************************************
CREATE TABLE time_by_day (
    time_id serial not null,
    the_date date default current not null,
    the_datetime timestamp not null,
    the_day varchar(30),
    the_month varchar(30),
    the_year smallint,
    day_of_month smallint,
    week_of_year integer,
    month_of_year smallint,
    quarter varchar(30),
    fiscal_period varchar(30)
) WITH OIDS;

CREATE INDEX i_time_by_day1
 ON time_by_day(the_date);

CREATE INDEX i_time_by_day2
 ON time_by_day(the_year);

-- Database Server on Postgres, DB2 will be created a index automatically with the same name of pk
ALTER TABLE time_by_day
 ADD CONSTRAINT p_time_by_day
 PRIMARY KEY (time_id);
Time: time_by_day
Column Type Details Definition
time_id serial Primary key. Time identifier.
the_date date Date.
the_datetime datetime Date and time.
the_day varchar Name of the day.
the_month varchar Name of the month.
the_year smallint Year.
day_of_month smallint Numerical day.
week_of_year integer Week number.
month_of_year smallint Numerical month.
quarter varchar Year quarter.
fiscal_period varchar Fiscal period.

2.2 Sale's Invoices

The table sales_fact gives time based invoices about sales, costs and units for each product bought at a store by a customer at a certain time and date.

<table name='sales_fact'>
    
    <!-- COLUMNS -->
    <column name='product_id'           type='integer'                      required='y'/>
    <column name='time_id'              type='integer'                      required='y'/>
    <column name='customer_id'          type='integer'                      required='y'/>
    <column name='promotion_id'         type='integer'                      required='y'/>
    <column name='store_id'             type='integer'                      required='y'/>
    <column name='store_sales'          type='decimal'      size='10,4'     required='y'/>
    <column name='store_cost'           type='decimal'      size='10,4'     required='y'/>
    <column name='unit_sales'           type='decimal'      size='10,4'     required='y'/>
    
    
    <index name='i_sales_fact1'         columns='time_id'                               />
    <index name='i_sales_fact2'         columns='customer_id'                           />
    <index name='i_sales_fact3'         columns='store_id'                              />
    
    <index name='i_sales_fact4'         columns='product_id'                            />
    
    <foreign name='f_sales_fact'        columns='time_id'       references='time_by_day'    refcols='time_id'       /> 
    <foreign name='f_sales_fact2'       columns='customer_id'   references='customer'       refcols='customer_id'   /> 
    <foreign name='f_sales_fact3'       columns='product_id'    references='product'        refcols='product_id'    />
    <foreign name='f_sales_fact4'       columns='store_id'      references='store'          refcols='store_id'      />
    

</table>
Copy
-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri Jun 21 16:11:27 CEST 2019 Engine: informix
-- sales_fact
-- **************************************************************************
CREATE TABLE sales_fact (
    product_id integer not null,
    time_id integer not null,
    customer_id integer not null,
    promotion_id integer not null,
    store_id integer not null,
    store_sales decimal(10,4) not null,
    store_cost decimal(10,4) not null,
    unit_sales decimal(10,4) not null
);
ALTER TABLE sales_fact LOCK MODE (ROW);

CREATE INDEX i_sales_fact1
 ON sales_fact(time_id);

CREATE INDEX i_sales_fact2
 ON sales_fact(customer_id);

CREATE INDEX i_sales_fact3
 ON sales_fact(store_id);

CREATE INDEX i_sales_fact4
 ON sales_fact(product_id);

-- Omitido indice f_sales_fact dado que ya existe un indice sobre las columnas 
ALTER TABLE sales_fact
 ADD CONSTRAINT  FOREIGN KEY (time_id)
 REFERENCES time_by_day(time_id)
 CONSTRAINT f_sales_fact;

-- Omitido indice f_sales_fact2 dado que ya existe un indice sobre las columnas 
ALTER TABLE sales_fact
 ADD CONSTRAINT  FOREIGN KEY (customer_id)
 REFERENCES customer(customer_id)
 CONSTRAINT f_sales_fact2;

-- Omitido indice f_sales_fact3 dado que ya existe un indice sobre las columnas 
ALTER TABLE sales_fact
 ADD CONSTRAINT  FOREIGN KEY (product_id)
 REFERENCES product(product_id)
 CONSTRAINT f_sales_fact3;

-- Omitido indice f_sales_fact4 dado que ya existe un indice sobre las columnas 
ALTER TABLE sales_fact
 ADD CONSTRAINT  FOREIGN KEY (store_id)
 REFERENCES store(store_id)
 CONSTRAINT f_sales_fact4;
Copy
-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri Jun 21 16:11:33 CEST 2019 Engine: postgres
-- sales_fact
-- **************************************************************************
CREATE TABLE sales_fact (
    product_id integer not null,
    time_id integer not null,
    customer_id integer not null,
    promotion_id integer not null,
    store_id integer not null,
    store_sales decimal(10,4) not null,
    store_cost decimal(10,4) not null,
    unit_sales decimal(10,4) not null
) WITH OIDS;

CREATE INDEX i_sales_fact1
 ON sales_fact(time_id);

CREATE INDEX i_sales_fact2
 ON sales_fact(customer_id);

CREATE INDEX i_sales_fact3
 ON sales_fact(store_id);

CREATE INDEX i_sales_fact4
 ON sales_fact(product_id);

-- Omitido indice f_sales_fact dado que ya existe un indice sobre las columnas 
ALTER TABLE sales_fact
 ADD CONSTRAINT f_sales_fact FOREIGN KEY (time_id)
 REFERENCES time_by_day(time_id) DEFERRABLE INITIALLY IMMEDIATE ;

-- Omitido indice f_sales_fact2 dado que ya existe un indice sobre las columnas 
ALTER TABLE sales_fact
 ADD CONSTRAINT f_sales_fact2 FOREIGN KEY (customer_id)
 REFERENCES customer(customer_id) DEFERRABLE INITIALLY IMMEDIATE ;

-- Omitido indice f_sales_fact3 dado que ya existe un indice sobre las columnas 
ALTER TABLE sales_fact
 ADD CONSTRAINT f_sales_fact3 FOREIGN KEY (product_id)
 REFERENCES product(product_id) DEFERRABLE INITIALLY IMMEDIATE ;

-- Omitido indice f_sales_fact4 dado que ya existe un indice sobre las columnas 
ALTER TABLE sales_fact
 ADD CONSTRAINT f_sales_fact4 FOREIGN KEY (store_id)
 REFERENCES store(store_id) DEFERRABLE INITIALLY IMMEDIATE ;
Sale's Invoices: sales_fact
Column Type Details Definition
product_id integer Primary key.
References to product.product_id.
Product identifier.
time_id integer Primary key.
References to time_by_day.time_id.
Time identifier.
customer_id integer Primary key.
References to customer.customer_id.
Customer identifier.
promotion_id integer References to promotion.promotion_id. Promotion identifier.
store_id serial Primary key.
References to store.store_id.
Store identifier.
store_sales decimal Sales.
store_cost decimal Cost.
unit_sales decimal Units sold.

This table provides data through procedure based triggers to sales_fact_month_customer, sales_fact_yearmonth_customer, sales_fact_month_store, sales_fact_yearmonth_store, sales_fact_month_product, sales_fact_yearmonth_product. This tables will be discussed in the following sections dedicated to each of the parts.

2.3 Product

In this section all the tables associated with product will be placed here.

<table name='promotion'>
    
    <!-- COLUMNS -->
    <column name='promotion_id'             type='integer'                                  required='y'/>
    <column name='promotion_district_id'    type='integer'                                              />
    <column name='promotion_name'           type='varchar'          size='30'                           />
    <column name='media_type'               type='varchar'          size='30'                           />
    <column name='cost'                     type='decimal'          size='10,4'                         />
    <column name='start_date'               type='datetime'         size='year to second'               />
    <column name='end_date'                 type='datetime'         size='year to second'               />
    
    <primary name='p_promotion'         columns='promotion_id'                                          />
    
</table>
<table name='product_class'>
    
    <!-- COLUMNS -->
    <column name='product_class_id'         type='integer'                  required='y'/>
    <column name='product_category'         type='varchar'      size='30'               />
    <column name='product_subcategory'      type='varchar'      size='30'               />
    <column name='product_department'       type='varchar'      size='30'               />
    <column name='product_family'           type='varchar'      size='30'               />
    
    <!-- PRIMARY KEY -->
    <primary name='p_product_class'     columns='product_class_id'                      />
    
</table>
<table name='product'>
    
    <!-- COLUMNS -->
    <column name='product_id'           type='integer'                          required='y'/>
    <column name='product_name'         type='varchar'      size='60'           required='y'/>
    <column name='product_class_id'     type='integer'                          required='y'/>
    <column name='brand_name'           type='varchar'      size='60'                       />
    <column name='sku'                  type='decimal'      size='17,0'         required='y'/>
    <column name='srp'                  type='decimal'      size='10,4'                     />
    <column name='gross_weight'         type='decimal'      size='17,0'                     />
    <column name='net_weight'           type='decimal'      size='17,0'                     />
    <column name='recyclable_package'   type='boolean'                                      />
    <column name='low_fat'              type='boolean'                                      />
    <column name='units_per_case'       type='smallint'                                     />
    <column name='cases_per_pallet'     type='smallint'                                     />
    <column name='shelf_width'          type='decimal'      size='17,0'                     />
    <column name='shelf_height'         type='decimal'      size='17,0'                     />
    <column name='shelf_depth'          type='decimal'      size='17,0'                     />
    
    <!-- PRIMARY -->
    <primary name='p_product'       columns='product_id'                                    />
    
    <!-- FOREIGN -->
    <foreign name='f_product'       columns='product_class_id'  references='product_class'  refcols='product_class_id'/>
   
</table>
<!-- Automatic Build -->
<table name='sales_fact_month_product'>
    
    <!-- COLUMNS -->
    <column name='product_id'                   type='integer'                                              required='y'/>
    <column name='the_year'                     type='integer'                                              required='y'/>
    <!-- UNITS -->      
    <column name='unit_sales'                   type='integer'                              default='0'                 />
    <column name='unit_sales_prev'              type='integer'                              default='0'                 />
    <!-- SALES -->      
    <column name='sales'                        type='decimal'          size='10,4'         default='0.0'               />
    <column name='sales_prev'                   type='decimal'          size='10,4'         default='0.0'               />
    <column name='sales_pct'                    type='decimal'          size='10,4'         default='0.0'               />
    <column name='sales_avg'                    type='decimal'          size='10,4'         default='0.0'               />
        
    <column name='sales_jan'                    type='decimal'          size='10,4'         default='0.0'               />
    <column name='sales_feb'                    type='decimal'          size='10,4'         default='0.0'               />
    <column name='sales_mar'                    type='decimal'          size='10,4'         default='0.0'               />
    <column name='sales_apr'                    type='decimal'          size='10,4'         default='0.0'               />
    <column name='sales_may'                    type='decimal'          size='10,4'         default='0.0'               />
    <column name='sales_jun'                    type='decimal'          size='10,4'         default='0.0'               />
    <column name='sales_jul'                    type='decimal'          size='10,4'         default='0.0'               />
    <column name='sales_ago'                    type='decimal'          size='10,4'         default='0.0'               />
    <column name='sales_sep'                    type='decimal'          size='10,4'         default='0.0'               />
    <column name='sales_oct'                    type='decimal'          size='10,4'         default='0.0'               />
    <column name='sales_nov'                    type='decimal'          size='10,4'         default='0.0'               />
    <column name='sales_dec'                    type='decimal'          size='10,4'         default='0.0'               />
        
    <!-- COST -->       
    <column name='cost'                         type='decimal'          size='10,4'         default='0.0'               />
        
    <column name='cost_jan'                     type='decimal'          size='10,4'         default='0.0'               />
    <column name='cost_feb'                     type='decimal'          size='10,4'         default='0.0'               />
    <column name='cost_mar'                     type='decimal'          size='10,4'         default='0.0'               />
    <column name='cost_apr'                     type='decimal'          size='10,4'         default='0.0'               />
    <column name='cost_may'                     type='decimal'          size='10,4'         default='0.0'               />
    <column name='cost_jun'                     type='decimal'          size='10,4'         default='0.0'               />
    <column name='cost_jul'                     type='decimal'          size='10,4'         default='0.0'               />
    <column name='cost_ago'                     type='decimal'          size='10,4'         default='0.0'               />
    <column name='cost_sep'                     type='decimal'          size='10,4'         default='0.0'               />
    <column name='cost_oct'                     type='decimal'          size='10,4'         default='0.0'               />
    <column name='cost_nov'                     type='decimal'          size='10,4'         default='0.0'               />
    <column name='cost_dec'                     type='decimal'          size='10,4'         default='0.0'               />
        
    <column name='cost_prev'                    type='decimal'          size='10,4'         default='0.0'               />
    <column name='cost_pct'                     type='decimal'          size='10,4'         default='0.0'               />
    <column name='cost_avg'                     type='decimal'          size='10,4'         default='0.0'               />
    <!-- PROFIT -->     
    <column name='profit'                       type='decimal'          size='10,4'         default='0.0'               />
        
    <!-- PROMOTION -->      
    <column name='promotion_sales'              type='decimal'          size='10,4'         default='0.0'               />
    <column name='promotion_sales_prev'         type='decimal'          size='10,4'         default='0.0'               />
                
    <!-- TICKETS -->
    <column name='sales_count'                  type='integer'                              default='0'                 />
    <column name='sales_count_prev'             type='integer'                              default='0'                 />

    
    <!-- PRIMARY -->
    <primary name='p_sales_fact_month_product'      columns='product_id, the_year'                                              />

    <foreign name='f_sales_fact_month_product'      columns='product_id'            references='product'    refcols='product_id'/>
    
</table>
<!-- Automatic Build -->
<table name='sales_fact_yearmonth_product'> 
    <!-- COLUMNS -->
    <column name='product_id'           type='integer'                                      required='y'/>
    <column name='the_year'             type='integer'                                      required='y'/>
    <column name='the_month'            type='varchar'      size='30'                       required='y'/>
    <column name='month_of_year'        type='integer'                                      required='y'/>
    <!-- UNITS -->
    <column name='unit_sales'           type='integer'                      default='0'                 />
    <column name='unit_sales_prev'      type='integer'                      default='0'                 />
    <!-- SALES -->                      
    <column name='sales'                type='decimal'      size='10,4'     default='0.0'               />
    <column name='sales_prev'           type='decimal'      size='10,4'     default='0.0'               />
    <column name='sales_pct'            type='decimal'      size='10,4'     default='0.0'               />
    <column name='sales_avg'            type='decimal'      size='10,4'     default='0.0'               />

    <!-- COST -->
    <column name='cost'                 type='decimal'      size='10,4'     default='0.0'               />
    <column name='cost_prev'            type='decimal'      size='10,4'     default='0.0'               />
    <column name='cost_pct'             type='decimal'      size='10,4'     default='0.0'               />
    <column name='cost_avg'             type='decimal'      size='10,4'     default='0.0'               />
    <!-- PROFIT -->
    <column name='profit'               type='decimal'      size='10,4'     default='0.0'               />

    <!-- PROMOTION -->
    <column name='promotion_sales'      type='decimal'      size='10,4'     default='0.0'               />
    <column name='promotion_sales_prev' type='decimal'      size='10,4'     default='0.0'               />

    <!-- TICKETS -->
    <column name='sales_count'          type='integer'                      default='0'                 />
    <column name='sales_count_prev'     type='integer'                      default='0'                 />
    
    
    <!-- PRIMARY -->
    <primary name='p_sales_fact_yearmonth_product'      columns='product_id, the_year, month_of_year'                                               />

    <foreign name='f_sales_fact_yearmonth_product'      columns='product_id'                            references='product'    refcols='product_id'/> 
    
</table>
Copy
-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Mon Jul 01 10:23:55 CEST 2019 Engine: informix
-- promotion
-- **************************************************************************
CREATE TABLE promotion (
    promotion_id integer not null,
    promotion_district_id integer,
    promotion_name varchar(30),
    media_type varchar(30),
    cost decimal(10,4),
    start_date datetime year to second,
    end_date datetime year to second
);
ALTER TABLE promotion LOCK MODE (ROW);

-- Index to avoid automatic named by database (named with pk name).
CREATE UNIQUE INDEX p_promotion ON promotion(promotion_id);
ALTER TABLE promotion
 ADD CONSTRAINT 
 PRIMARY KEY (promotion_id)
 CONSTRAINT p_promotion;-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri Jun 21 11:53:08 CEST 2019 Engine: informix
-- product_class
-- **************************************************************************
CREATE TABLE product_class (
    product_class_id integer not null,
    product_category varchar(30),
    product_subcategory varchar(30),
    product_department varchar(30),
    product_family varchar(30)
);
ALTER TABLE product_class LOCK MODE (ROW);

-- Index to avoid automatic named by database (named with pk name).
CREATE UNIQUE INDEX p_product_class ON product_class(product_class_id);
ALTER TABLE product_class
 ADD CONSTRAINT 
 PRIMARY KEY (product_class_id)
 CONSTRAINT p_product_class;

-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri Jun 21 11:54:48 CEST 2019 Engine: informix
-- product
-- **************************************************************************
CREATE TABLE product (
    product_id integer not null,
    product_name varchar(60) not null,
    product_class_id integer not null,
    brand_name varchar(60),
    sku decimal(17,0) not null,
    srp decimal(10,4),
    gross_weight decimal(17,0),
    net_weight decimal(17,0),
    recyclable_package boolean,
    low_fat boolean,
    units_per_case smallint,
    cases_per_pallet smallint,
    shelf_width decimal(17,0),
    shelf_height decimal(17,0),
    shelf_depth decimal(17,0)
);
ALTER TABLE product LOCK MODE (ROW);

-- Index to avoid automatic named by database (named with pk name).
CREATE UNIQUE INDEX p_product ON product(product_id);
ALTER TABLE product
 ADD CONSTRAINT 
 PRIMARY KEY (product_id)
 CONSTRAINT p_product;

CREATE  INDEX f_product ON product(product_class_id);
ALTER TABLE product
 ADD CONSTRAINT  FOREIGN KEY (product_class_id)
 REFERENCES product_class(product_class_id)
 CONSTRAINT f_product;
 
-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri Jun 21 11:55:57 CEST 2019 Engine: informix
-- sales_fact_month_product
-- **************************************************************************
CREATE TABLE sales_fact_month_product (
    product_id integer not null,
    the_year integer not null,
    unit_sales integer default 0,
    unit_sales_prev integer default 0,
    sales decimal(10,4) default 0.0,
    sales_prev decimal(10,4) default 0.0,
    sales_pct decimal(10,4) default 0.0,
    sales_avg decimal(10,4) default 0.0,
    sales_jan decimal(10,4) default 0.0,
    sales_feb decimal(10,4) default 0.0,
    sales_mar decimal(10,4) default 0.0,
    sales_apr decimal(10,4) default 0.0,
    sales_may decimal(10,4) default 0.0,
    sales_jun decimal(10,4) default 0.0,
    sales_jul decimal(10,4) default 0.0,
    sales_ago decimal(10,4) default 0.0,
    sales_sep decimal(10,4) default 0.0,
    sales_oct decimal(10,4) default 0.0,
    sales_nov decimal(10,4) default 0.0,
    sales_dec decimal(10,4) default 0.0,
    cost decimal(10,4) default 0.0,
    cost_jan decimal(10,4) default 0.0,
    cost_feb decimal(10,4) default 0.0,
    cost_mar decimal(10,4) default 0.0,
    cost_apr decimal(10,4) default 0.0,
    cost_may decimal(10,4) default 0.0,
    cost_jun decimal(10,4) default 0.0,
    cost_jul decimal(10,4) default 0.0,
    cost_ago decimal(10,4) default 0.0,
    cost_sep decimal(10,4) default 0.0,
    cost_oct decimal(10,4) default 0.0,
    cost_nov decimal(10,4) default 0.0,
    cost_dec decimal(10,4) default 0.0,
    cost_prev decimal(10,4) default 0.0,
    cost_pct decimal(10,4) default 0.0,
    cost_avg decimal(10,4) default 0.0,
    profit decimal(10,4) default 0.0,
    promotion_sales decimal(10,4) default 0.0,
    promotion_sales_prev decimal(10,4) default 0.0,
    sales_count integer default 0,
    sales_count_prev integer default 0
);
ALTER TABLE sales_fact_month_product LOCK MODE (ROW);

-- Index to avoid automatic named by database (named with pk name).
CREATE UNIQUE INDEX p_sales_fact_month_product ON sales_fact_month_product(product_id, the_year);
ALTER TABLE sales_fact_month_product
 ADD CONSTRAINT 
 PRIMARY KEY (product_id, the_year)
 CONSTRAINT p_sales_fact_month_product;

CREATE  INDEX f_sales_fact_month_product ON sales_fact_month_product(product_id);
ALTER TABLE sales_fact_month_product
 ADD CONSTRAINT  FOREIGN KEY (product_id)
 REFERENCES product(product_id)
 CONSTRAINT f_sales_fact_month_product;

-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri Jun 21 11:56:33 CEST 2019 Engine: informix
-- sales_fact_yearmonth_product
-- **************************************************************************
CREATE TABLE sales_fact_yearmonth_product (
    product_id integer not null,
    the_year integer not null,
    the_month varchar(30) not null,
    month_of_year integer not null,
    unit_sales integer default 0,
    unit_sales_prev integer default 0,
    sales decimal(10,4) default 0.0,
    sales_prev decimal(10,4) default 0.0,
    sales_pct decimal(10,4) default 0.0,
    sales_avg decimal(10,4) default 0.0,
    cost decimal(10,4) default 0.0,
    cost_prev decimal(10,4) default 0.0,
    cost_pct decimal(10,4) default 0.0,
    cost_avg decimal(10,4) default 0.0,
    profit decimal(10,4) default 0.0,
    promotion_sales decimal(10,4) default 0.0,
    promotion_sales_prev decimal(10,4) default 0.0,
    sales_count integer default 0,
    sales_count_prev integer default 0
);
ALTER TABLE sales_fact_yearmonth_product LOCK MODE (ROW);

-- Index to avoid automatic named by database (named with pk name).
CREATE UNIQUE INDEX p_sales_fact_yearmonth_product ON sales_fact_yearmonth_product(product_id, the_year, month_of_year);
ALTER TABLE sales_fact_yearmonth_product
 ADD CONSTRAINT 
 PRIMARY KEY (product_id, the_year, month_of_year)
 CONSTRAINT p_sales_fact_yearmonth_product;

CREATE  INDEX f_sales_fact_yearmonth_product ON sales_fact_yearmonth_product(product_id);
ALTER TABLE sales_fact_yearmonth_product
 ADD CONSTRAINT  FOREIGN KEY (product_id)
 REFERENCES product(product_id)
 CONSTRAINT f_sales_fact_yearmonth_product;
Copy
-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Mon Jul 01 10:24:10 CEST 2019 Engine: postgres
-- promotion
-- **************************************************************************
CREATE TABLE promotion (
    promotion_id integer not null,
    promotion_district_id integer,
    promotion_name varchar(30),
    media_type varchar(30),
    cost decimal(10,4),
    start_date timestamp,
    end_date timestamp
) WITH OIDS;

-- Database Server on Postgres, DB2 will be created a index automatically with the same name of pk
ALTER TABLE promotion
 ADD CONSTRAINT p_promotion
 PRIMARY KEY (promotion_id);

-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri Jun 21 11:53:52 CEST 2019 Engine: postgres
-- product_class
-- **************************************************************************
CREATE TABLE product_class (
    product_class_id integer not null,
    product_category varchar(30),
    product_subcategory varchar(30),
    product_department varchar(30),
    product_family varchar(30)
) WITH OIDS;

-- Database Server on Postgres, DB2 will be created a index automatically with the same name of pk
ALTER TABLE product_class
 ADD CONSTRAINT p_product_class
 PRIMARY KEY (product_class_id);

-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri Jun 21 11:55:03 CEST 2019 Engine: postgres
-- product
-- **************************************************************************
CREATE TABLE product (
    product_id integer not null,
    product_name varchar(60) not null,
    product_class_id integer not null,
    brand_name varchar(60),
    sku decimal(17,0) not null,
    srp decimal(10,4),
    gross_weight decimal(17,0),
    net_weight decimal(17,0),
    recyclable_package boolean,
    low_fat boolean,
    units_per_case smallint,
    cases_per_pallet smallint,
    shelf_width decimal(17,0),
    shelf_height decimal(17,0),
    shelf_depth decimal(17,0)
) WITH OIDS;

-- Database Server on Postgres, DB2 will be created a index automatically with the same name of pk
ALTER TABLE product
 ADD CONSTRAINT p_product
 PRIMARY KEY (product_id);

CREATE  INDEX f_product ON product(product_class_id);
ALTER TABLE product
 ADD CONSTRAINT f_product FOREIGN KEY (product_class_id)
 REFERENCES product_class(product_class_id) DEFERRABLE INITIALLY IMMEDIATE ;
 
-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri Jun 21 11:56:16 CEST 2019 Engine: postgres
-- sales_fact_month_product
-- **************************************************************************
CREATE TABLE sales_fact_month_product (
    product_id integer not null,
    the_year integer not null,
    unit_sales integer default 0,
    unit_sales_prev integer default 0,
    sales decimal(10,4) default 0.0,
    sales_prev decimal(10,4) default 0.0,
    sales_pct decimal(10,4) default 0.0,
    sales_avg decimal(10,4) default 0.0,
    sales_jan decimal(10,4) default 0.0,
    sales_feb decimal(10,4) default 0.0,
    sales_mar decimal(10,4) default 0.0,
    sales_apr decimal(10,4) default 0.0,
    sales_may decimal(10,4) default 0.0,
    sales_jun decimal(10,4) default 0.0,
    sales_jul decimal(10,4) default 0.0,
    sales_ago decimal(10,4) default 0.0,
    sales_sep decimal(10,4) default 0.0,
    sales_oct decimal(10,4) default 0.0,
    sales_nov decimal(10,4) default 0.0,
    sales_dec decimal(10,4) default 0.0,
    cost decimal(10,4) default 0.0,
    cost_jan decimal(10,4) default 0.0,
    cost_feb decimal(10,4) default 0.0,
    cost_mar decimal(10,4) default 0.0,
    cost_apr decimal(10,4) default 0.0,
    cost_may decimal(10,4) default 0.0,
    cost_jun decimal(10,4) default 0.0,
    cost_jul decimal(10,4) default 0.0,
    cost_ago decimal(10,4) default 0.0,
    cost_sep decimal(10,4) default 0.0,
    cost_oct decimal(10,4) default 0.0,
    cost_nov decimal(10,4) default 0.0,
    cost_dec decimal(10,4) default 0.0,
    cost_prev decimal(10,4) default 0.0,
    cost_pct decimal(10,4) default 0.0,
    cost_avg decimal(10,4) default 0.0,
    profit decimal(10,4) default 0.0,
    promotion_sales decimal(10,4) default 0.0,
    promotion_sales_prev decimal(10,4) default 0.0,
    sales_count integer default 0,
    sales_count_prev integer default 0
) WITH OIDS;

-- Database Server on Postgres, DB2 will be created a index automatically with the same name of pk
ALTER TABLE sales_fact_month_product
 ADD CONSTRAINT p_sales_fact_month_product
 PRIMARY KEY (product_id, the_year);

CREATE  INDEX f_sales_fact_month_product ON sales_fact_month_product(product_id);
ALTER TABLE sales_fact_month_product
 ADD CONSTRAINT f_sales_fact_month_product FOREIGN KEY (product_id)
 REFERENCES product(product_id) DEFERRABLE INITIALLY IMMEDIATE ;

-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri Jun 21 11:56:45 CEST 2019 Engine: postgres
-- sales_fact_yearmonth_product
-- **************************************************************************
CREATE TABLE sales_fact_yearmonth_product (
    product_id integer not null,
    the_year integer not null,
    the_month varchar(30) not null,
    month_of_year integer not null,
    unit_sales integer default 0,
    unit_sales_prev integer default 0,
    sales decimal(10,4) default 0.0,
    sales_prev decimal(10,4) default 0.0,
    sales_pct decimal(10,4) default 0.0,
    sales_avg decimal(10,4) default 0.0,
    cost decimal(10,4) default 0.0,
    cost_prev decimal(10,4) default 0.0,
    cost_pct decimal(10,4) default 0.0,
    cost_avg decimal(10,4) default 0.0,
    profit decimal(10,4) default 0.0,
    promotion_sales decimal(10,4) default 0.0,
    promotion_sales_prev decimal(10,4) default 0.0,
    sales_count integer default 0,
    sales_count_prev integer default 0
) WITH OIDS;

-- Database Server on Postgres, DB2 will be created a index automatically with the same name of pk
ALTER TABLE sales_fact_yearmonth_product
 ADD CONSTRAINT p_sales_fact_yearmonth_product
 PRIMARY KEY (product_id, the_year, month_of_year);

CREATE  INDEX f_sales_fact_yearmonth_product ON sales_fact_yearmonth_product(product_id);
ALTER TABLE sales_fact_yearmonth_product
 ADD CONSTRAINT f_sales_fact_yearmonth_product FOREIGN KEY (product_id)
 REFERENCES product(product_id) DEFERRABLE INITIALLY IMMEDIATE ;
Promotion: promotion
Column Type Details Definition
promotion_id integer Primary key. Promotion identifier.
promotion_district_id integer References to district.district_id. District identifier.
promotion_name varchar Promotion name.
media_type varchar Promotion media type.
cost decimal Promotion cost.
start_date datetime Promotion start date.
end_date datetime Promotion end date.

promotion provides information about the possible promotions to be applied to products.

Product Class: product_class
Column Type Details Definition
product_class_id integer Primary key. Product class identifier.
product_category varchar Product category.
product_subcategory varchar Product subcategory.
product_department varchar Product department.
product_family varchar Product family.

product_class stores all the different categories and subcategories a product can be part of.

Product: product
Column Type Details Definition
product_id integer Primary key. Product identifier.
product_name varchar Product name.
product_class_id integer References product_class.product_class_id. Product class identifier.
brand_name varchar Product's brand name.
sku decimal Product's amount of stock keeping units (SKU).
srp decimal Product's amount of shelf ready packaging (SRP).
gross_weight decimal Product's gross weight.
net_weight decimal Product's net weight.
recyclable_packaging boolean Whether product's packaging is recyclable or not.
low_fat boolean Whether product's low fat or not.
units_per_case smallint Product's units per case.
cases_per_pallet smallint Product's cases per pallet.
shelf_width decimal Product's shelf width.
shelf_height decimal Product's shelf height.
shelf_depth decimal Product's shelf depth.

product provides information about products and relates to product_class by its id.

Product Sales by Year: sales_fact_month_product
Column Type Details Definition
product_id integer Primary key.
References product.product_id.
Product identifier.
the_year integer Primary key. Year.
unit_sales integer Units sold of the product.
unit_sales_prev integer Units sold of the product the previous year.
sales decimal Product sales.
sales_prev decimal Product sales the previous year.
sales_pct decimal Product sales percentage.
sales_avg decimal Product sales month average.
sales_[mon] decimal Product sales per month of year.
Columns: sales_jan, sales_feb...
cost decimal Product cost.
cost_prev decimal Product cost the previous year.
cost_pct decimal Product cost percentage.
cost_avg decimal Product cost month average.
cost_[mon] decimal Product cost per month of year.
Columns: cost_jan, cost_feb...
profit decimal Product profit = sales - cost.
promotion_sales decimal Product sales when promotion is applied.
promotion_sales_prev decimal Product sales when promotion is applied of previous year.
sales_count integer Sales counter.

sales_fact_month_product stores yearly information about products and also monthly sales and costs.

Product Sales by Month: sales_fact_yearmonth_product
Column Type Details Definition
product_id integer Primary key.
References product.product_id.
Product identifier.
the_year integer Primary key. Year.
the_month varchar Month's name.
month_of_year integer Primary key. Month's number.
unit_sales integer Units sold of the product.
unit_sales_prev integer Units sold of the product the previous month.
sales decimal Product sales.
sales_prev decimal Product sales the previous month.
sales_pct decimal Product sales percentage.
sales_avg decimal Product sales day average.
cost decimal Product cost.
cost_prev decimal Product cost the previous month.
cost_pct decimal Product cost percentage.
cost_avg decimal Product cost day average.
profit decimal Product profit = sales - cost.
promotion_sales decimal Product sales when promotion is applied.
promotion_sales_prev decimal Product sales when promotion is applied of previous month.
sales_count integer Sales counter.

sales_fact_yearmonth_product provides monthly information about products.

2.4 Customer

In this section all the tables associated with customer will be placed here.

<table name='customer'>
    
    <!-- COLUMNS -->
    <column name='customer_id'          type='integer'                          required='y'/>
    <column name='fullname'             type='varchar'      size='30'           required='y'/>    
    <column name='lname'                type='varchar'      size='30'           required='y'/>
    <column name='fname'                type='varchar'      size='30'           required='y'/>
    <column name='account_num'          type='int8'                             required='y'/>
    <column name='mi'                   type='varchar'      size='30'                       />
    <column name='address1'             type='varchar'      size='30'                       />
    <column name='address2'             type='varchar'      size='30'                       />
    <column name='address3'             type='varchar'      size='30'                       />
    <column name='address4'             type='varchar'      size='30'                       />
    <column name='city'                 type='varchar'      size='30'                       />
    <column name='state_province'       type='varchar'      size='30'                       />
    <column name='postal_code'          type='varchar'      size='30'           required='y'/>
    <column name='country'              type='varchar'      size='30'           required='y'/>
    <column name='customer_region_id'   type='integer'                          required='y'/>
    <column name='phone1'               type='varchar'      size='30'           required='y'/>
    <column name='phone2'               type='varchar'      size='30'           required='y'/>
    <column name='birthdate'            type='date'         size='year to day'  required='y'/>
    <column name='marital_status'       type='varchar'      size='30'           required='y'/>
    <column name='income_id'            type='integer'                                      />  
    <column name='gender'               type='varchar'      size='30'           required='y'/>
    <column name='total_children'       type='smallint'                         required='y'/>
    <column name='num_children_at_home' type='smallint'                         required='y'/>
    <column name='education'            type='varchar'      size='30'           required='y'/>
    <column name='date_accnt_opened'    type='date'         size='year to day'  required='y'/>
    <column name='member_card'          type='varchar'      size='30'                       />
    <column name='occupation'           type='varchar'      size='30'                       />
    <column name='houseowner'           type='varchar'      size='30'                       />
    <column name='num_cars_owned'       type='integer'							            />
    
    <!-- INDEXES -->    
    <primary name='p_customer'      columns='customer_id'                                   />
    
    <!-- FOREIGN KEY --> 
    <foreign name='f_customer_1'    columns='customer_region_id'    references='region'                 refcols='region_id' />
    <foreign name='f_customer_2'    columns='income_id'             references='customer_yearly_income' refcols='income_id' />
    
</table>
<!-- Automatic Build -->
<table name='sales_fact_month_customer'>
    
    <!-- COLUMNS -->
    <column name='customer_id'              type='integer'                                          required='y'/>
    <column name='the_year'                 type='integer'                                          required='y'/>
    <!-- UNITS -->
    <column name='unit_sales'               type='integer'                          default='0'                 />
    <column name='unit_sales_prev'          type='integer'                          default='0'                 />
    <!-- SALES -->
    <column name='sales'                    type='decimal'      size='10,4'         default='0.0'               />
    <column name='sales_prev'               type='decimal'      size='10,4'         default='0.0'               />
    <column name='sales_pct'                type='decimal'      size='10,4'         default='0.0'               />
    <column name='sales_avg'                type='decimal'      size='10,4'         default='0.0'               />

    <column name='sales_jan'                type='decimal'      size='10,4'         default='0.0'               />
    <column name='sales_feb'                type='decimal'      size='10,4'         default='0.0'               />
    <column name='sales_mar'                type='decimal'      size='10,4'         default='0.0'               />
    <column name='sales_apr'                type='decimal'      size='10,4'         default='0.0'               />
    <column name='sales_may'                type='decimal'      size='10,4'         default='0.0'               />
    <column name='sales_jun'                type='decimal'      size='10,4'         default='0.0'               />
    <column name='sales_jul'                type='decimal'      size='10,4'         default='0.0'               />
    <column name='sales_ago'                type='decimal'      size='10,4'         default='0.0'               />
    <column name='sales_sep'                type='decimal'      size='10,4'         default='0.0'               />
    <column name='sales_oct'                type='decimal'      size='10,4'         default='0.0'               />
    <column name='sales_nov'                type='decimal'      size='10,4'         default='0.0'               />
    <column name='sales_dec'                type='decimal'      size='10,4'         default='0.0'               />

    <!-- COST -->
    <column name='cost'                     type='decimal'      size='10,4'         default='0.0'               />

    <column name='cost_jan'                 type='decimal'      size='10,4'         default='0.0'               />
    <column name='cost_feb'                 type='decimal'      size='10,4'         default='0.0'               />
    <column name='cost_mar'                 type='decimal'      size='10,4'         default='0.0'               />
    <column name='cost_apr'                 type='decimal'      size='10,4'         default='0.0'               />
    <column name='cost_may'                 type='decimal'      size='10,4'         default='0.0'               />
    <column name='cost_jun'                 type='decimal'      size='10,4'         default='0.0'               />
    <column name='cost_jul'                 type='decimal'      size='10,4'         default='0.0'               />
    <column name='cost_ago'                 type='decimal'      size='10,4'         default='0.0'               />
    <column name='cost_sep'                 type='decimal'      size='10,4'         default='0.0'               />
    <column name='cost_oct'                 type='decimal'      size='10,4'         default='0.0'               />
    <column name='cost_nov'                 type='decimal'      size='10,4'         default='0.0'               />
    <column name='cost_dec'                 type='decimal'      size='10,4'         default='0.0'               />

    <column name='cost_prev'                type='decimal'      size='10,4'         default='0.0'               />
    <column name='cost_pct'                 type='decimal'      size='10,4'         default='0.0'               />
    <column name='cost_avg'                 type='decimal'      size='10,4'         default='0.0'               />
    <!-- PROFIT -->
    <column name='profit'                   type='decimal'      size='10,4'         default='0.0'               />

    <!-- PROMOTION -->
    <column name='promotion_sales'          type='decimal'      size='10,4'         default='0.0'               />
    <column name='promotion_sales_prev'     type='decimal'      size='10,4'         default='0.0'               />

    <!-- TICKETS -->            
    <column name='sales_count'              type='integer'                          default='0'                 />
    <column name='sales_count_prev'         type='integer'                          default='0'                 />
    
    
    <!-- PRIMARY -->
    <primary name='p_sales_fact_month_customer'     columns='customer_id, the_year'                                                     />

    <foreign name='f_sales_fact_month_customer'     columns='customer_id'           references='customer'   refcols='customer_id'       />
    
</table>
<!-- Automatic Build -->
<table name='sales_fact_yearmonth_customer'>
    
    <!-- COLUMNS -->
    <column name='customer_id'              type='integer'                                      required='y'/>
    <column name='the_year'                 type='integer'                                      required='y'/>
    <column name='the_month'                type='varchar'      size='30'                       required='y'/>
    <column name='month_of_year'            type='integer'                                      required='y'/>
    <!-- UNITS -->
    <column name='unit_sales'               type='integer'                      default='0'                 />
    <column name='unit_sales_prev'          type='integer'                      default='0'                 />
    <!-- SALES -->
    <column name='sales'                    type='decimal'      size='10,4'     default='0.0'               />
    <column name='sales_prev'               type='decimal'      size='10,4'     default='0.0'               />
    <column name='sales_pct'                type='decimal'      size='10,4'     default='0.0'               />
    <column name='sales_avg'                type='decimal'      size='10,4'     default='0.0'               />
                
    <!-- COST -->               
    <column name='cost'                     type='decimal'      size='10,4'     default='0.0'               />
    <column name='cost_prev'                type='decimal'      size='10,4'     default='0.0'               />
    <column name='cost_pct'                 type='decimal'      size='10,4'     default='0.0'               />
    <column name='cost_avg'                 type='decimal'      size='10,4'     default='0.0'               />
    <!-- PROFIT -->             
    <column name='profit'                   type='decimal'      size='10,4'     default='0.0'               />
                        
    <!-- PROMOTION -->              
    <column name='promotion_sales'          type='decimal'      size='10,4'     default='0.0'               />
    <column name='promotion_sales_prev'     type='decimal'      size='10,4'     default='0.0'               />

    <!-- TICKETS -->
    <column name='sales_count'              type='integer'                      default='0'                 />
    <column name='sales_count_prev'         type='integer'                      default='0'                 />
    
    
    <!-- PRIMARY -->
    <primary name='p_sales_fact_yearmonth_customer'     columns='customer_id, the_year, month_of_year'                                                  />

    <foreign name='f_sales_fact_yearmonth_customer'     columns='customer_id'                           references='customer'   refcols='customer_id'   />
    
</table>
Copy
-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri Jun 21 13:08:48 CEST 2019 Engine: informix
-- customer
-- **************************************************************************
CREATE TABLE customer (
    customer_id integer not null,
    fullname varchar(30) not null,
    lname varchar(30) not null,
    fname varchar(30) not null,
    account_num int8 not null,
    mi varchar(30),
    address1 varchar(30),
    address2 varchar(30),
    address3 varchar(30),
    address4 varchar(30),
    city varchar(30),
    state_province varchar(30),
    postal_code varchar(30) not null,
    country varchar(30) not null,
    customer_region_id integer not null,
    phone1 varchar(30) not null,
    phone2 varchar(30) not null,
    birthdate date not null,
    marital_status varchar(30) not null,
    income_id integer,
    gender varchar(30) not null,
    total_children smallint not null,
    num_children_at_home smallint not null,
    education varchar(30) not null,
    date_accnt_opened date not null,
    member_card varchar(30),
    occupation varchar(30),
    houseowner varchar(30),
    num_cars_owned integer
);
ALTER TABLE customer LOCK MODE (ROW);

-- Index to avoid automatic named by database (named with pk name).
CREATE UNIQUE INDEX p_customer ON customer(customer_id);
ALTER TABLE customer
 ADD CONSTRAINT 
 PRIMARY KEY (customer_id)
 CONSTRAINT p_customer;

CREATE  INDEX f_customer_1 ON customer(customer_region_id);
ALTER TABLE customer
 ADD CONSTRAINT  FOREIGN KEY (customer_region_id)
 REFERENCES region(region_id)
 CONSTRAINT f_customer_1;

CREATE  INDEX f_customer_2 ON customer(income_id);
ALTER TABLE customer
 ADD CONSTRAINT  FOREIGN KEY (income_id)
 REFERENCES customer_yearly_income(income_id)
 CONSTRAINT f_customer_2;

-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri Jun 21 13:09:09 CEST 2019 Engine: informix
-- sales_fact_month_customer
-- **************************************************************************
CREATE TABLE sales_fact_month_customer (
    customer_id integer not null,
    the_year integer not null,
    unit_sales integer default 0,
    unit_sales_prev integer default 0,
    sales decimal(10,4) default 0.0,
    sales_prev decimal(10,4) default 0.0,
    sales_pct decimal(10,4) default 0.0,
    sales_avg decimal(10,4) default 0.0,
    sales_jan decimal(10,4) default 0.0,
    sales_feb decimal(10,4) default 0.0,
    sales_mar decimal(10,4) default 0.0,
    sales_apr decimal(10,4) default 0.0,
    sales_may decimal(10,4) default 0.0,
    sales_jun decimal(10,4) default 0.0,
    sales_jul decimal(10,4) default 0.0,
    sales_ago decimal(10,4) default 0.0,
    sales_sep decimal(10,4) default 0.0,
    sales_oct decimal(10,4) default 0.0,
    sales_nov decimal(10,4) default 0.0,
    sales_dec decimal(10,4) default 0.0,
    cost decimal(10,4) default 0.0,
    cost_jan decimal(10,4) default 0.0,
    cost_feb decimal(10,4) default 0.0,
    cost_mar decimal(10,4) default 0.0,
    cost_apr decimal(10,4) default 0.0,
    cost_may decimal(10,4) default 0.0,
    cost_jun decimal(10,4) default 0.0,
    cost_jul decimal(10,4) default 0.0,
    cost_ago decimal(10,4) default 0.0,
    cost_sep decimal(10,4) default 0.0,
    cost_oct decimal(10,4) default 0.0,
    cost_nov decimal(10,4) default 0.0,
    cost_dec decimal(10,4) default 0.0,
    cost_prev decimal(10,4) default 0.0,
    cost_pct decimal(10,4) default 0.0,
    cost_avg decimal(10,4) default 0.0,
    profit decimal(10,4) default 0.0,
    promotion_sales decimal(10,4) default 0.0,
    promotion_sales_prev decimal(10,4) default 0.0,
    sales_count integer default 0,
    sales_count_prev integer default 0
);
ALTER TABLE sales_fact_month_customer LOCK MODE (ROW);

-- Index to avoid automatic named by database (named with pk name).
CREATE UNIQUE INDEX p_sales_fact_month_customer ON sales_fact_month_customer(customer_id, the_year);
ALTER TABLE sales_fact_month_customer
 ADD CONSTRAINT 
 PRIMARY KEY (customer_id, the_year)
 CONSTRAINT p_sales_fact_month_customer;

CREATE  INDEX f_sales_fact_month_customer ON sales_fact_month_customer(customer_id);
ALTER TABLE sales_fact_month_customer
 ADD CONSTRAINT  FOREIGN KEY (customer_id)
 REFERENCES customer(customer_id)
 CONSTRAINT f_sales_fact_month_customer;

-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri Jun 21 13:09:29 CEST 2019 Engine: informix
-- sales_fact_yearmonth_customer
-- **************************************************************************
CREATE TABLE sales_fact_yearmonth_customer (
    customer_id integer not null,
    the_year integer not null,
    the_month varchar(30) not null,
    month_of_year integer not null,
    unit_sales integer default 0,
    unit_sales_prev integer default 0,
    sales decimal(10,4) default 0.0,
    sales_prev decimal(10,4) default 0.0,
    sales_pct decimal(10,4) default 0.0,
    sales_avg decimal(10,4) default 0.0,
    cost decimal(10,4) default 0.0,
    cost_prev decimal(10,4) default 0.0,
    cost_pct decimal(10,4) default 0.0,
    cost_avg decimal(10,4) default 0.0,
    profit decimal(10,4) default 0.0,
    promotion_sales decimal(10,4) default 0.0,
    promotion_sales_prev decimal(10,4) default 0.0,
    sales_count integer default 0,
    sales_count_prev integer default 0
);
ALTER TABLE sales_fact_yearmonth_customer LOCK MODE (ROW);

-- Index to avoid automatic named by database (named with pk name).
CREATE UNIQUE INDEX p_sales_fact_yearmonth_customer ON sales_fact_yearmonth_customer(customer_id, the_year, month_of_year);
ALTER TABLE sales_fact_yearmonth_customer
 ADD CONSTRAINT 
 PRIMARY KEY (customer_id, the_year, month_of_year)
 CONSTRAINT p_sales_fact_yearmonth_customer;

CREATE  INDEX f_sales_fact_yearmonth_customer ON sales_fact_yearmonth_customer(customer_id);
ALTER TABLE sales_fact_yearmonth_customer
 ADD CONSTRAINT  FOREIGN KEY (customer_id)
 REFERENCES customer(customer_id)
 CONSTRAINT f_sales_fact_yearmonth_customer;
Copy
-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri Jun 21 13:08:55 CEST 2019 Engine: postgres
-- customer
-- **************************************************************************
CREATE TABLE customer (
    customer_id integer not null,
    fullname varchar(30) not null,
    lname varchar(30) not null,
    fname varchar(30) not null,
    account_num int8 not null,
    mi varchar(30),
    address1 varchar(30),
    address2 varchar(30),
    address3 varchar(30),
    address4 varchar(30),
    city varchar(30),
    state_province varchar(30),
    postal_code varchar(30) not null,
    country varchar(30) not null,
    customer_region_id integer not null,
    phone1 varchar(30) not null,
    phone2 varchar(30) not null,
    birthdate date not null,
    marital_status varchar(30) not null,
    income_id integer,
    gender varchar(30) not null,
    total_children smallint not null,
    num_children_at_home smallint not null,
    education varchar(30) not null,
    date_accnt_opened date not null,
    member_card varchar(30),
    occupation varchar(30),
    houseowner varchar(30),
    num_cars_owned integer
) WITH OIDS;

-- Database Server on Postgres, DB2 will be created a index automatically with the same name of pk
ALTER TABLE customer
 ADD CONSTRAINT p_customer
 PRIMARY KEY (customer_id);

CREATE  INDEX f_customer_1 ON customer(customer_region_id);
ALTER TABLE customer
 ADD CONSTRAINT f_customer_1 FOREIGN KEY (customer_region_id)
 REFERENCES region(region_id) DEFERRABLE INITIALLY IMMEDIATE ;

CREATE  INDEX f_customer_2 ON customer(income_id);
ALTER TABLE customer
 ADD CONSTRAINT f_customer_2 FOREIGN KEY (income_id)
 REFERENCES customer_yearly_income(income_id) DEFERRABLE INITIALLY IMMEDIATE ;

-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri Jun 21 13:09:19 CEST 2019 Engine: postgres
-- sales_fact_month_customer
-- **************************************************************************
CREATE TABLE sales_fact_month_customer (
    customer_id integer not null,
    the_year integer not null,
    unit_sales integer default 0,
    unit_sales_prev integer default 0,
    sales decimal(10,4) default 0.0,
    sales_prev decimal(10,4) default 0.0,
    sales_pct decimal(10,4) default 0.0,
    sales_avg decimal(10,4) default 0.0,
    sales_jan decimal(10,4) default 0.0,
    sales_feb decimal(10,4) default 0.0,
    sales_mar decimal(10,4) default 0.0,
    sales_apr decimal(10,4) default 0.0,
    sales_may decimal(10,4) default 0.0,
    sales_jun decimal(10,4) default 0.0,
    sales_jul decimal(10,4) default 0.0,
    sales_ago decimal(10,4) default 0.0,
    sales_sep decimal(10,4) default 0.0,
    sales_oct decimal(10,4) default 0.0,
    sales_nov decimal(10,4) default 0.0,
    sales_dec decimal(10,4) default 0.0,
    cost decimal(10,4) default 0.0,
    cost_jan decimal(10,4) default 0.0,
    cost_feb decimal(10,4) default 0.0,
    cost_mar decimal(10,4) default 0.0,
    cost_apr decimal(10,4) default 0.0,
    cost_may decimal(10,4) default 0.0,
    cost_jun decimal(10,4) default 0.0,
    cost_jul decimal(10,4) default 0.0,
    cost_ago decimal(10,4) default 0.0,
    cost_sep decimal(10,4) default 0.0,
    cost_oct decimal(10,4) default 0.0,
    cost_nov decimal(10,4) default 0.0,
    cost_dec decimal(10,4) default 0.0,
    cost_prev decimal(10,4) default 0.0,
    cost_pct decimal(10,4) default 0.0,
    cost_avg decimal(10,4) default 0.0,
    profit decimal(10,4) default 0.0,
    promotion_sales decimal(10,4) default 0.0,
    promotion_sales_prev decimal(10,4) default 0.0,
    sales_count integer default 0,
    sales_count_prev integer default 0
) WITH OIDS;

-- Database Server on Postgres, DB2 will be created a index automatically with the same name of pk
ALTER TABLE sales_fact_month_customer
 ADD CONSTRAINT p_sales_fact_month_customer
 PRIMARY KEY (customer_id, the_year);

CREATE  INDEX f_sales_fact_month_customer ON sales_fact_month_customer(customer_id);
ALTER TABLE sales_fact_month_customer
 ADD CONSTRAINT f_sales_fact_month_customer FOREIGN KEY (customer_id)
 REFERENCES customer(customer_id) DEFERRABLE INITIALLY IMMEDIATE ;

-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri Jun 21 13:09:37 CEST 2019 Engine: postgres
-- sales_fact_yearmonth_customer
-- **************************************************************************
CREATE TABLE sales_fact_yearmonth_customer (
    customer_id integer not null,
    the_year integer not null,
    the_month varchar(30) not null,
    month_of_year integer not null,
    unit_sales integer default 0,
    unit_sales_prev integer default 0,
    sales decimal(10,4) default 0.0,
    sales_prev decimal(10,4) default 0.0,
    sales_pct decimal(10,4) default 0.0,
    sales_avg decimal(10,4) default 0.0,
    cost decimal(10,4) default 0.0,
    cost_prev decimal(10,4) default 0.0,
    cost_pct decimal(10,4) default 0.0,
    cost_avg decimal(10,4) default 0.0,
    profit decimal(10,4) default 0.0,
    promotion_sales decimal(10,4) default 0.0,
    promotion_sales_prev decimal(10,4) default 0.0,
    sales_count integer default 0,
    sales_count_prev integer default 0
) WITH OIDS;

-- Database Server on Postgres, DB2 will be created a index automatically with the same name of pk
ALTER TABLE sales_fact_yearmonth_customer
 ADD CONSTRAINT p_sales_fact_yearmonth_customer
 PRIMARY KEY (customer_id, the_year, month_of_year);

CREATE  INDEX f_sales_fact_yearmonth_customer ON sales_fact_yearmonth_customer(customer_id);
ALTER TABLE sales_fact_yearmonth_customer
 ADD CONSTRAINT f_sales_fact_yearmonth_customer FOREIGN KEY (customer_id)
 REFERENCES customer(customer_id) DEFERRABLE INITIALLY IMMEDIATE ;
Customer: Customer
Column Type Details Definition
customer_id integer Primary key. Customer identifier.
account_number bigint Customer's account number.
lname varchar Last name.
fname varchar First name.
mi varchar Middle Initial.
address[number] varchar Customer's address. There are four of them
Columns: address1, address2...
city varchar Customer's city.
state_province varchar Customer's state province.
postal_code varchar Customer's postal code.
country decimal Customer's country.
customer_region_id decimal Refers to region.region_id Product's net weight.
phone[number] varchar Customer's telephone number. There's two of them.
Columns: phone1, phone2
birthdate date Customer's birth date.
marital_status varchar Customer's marital status.
yearly_income varchar Customer's yearly income.
gender varchar Customer's gender.
total_children smallint Customer's number of children.
num_children_at_home smallint Customer's number of children at home.
education varchar Customer's education.
date_accnt_opened varchar Date customer opened account.
member_card varchar Customer's type of membership card.
occupation varchar Customer's occupation.
houseowner varchar Whether or not customer owns a house.
num_cars_owned integer Number of cars customer owns.
fullname varchar Customer's fullname.

customer provides the customers information.

Customer Sales by Year: sales_fact_month_customer
Column Type Details Definition
customer_id integer Primary key.
References customer.customer_id.
Customer identifier.
the_year integer Primary key. Year.
unit_sales integer Units sold of the customer.
unit_sales_prev integer Units sold of the customer the previous year.
sales decimal Customer sales.
sales_prev decimal Customer sales the previous year.
sales_pct decimal Customer sales percentage.
sales_avg decimal Customer sales month average.
sales_[mon] decimal Customer sales per month of year.
Example: sales_jan, sales_feb...
cost decimal Customer cost.
cost_prev decimal Customer cost the previous year.
cost_pct decimal Customer cost percentage.
cost_avg decimal Customer cost month average.
cost_[mon] decimal Customer cost per month of year.
Example: cost_jan, cost_feb...
profit decimal Customer profit = sales - cost.
promotion_sales decimal Customer sales when promotion is applied.
promotion_sales_prev decimal Customer sales when promotion is applied of previous year.
sales_count integer Sales counter.

sales_fact_month_customer stores yearly information about customers and also monthly sales and costs.

Customer Sales by Month: sales_fact_yearmonth_customer
Column Type Details Definition
customer_id integer Primary key.
References customer.customer_id.
Customer identifier.
the_year integer Primary key. Year.
the_month varchar Month's name.
month_of_year integer Primary key. Month's number.
unit_sales integer Units sold of the customer.
unit_sales_prev integer Units sold of the customer the previous month.
sales decimal Customer sales.
sales_prev decimal Customer sales the previous month.
sales_pct decimal Customer sales percentage.
sales_avg decimal Customer sales day average.
cost decimal Customer cost.
cost_prev decimal Customer cost the previous month.
cost_pct decimal Customer cost percentage.
cost_avg decimal Customer cost day average.
profit decimal Customer profit = sales - cost.
promotion_sales decimal Customer sales when promotion is applied.
promotion_sales_prev decimal Customer sales when promotion is applied of previous month.
sales_count integer Sales counter.

sales_fact_yearmonth_customer provides monthly information about customers.

2.5 Store

In this section all the tables associated with store will be placed here.

<table name='warehouse_class'>  
    <!-- COLUMNS -->
    <column name='warehouse_class_id'       type='integer'                  required='y'/> 
    <column name='warehouse_name'           type='varchar'      size='30'               />

    
    <primary name='p_warehouse_class'   columns='warehouse_class_id'/>
    
</table>
<table name='warehouse'>

   <!-- COLUMNS -->
   <column name='warehouse_id'               type='integer'                required='y'  />
   <column name='warehouse_class_id'         type='integer'                              />
   <column name='stores_id'                  type='integer'                              />
   <column name='warehouse_name'             type='varchar'  size='60'                   />
   <column name='wa_address1'                type='varchar'  size='30'                   />
   <column name='wa_address2'                type='varchar'  size='30'                   />
   <column name='wa_address3'                type='varchar'  size='30'                   />
   <column name='wa_address4'                type='varchar'  size='30'                   />
   <column name='warehouse_city'             type='varchar'  size='30'                   />
   <column name='warehouse_state_province'   type='varchar'  size='30'                   />
   <column name='warehouse_postal_code'      type='varchar'  size='30'                   />
   <column name='warehouse_country'          type='varchar'  size='30'                   />
   <column name='warehouse_owner_name'       type='varchar'  size='30'                   />
   <column name='warehouse_phone'            type='varchar'  size='30'                   />
   <column name='warehouse_fax'              type='varchar'  size='30'                   />
   
   <primary name='p_warehouse' columns='warehouse_id' />
   
</table>
<table name='department'>

    <column name='department_id'            type='integer'                          required='y'/>
    <column name='department_description'   type='varchar'  size='30'               required='y'/>
    <column name='state'                    type='char'     size='1'    default='A' required='y'/>
    
</table>
<table name='position'>

   <!-- COLUMNS -->
   <column name='position_id' 		        type='integer'                      required='y'    />
   <column name='position_title'			type='varchar' 		size='30'       required='y'    />
   <column name='pay_type' 		            type='varchar' 		size='30'       required='y'    />
   <column name='min_scale' 	           	type='decimal' 		size='10,4'     required='y'    />
   <column name='max_scale'	    	        type='decimal' 		size='10,4'     required='y'    />
   <column name='management_role'	        type='varchar' 		size='30' 	    required='y'    />

</table>
<table name='employee'>

    <!-- COLUMNS -->
    <column name='employee_id'      type='integer'      size='10'                               required='y'/>
    <column name='full_name'        type='varchar'      size='30'                               required='y'/>
    <column name='first_name'       type='varchar'      size='30'                               required='y'/>
    <column name='last_name'        type='varchar'      size='30'                               required='y'/>
    <column name='position_id'      type='integer'      size='10'                                           />
    <column name='position_title'   type='varchar'      size='30'                                           />
    <column name='store_id'         type='integer'      size='10'                               required='y'/>
    <column name='department_id'    type='integer'      size='10'                               required='y'/>
    <column name='birth_date'       type='date'         size='13'                               required='y'/>
    <column name='hire_date'        type='datetime'     size='year to second'                               />
    <column name='end_date'         type='datetime'     size='year to second'                               />
    <column name='salary'           type='decimal'      size='10,4'                             required='y'/>
    <column name='supervisor_id'    type='integer'      size='10'                                           />
    <column name='education_level'  type='varchar'      size='30'                               required='y'/>
    <column name='marital_status'   type='varchar'      size='30'                               required='y'/>
    <column name='gender'           type='varchar'      size='30'                               required='y'/>
    <column name='management_role'  type='varchar'      size='30'               				            />
    <column name='state'            type='char'         size='1'                default='A'     required='y'/>
    
    <!-- PRIMARY KEY -->
    <primary name='p_employee'          columns='employee_id'                                               />
    
    <!-- FOREIGN KEY -->
    <foreign name='f_employee_1'        columns='store_id'          references='store'          refcols='store_id'      />
    <foreign name='f_employee_2'        columns='position_id'       references='position'       refcols='position_id'   />
    <foreign name='f_employee_3'        columns='employee_id'       references='salary'         refcols='employee_id'   />
    <foreign name='f_employee_4'        columns='department_id'     references='department'     refcols='department_id' />
   
</table>
<table name='store'>
    
    <!-- COLUMNS -->
    <column name='store_id'             type='serial'                                               required='y'/>
    <column name='store_type'           type='varchar'      size='30'                                           />
    <column name='region_id'            type='integer'                                                          />
    <column name='store_name'           type='varchar'      size='30'                                           />
    <column name='store_number'         type='integer'                                                          />
    <column name='store_street_address' type='varchar'      size='30'                                           />
    <column name='store_city'           type='varchar'      size='30'                                           />
    <column name='store_state'          type='varchar'      size='30'                                           />
    <column name='store_postal_code'    type='varchar'      size='30'                                           />
    <column name='store_country'        type='varchar'      size='30'                                           />
    <column name='store_manager'        type='varchar'      size='30'                                           />
    <column name='store_phone'          type='varchar'      size='30'                                           />
    <column name='store_fax'            type='varchar'      size='30'                                           />
    <column name='first_opened_date'    type='datetime'     size='year to second'                               />
    <column name='last_remodel_date'    type='datetime'     size='year to second'                               />
    <column name='store_sqft'           type='integer'                                                          />
    <column name='grocery_sqft'         type='integer'                                                          />
    <column name='frozen_sqft'          type='integer'                                                          />
    <column name='meat_sqft'            type='integer'                                                          />
    <column name='coffee_bar'           type='boolean'                                                          />
    <column name='video_store'          type='boolean'                                                          />
    <column name='salad_bar'            type='boolean'                                                          />
    <column name='prepared_food'        type='boolean'                                                          />
    <column name='florist'              type='boolean'                                                          />
    
    <column name='state'                type='char'         size='1'                default='A'     required='y'/>
    
    <!-- PRIMARY KEY --> 
    <primary name='p_store'         columns='store_id'                                                  />
    
    <!-- FOREIGN KEY --> 
    <foreign name='f_store_1'       columns='region_id'     references='region'     refcols='region_id' />
    
</table>
<!-- Automatic Build -->
<table name='sales_fact_month_store'>
    
    <!-- COLUMNS -->
    <column name='store_id'                 type='integer'                                      required='y'/>
    <column name='the_year'                 type='integer'                                      required='y'/>
    <!-- UNITS -->
    <column name='unit_sales'               type='integer'                       default='0'                />
    <column name='unit_sales_prev'          type='integer'                       default='0'                />
    <!-- SALES -->
    <column name='sales'                    type='decimal'      size='10,4'     default='0.0'               />
    <column name='sales_prev'               type='decimal'      size='10,4'     default='0.0'               />
    <column name='sales_pct'                type='decimal'      size='10,4'     default='0.0'               />
    <column name='sales_avg'                type='decimal'      size='10,4'     default='0.0'               />

    <column name='sales_jan'                type='decimal'      size='10,4'     default='0.0'               />
    <column name='sales_feb'                type='decimal'      size='10,4'     default='0.0'               />
    <column name='sales_mar'                type='decimal'      size='10,4'     default='0.0'               />
    <column name='sales_apr'                type='decimal'      size='10,4'     default='0.0'               />
    <column name='sales_may'                type='decimal'      size='10,4'     default='0.0'               />
    <column name='sales_jun'                type='decimal'      size='10,4'     default='0.0'               />
    <column name='sales_jul'                type='decimal'      size='10,4'     default='0.0'               />
    <column name='sales_ago'                type='decimal'      size='10,4'     default='0.0'               />
    <column name='sales_sep'                type='decimal'      size='10,4'     default='0.0'               />
    <column name='sales_oct'                type='decimal'      size='10,4'     default='0.0'               />
    <column name='sales_nov'                type='decimal'      size='10,4'     default='0.0'               />
    <column name='sales_dec'                type='decimal'      size='10,4'     default='0.0'               />

    <!-- COST -->
    <column name='cost'                     type='decimal'      size='10,4'     default='0.0'               />

    <column name='cost_jan'                 type='decimal'      size='10,4'     default='0.0'               />
    <column name='cost_feb'                 type='decimal'      size='10,4'     default='0.0'               />
    <column name='cost_mar'                 type='decimal'      size='10,4'     default='0.0'               />
    <column name='cost_apr'                 type='decimal'      size='10,4'     default='0.0'               />
    <column name='cost_may'                 type='decimal'      size='10,4'     default='0.0'               />
    <column name='cost_jun'                 type='decimal'      size='10,4'     default='0.0'               />
    <column name='cost_jul'                 type='decimal'      size='10,4'     default='0.0'               />
    <column name='cost_ago'                 type='decimal'      size='10,4'     default='0.0'               />
    <column name='cost_sep'                 type='decimal'      size='10,4'     default='0.0'               />
    <column name='cost_oct'                 type='decimal'      size='10,4'     default='0.0'               />
    <column name='cost_nov'                 type='decimal'      size='10,4'     default='0.0'               />
    <column name='cost_dec'                 type='decimal'      size='10,4'     default='0.0'               />

    <column name='cost_prev'                type='decimal'      size='10,4'     default='0.0'               />
    <column name='cost_pct'                 type='decimal'      size='10,4'     default='0.0'               />
    <column name='cost_avg'                 type='decimal'      size='10,4'     default='0.0'               />
    <!-- PROFIT -->
    <column name='profit'                   type='decimal'      size='10,4'     default='0.0'               />

    <!-- PROMOTION -->
    <column name='promotion_sales'          type='decimal'      size='10,4'     default='0.0'               />
    <column name='promotion_sales_prev'     type='decimal'      size='10,4'     default='0.0'               />
        
    <!-- TICKETS -->
    <column name='sales_count'              type='integer'                      default='0'                 />
    <column name='sales_count_prev'         type='integer'                      default='0'                 />

    
    <!-- PRIMARY -->
    <primary name='p_sales_fact_month_store'        columns='store_id, the_year'                                            />

    <foreign name='f_sales_fact_month_store'        columns='store_id'              references='store'  refcols='store_id'  />
    
</table>
<!-- Automatic Build -->
<table name='sales_fact_yearmonth_store'>
    
    <!-- COLUMNS -->
    <column name='store_id'             type='integer'                                      required='y'/>
    <column name='the_year'             type='integer'                                      required='y'/>
    <column name='the_month'            type='varchar'      size='30'                       required='y'/>
    <column name='month_of_year'        type='integer'                                      required='y'/>
    <!-- UNITS -->
    <column name='unit_sales'           type='integer'                      default='0'                 />
    <column name='unit_sales_prev'      type='integer'                      default='0'                 />
    <!-- SALES -->
    <column name='sales'                type='decimal'      size='10,4'     default='0.0'               />
    <column name='sales_prev'           type='decimal'      size='10,4'     default='0.0'               />
    <column name='sales_pct'            type='decimal'      size='10,4'     default='0.0'               />
    <column name='sales_avg'            type='decimal'      size='10,4'     default='0.0'               />

    <!-- COST -->
    <column name='cost'                 type='decimal'      size='10,4'     default='0.0'               />
    <column name='cost_prev'            type='decimal'      size='10,4'     default='0.0'               />
    <column name='cost_pct'             type='decimal'      size='10,4'     default='0.0'               />
    <column name='cost_avg'             type='decimal'      size='10,4'     default='0.0'               />
    <!-- PROFIT -->
    <column name='profit'               type='decimal'      size='10,4'     default='0.0'               />

    <!-- PROMOTION -->
    <column name='promotion_sales'      type='decimal'      size='10,4'     default='0.0'               />
    <column name='promotion_sales_prev' type='decimal'      size='10,4'     default='0.0'               />

    <!-- TICKETS -->
    <column name='sales_count'          type='integer'                      default='0'                 />
    <column name='sales_count_prev'     type='integer'                      default='0'                 />
    
    
    <!-- PRIMARY -->
    <primary name='p_sales_fact_yearmonth_store'        columns='store_id, the_year, month_of_year'                                             />

    <foreign name='f_sales_fact_yearmonth_store'        columns='store_id'                          references='store'      refcols='store_id'  />

</table>
Copy
-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri Jun 21 15:59:12 CEST 2019 Engine: informix
-- warehouse_class
-- **************************************************************************
CREATE TABLE warehouse_class (
    warehouse_class_id integer not null,
    warehouse_name varchar(30)
);
ALTER TABLE warehouse_class LOCK MODE (ROW);

-- Index to avoid automatic named by database (named with pk name).
CREATE UNIQUE INDEX p_warehouse_class ON warehouse_class(warehouse_class_id);
ALTER TABLE warehouse_class
 ADD CONSTRAINT 
 PRIMARY KEY (warehouse_class_id)
 CONSTRAINT p_warehouse_class;

-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri Jun 21 15:56:02 CEST 2019 Engine: informix
-- warehouse
-- **************************************************************************
CREATE TABLE warehouse (
    warehouse_id integer not null,
    warehouse_class_id integer,
    stores_id integer,
    warehouse_name varchar(60),
    wa_address1 varchar(30),
    wa_address2 varchar(30),
    wa_address3 varchar(30),
    wa_address4 varchar(30),
    warehouse_city varchar(30),
    warehouse_state_province varchar(30),
    warehouse_postal_code varchar(30),
    warehouse_country varchar(30),
    warehouse_owner_name varchar(30),
    warehouse_phone varchar(30),
    warehouse_fax varchar(30)
);
ALTER TABLE warehouse LOCK MODE (ROW);

-- Index to avoid automatic named by database (named with pk name).
CREATE UNIQUE INDEX p_warehouse ON warehouse(warehouse_id);
ALTER TABLE warehouse
 ADD CONSTRAINT 
 PRIMARY KEY (warehouse_id)
 CONSTRAINT p_warehouse;

-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri Jun 21 16:00:08 CEST 2019 Engine: informix
-- department
-- **************************************************************************
CREATE TABLE department (
    department_id integer not null,
    department_description varchar(30) not null,
    state char(1) default 'A' not null
);
ALTER TABLE department LOCK MODE (ROW);

-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri Jun 21 16:00:28 CEST 2019 Engine: informix
-- position
-- **************************************************************************
CREATE TABLE position (
    position_id integer not null,
    position_title varchar(30) not null,
    pay_type varchar(30) not null,
    min_scale decimal(10,4) not null,
    max_scale decimal(10,4) not null,
    management_role varchar(30) not null
);
ALTER TABLE position LOCK MODE (ROW);

-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri Jun 21 16:00:50 CEST 2019 Engine: informix
-- employee
-- **************************************************************************
CREATE TABLE employee (
    employee_id integer not null,
    full_name varchar(30) not null,
    first_name varchar(30) not null,
    last_name varchar(30) not null,
    position_id integer,
    position_title varchar(30),
    store_id integer not null,
    department_id integer not null,
    birth_date date not null,
    hire_date datetime year to second,
    end_date datetime year to second,
    salary decimal(10,4) not null,
    supervisor_id integer,
    education_level varchar(30) not null,
    marital_status varchar(30) not null,
    gender varchar(30) not null,
    management_role varchar(30),
    state char(1) default 'A' not null
);
ALTER TABLE employee LOCK MODE (ROW);

-- Index to avoid automatic named by database (named with pk name).
CREATE UNIQUE INDEX p_employee ON employee(employee_id);
ALTER TABLE employee
 ADD CONSTRAINT 
 PRIMARY KEY (employee_id)
 CONSTRAINT p_employee;

CREATE  INDEX f_employee_1 ON employee(store_id);
ALTER TABLE employee
 ADD CONSTRAINT  FOREIGN KEY (store_id)
 REFERENCES store(store_id)
 CONSTRAINT f_employee_1;

CREATE  INDEX f_employee_2 ON employee(position_id);
ALTER TABLE employee
 ADD CONSTRAINT  FOREIGN KEY (position_id)
 REFERENCES position(position_id)
 CONSTRAINT f_employee_2;

CREATE  INDEX f_employee_3 ON employee(supervisor_id);
ALTER TABLE employee
 ADD CONSTRAINT  FOREIGN KEY (supervisor_id)
 REFERENCES employee(employee_id)
 CONSTRAINT f_employee_3;

CREATE  INDEX f_employee_4 ON employee(department_id);
ALTER TABLE employee
 ADD CONSTRAINT  FOREIGN KEY (department_id)
 REFERENCES department(department_id)
 CONSTRAINT f_employee_4;

-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri Jun 21 16:01:28 CEST 2019 Engine: informix
-- store
-- **************************************************************************
CREATE TABLE store (
    store_id serial not null,
    store_type varchar(30),
    region_id integer,
    store_name varchar(30),
    store_number integer,
    store_street_address varchar(30),
    store_city varchar(30),
    store_state varchar(30),
    store_postal_code varchar(30),
    store_country varchar(30),
    store_manager varchar(30),
    store_phone varchar(30),
    store_fax varchar(30),
    first_opened_date datetime year to second,
    last_remodel_date datetime year to second,
    store_sqft integer,
    grocery_sqft integer,
    frozen_sqft integer,
    meat_sqft integer,
    coffee_bar boolean,
    video_store boolean,
    salad_bar boolean,
    prepared_food boolean,
    florist boolean,
    state char(1) default 'A' not null
);
ALTER TABLE store LOCK MODE (ROW);

-- Index to avoid automatic named by database (named with pk name).
CREATE UNIQUE INDEX p_store ON store(store_id);
ALTER TABLE store
 ADD CONSTRAINT 
 PRIMARY KEY (store_id)
 CONSTRAINT p_store;

CREATE  INDEX f_store_1 ON store(region_id);
ALTER TABLE store
 ADD CONSTRAINT  FOREIGN KEY (region_id)
 REFERENCES region(region_id)
 CONSTRAINT f_store_1;

-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri Jun 21 16:02:07 CEST 2019 Engine: informix
-- sales_fact_month_store
-- **************************************************************************
CREATE TABLE sales_fact_month_store (
    store_id integer not null,
    the_year integer not null,
    unit_sales integer default 0,
    unit_sales_prev integer default 0,
    sales decimal(10,4) default 0.0,
    sales_prev decimal(10,4) default 0.0,
    sales_pct decimal(10,4) default 0.0,
    sales_avg decimal(10,4) default 0.0,
    sales_jan decimal(10,4) default 0.0,
    sales_feb decimal(10,4) default 0.0,
    sales_mar decimal(10,4) default 0.0,
    sales_apr decimal(10,4) default 0.0,
    sales_may decimal(10,4) default 0.0,
    sales_jun decimal(10,4) default 0.0,
    sales_jul decimal(10,4) default 0.0,
    sales_ago decimal(10,4) default 0.0,
    sales_sep decimal(10,4) default 0.0,
    sales_oct decimal(10,4) default 0.0,
    sales_nov decimal(10,4) default 0.0,
    sales_dec decimal(10,4) default 0.0,
    cost decimal(10,4) default 0.0,
    cost_jan decimal(10,4) default 0.0,
    cost_feb decimal(10,4) default 0.0,
    cost_mar decimal(10,4) default 0.0,
    cost_apr decimal(10,4) default 0.0,
    cost_may decimal(10,4) default 0.0,
    cost_jun decimal(10,4) default 0.0,
    cost_jul decimal(10,4) default 0.0,
    cost_ago decimal(10,4) default 0.0,
    cost_sep decimal(10,4) default 0.0,
    cost_oct decimal(10,4) default 0.0,
    cost_nov decimal(10,4) default 0.0,
    cost_dec decimal(10,4) default 0.0,
    cost_prev decimal(10,4) default 0.0,
    cost_pct decimal(10,4) default 0.0,
    cost_avg decimal(10,4) default 0.0,
    profit decimal(10,4) default 0.0,
    promotion_sales decimal(10,4) default 0.0,
    promotion_sales_prev decimal(10,4) default 0.0,
    sales_count integer default 0,
    sales_count_prev integer default 0
);
ALTER TABLE sales_fact_month_store LOCK MODE (ROW);

-- Index to avoid automatic named by database (named with pk name).
CREATE UNIQUE INDEX p_sales_fact_month_store ON sales_fact_month_store(store_id, the_year);
ALTER TABLE sales_fact_month_store
 ADD CONSTRAINT 
 PRIMARY KEY (store_id, the_year)
 CONSTRAINT p_sales_fact_month_store;

CREATE  INDEX f_sales_fact_month_store ON sales_fact_month_store(store_id);
ALTER TABLE sales_fact_month_store
 ADD CONSTRAINT  FOREIGN KEY (store_id)
 REFERENCES store(store_id)
 CONSTRAINT f_sales_fact_month_store;

-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri Jun 21 16:02:41 CEST 2019 Engine: informix
-- sales_fact_yearmonth_store
-- **************************************************************************
CREATE TABLE sales_fact_yearmonth_store (
    store_id integer not null,
    the_year integer not null,
    the_month varchar(30) not null,
    month_of_year integer not null,
    unit_sales integer default 0,
    unit_sales_prev integer default 0,
    sales decimal(10,4) default 0.0,
    sales_prev decimal(10,4) default 0.0,
    sales_pct decimal(10,4) default 0.0,
    sales_avg decimal(10,4) default 0.0,
    cost decimal(10,4) default 0.0,
    cost_prev decimal(10,4) default 0.0,
    cost_pct decimal(10,4) default 0.0,
    cost_avg decimal(10,4) default 0.0,
    profit decimal(10,4) default 0.0,
    promotion_sales decimal(10,4) default 0.0,
    promotion_sales_prev decimal(10,4) default 0.0,
    sales_count integer default 0,
    sales_count_prev integer default 0
);
ALTER TABLE sales_fact_yearmonth_store LOCK MODE (ROW);

-- Index to avoid automatic named by database (named with pk name).
CREATE UNIQUE INDEX p_sales_fact_yearmonth_store ON sales_fact_yearmonth_store(store_id, the_year, month_of_year);
ALTER TABLE sales_fact_yearmonth_store
 ADD CONSTRAINT 
 PRIMARY KEY (store_id, the_year, month_of_year)
 CONSTRAINT p_sales_fact_yearmonth_store;

CREATE  INDEX f_sales_fact_yearmonth_store ON sales_fact_yearmonth_store(store_id);
ALTER TABLE sales_fact_yearmonth_store
 ADD CONSTRAINT  FOREIGN KEY (store_id)
 REFERENCES store(store_id)
 CONSTRAINT f_sales_fact_yearmonth_store;
Copy
-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri Jun 21 15:59:30 CEST 2019 Engine: postgres
-- warehouse_class
-- **************************************************************************
CREATE TABLE warehouse_class (
    warehouse_class_id integer not null,
    warehouse_name varchar(30)
) WITH OIDS;

-- Database Server on Postgres, DB2 will be created a index automatically with the same name of pk
ALTER TABLE warehouse_class
 ADD CONSTRAINT p_warehouse_class
 PRIMARY KEY (warehouse_class_id);

-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri Jun 21 15:56:20 CEST 2019 Engine: postgres
-- warehouse
-- **************************************************************************
CREATE TABLE warehouse (
    warehouse_id integer not null,
    warehouse_class_id integer,
    stores_id integer,
    warehouse_name varchar(60),
    wa_address1 varchar(30),
    wa_address2 varchar(30),
    wa_address3 varchar(30),
    wa_address4 varchar(30),
    warehouse_city varchar(30),
    warehouse_state_province varchar(30),
    warehouse_postal_code varchar(30),
    warehouse_country varchar(30),
    warehouse_owner_name varchar(30),
    warehouse_phone varchar(30),
    warehouse_fax varchar(30)
) WITH OIDS;

-- Database Server on Postgres, DB2 will be created a index automatically with the same name of pk
ALTER TABLE warehouse
 ADD CONSTRAINT p_warehouse
 PRIMARY KEY (warehouse_id);

-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri Jun 21 16:00:15 CEST 2019 Engine: postgres
-- department
-- **************************************************************************
CREATE TABLE department (
    department_id integer not null,
    department_description varchar(30) not null,
    state varchar(1) default 'A' not null
) WITH OIDS;

-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri Jun 21 16:00:35 CEST 2019 Engine: postgres
-- position
-- **************************************************************************
CREATE TABLE position (
    position_id integer not null,
    position_title varchar(30) not null,
    pay_type varchar(30) not null,
    min_scale decimal(10,4) not null,
    max_scale decimal(10,4) not null,
    management_role varchar(30) not null
) WITH OIDS;

-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri Jun 21 16:00:56 CEST 2019 Engine: postgres
-- employee
-- **************************************************************************
CREATE TABLE employee (
    employee_id integer not null,
    full_name varchar(30) not null,
    first_name varchar(30) not null,
    last_name varchar(30) not null,
    position_id integer,
    position_title varchar(30),
    store_id integer not null,
    department_id integer not null,
    birth_date date not null,
    hire_date timestamp,
    end_date timestamp,
    salary decimal(10,4) not null,
    supervisor_id integer,
    education_level varchar(30) not null,
    marital_status varchar(30) not null,
    gender varchar(30) not null,
    management_role varchar(30),
    state varchar(1) default 'A' not null
) WITH OIDS;

-- Database Server on Postgres, DB2 will be created a index automatically with the same name of pk
ALTER TABLE employee
 ADD CONSTRAINT p_employee
 PRIMARY KEY (employee_id);

CREATE  INDEX f_employee_1 ON employee(store_id);
ALTER TABLE employee
 ADD CONSTRAINT f_employee_1 FOREIGN KEY (store_id)
 REFERENCES store(store_id) DEFERRABLE INITIALLY IMMEDIATE ;

CREATE  INDEX f_employee_2 ON employee(position_id);
ALTER TABLE employee
 ADD CONSTRAINT f_employee_2 FOREIGN KEY (position_id)
 REFERENCES position(position_id) DEFERRABLE INITIALLY IMMEDIATE ;

CREATE  INDEX f_employee_3 ON employee(supervisor_id);
ALTER TABLE employee
 ADD CONSTRAINT f_employee_3 FOREIGN KEY (supervisor_id)
 REFERENCES employee(employee_id) DEFERRABLE INITIALLY IMMEDIATE ;

CREATE  INDEX f_employee_4 ON employee(department_id);
ALTER TABLE employee
 ADD CONSTRAINT f_employee_4 FOREIGN KEY (department_id)
 REFERENCES department(department_id) DEFERRABLE INITIALLY IMMEDIATE ;

-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri Jun 21 16:01:39 CEST 2019 Engine: postgres
-- store
-- **************************************************************************
CREATE TABLE store (
    store_id serial not null,
    store_type varchar(30),
    region_id integer,
    store_name varchar(30),
    store_number integer,
    store_street_address varchar(30),
    store_city varchar(30),
    store_state varchar(30),
    store_postal_code varchar(30),
    store_country varchar(30),
    store_manager varchar(30),
    store_phone varchar(30),
    store_fax varchar(30),
    first_opened_date timestamp,
    last_remodel_date timestamp,
    store_sqft integer,
    grocery_sqft integer,
    frozen_sqft integer,
    meat_sqft integer,
    coffee_bar boolean,
    video_store boolean,
    salad_bar boolean,
    prepared_food boolean,
    florist boolean,
    state varchar(1) default 'A' not null
) WITH OIDS;

-- Database Server on Postgres, DB2 will be created a index automatically with the same name of pk
ALTER TABLE store
 ADD CONSTRAINT p_store
 PRIMARY KEY (store_id);

CREATE  INDEX f_store_1 ON store(region_id);
ALTER TABLE store
 ADD CONSTRAINT f_store_1 FOREIGN KEY (region_id)
 REFERENCES region(region_id) DEFERRABLE INITIALLY IMMEDIATE ;

-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri Jun 21 16:02:17 CEST 2019 Engine: postgres
-- sales_fact_month_store
-- **************************************************************************
CREATE TABLE sales_fact_month_store (
    store_id integer not null,
    the_year integer not null,
    unit_sales integer default 0,
    unit_sales_prev integer default 0,
    sales decimal(10,4) default 0.0,
    sales_prev decimal(10,4) default 0.0,
    sales_pct decimal(10,4) default 0.0,
    sales_avg decimal(10,4) default 0.0,
    sales_jan decimal(10,4) default 0.0,
    sales_feb decimal(10,4) default 0.0,
    sales_mar decimal(10,4) default 0.0,
    sales_apr decimal(10,4) default 0.0,
    sales_may decimal(10,4) default 0.0,
    sales_jun decimal(10,4) default 0.0,
    sales_jul decimal(10,4) default 0.0,
    sales_ago decimal(10,4) default 0.0,
    sales_sep decimal(10,4) default 0.0,
    sales_oct decimal(10,4) default 0.0,
    sales_nov decimal(10,4) default 0.0,
    sales_dec decimal(10,4) default 0.0,
    cost decimal(10,4) default 0.0,
    cost_jan decimal(10,4) default 0.0,
    cost_feb decimal(10,4) default 0.0,
    cost_mar decimal(10,4) default 0.0,
    cost_apr decimal(10,4) default 0.0,
    cost_may decimal(10,4) default 0.0,
    cost_jun decimal(10,4) default 0.0,
    cost_jul decimal(10,4) default 0.0,
    cost_ago decimal(10,4) default 0.0,
    cost_sep decimal(10,4) default 0.0,
    cost_oct decimal(10,4) default 0.0,
    cost_nov decimal(10,4) default 0.0,
    cost_dec decimal(10,4) default 0.0,
    cost_prev decimal(10,4) default 0.0,
    cost_pct decimal(10,4) default 0.0,
    cost_avg decimal(10,4) default 0.0,
    profit decimal(10,4) default 0.0,
    promotion_sales decimal(10,4) default 0.0,
    promotion_sales_prev decimal(10,4) default 0.0,
    sales_count integer default 0,
    sales_count_prev integer default 0
) WITH OIDS;

-- Database Server on Postgres, DB2 will be created a index automatically with the same name of pk
ALTER TABLE sales_fact_month_store
 ADD CONSTRAINT p_sales_fact_month_store
 PRIMARY KEY (store_id, the_year);

CREATE  INDEX f_sales_fact_month_store ON sales_fact_month_store(store_id);
ALTER TABLE sales_fact_month_store
 ADD CONSTRAINT f_sales_fact_month_store FOREIGN KEY (store_id)
 REFERENCES store(store_id) DEFERRABLE INITIALLY IMMEDIATE ;

-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri Jun 21 16:02:51 CEST 2019 Engine: postgres
-- sales_fact_yearmonth_store
-- **************************************************************************
CREATE TABLE sales_fact_yearmonth_store (
    store_id integer not null,
    the_year integer not null,
    the_month varchar(30) not null,
    month_of_year integer not null,
    unit_sales integer default 0,
    unit_sales_prev integer default 0,
    sales decimal(10,4) default 0.0,
    sales_prev decimal(10,4) default 0.0,
    sales_pct decimal(10,4) default 0.0,
    sales_avg decimal(10,4) default 0.0,
    cost decimal(10,4) default 0.0,
    cost_prev decimal(10,4) default 0.0,
    cost_pct decimal(10,4) default 0.0,
    cost_avg decimal(10,4) default 0.0,
    profit decimal(10,4) default 0.0,
    promotion_sales decimal(10,4) default 0.0,
    promotion_sales_prev decimal(10,4) default 0.0,
    sales_count integer default 0,
    sales_count_prev integer default 0
) WITH OIDS;

-- Database Server on Postgres, DB2 will be created a index automatically with the same name of pk
ALTER TABLE sales_fact_yearmonth_store
 ADD CONSTRAINT p_sales_fact_yearmonth_store
 PRIMARY KEY (store_id, the_year, month_of_year);

CREATE  INDEX f_sales_fact_yearmonth_store ON sales_fact_yearmonth_store(store_id);
ALTER TABLE sales_fact_yearmonth_store
 ADD CONSTRAINT f_sales_fact_yearmonth_store FOREIGN KEY (store_id)
 REFERENCES store(store_id) DEFERRABLE INITIALLY IMMEDIATE ;
Warehouse Class: warehouse_class
Column Type Details Definition
warehouse_class_id integer Primary key. Warehouse class identifier.
description varchar Warehouse class description.

warehouse_class provides description of the warehouses.

Warehouse: warehouse
Column Type Details Definition
warehouse_id integer Primary key. Warehouse identifier.
warehouse_class_id integer References to warehouse_class.warehouse_class_id. Warehouse class identifier.
stores_id integer References to store.store_id Store identifier.
warehouse_name varchar Warehouse name.
wa_address[number] varchar Warehouse address. There are four of them.
Columns: wa_address1, wa_address2...
warehouse_city varchar Warehouse's city.
warehouse_state_province varchar Warehouse's state province.
warehouse_postal_code varchar Warehouse's postal code.
warehouse_country decimal Warehouse's country.
warehouse_owner_name varchar Warehouse owner's name.
warehouse_phone varchar Warehouse's telephone number.
warehouse_fax varchar Warehouse's fax number.

warehouse stores information about the warehouses location and owner

Department: department
Column Type Details Definition
department_id integer Primary key. Department identifier.
department_description varchar Department description.
state char State of the department.

department stores types of departments for the employee table to use.

Position: position
Column Type Details Definition
position_id integer Position identifier.
position_title varchar Position title.
pay_type varchar Payment type.
min_scale decimal Minimum scale.
max_scale decimal Maximum scale.
management_role varchar Management role.

position provides types of positions for the employee table to use.

Employee: employee
Column Type Details Definition
employee_id integer Primary key. Employee identifier.
full_name varchar Employee name.
first_name varchar First name.
last_name varchar Last name.
position_id varchar References to position.position_id Position identifier.
position_title varchar Position title.
stores_id integer References to store.store_id Store identifier.
department_id integer References to department.department_id Department identifier.
birth_date date Employee's birth date.
hire_date datetime Employee hiring date.
end_date datetime Employee end of contract date.
salary decimal Employee's salary.
supervisor_id integer References to employee.employee_id Supervisor identifier.
education_level varchar Employee's education level.
marital_status varchar Employees's marital status.
gender varchar Employee's gender.
management_role varchar Management role.
state char State of employee.

employee stores information about employees and their supervisors.

Store: store
Column Type Details Definition
store_id serial Primary key. Store identifier.
store_type varchar Type of store.
store_name varchar Store name.
store_number integer Store number.
store_street_address varchar Store's address.
store_city varchar Store's city.
store_state varchar Store's state province.
store_postal_code varchar Store's postal code.
store_country varchar Store's country.
store_manager varchar Store's manager.
store_phone varchar Store's telephone number.
store_fax varchar Store's fax number.
first_opened_date varchar Date store's first opening.
store_sqft integer Store squared feet.
grocery_sqft integer Groceries section squared feet.
frozen_sqft integer Freezers section squared feet.
meat_sqft integer Meat section squared feet.
coffee_bar boolean Whether there's coffee bar or not.
video_store boolean Whether there's video store or not.
salad_bar boolean Whether there's salad bar or not.
prepared_food boolean Whether there's prepared food or not.
florist boolean Whether there's florist or not.
state char State of store.

store provides information about the stores locations and other facts.

Store Sales by Year: sales_fact_month_store
Column Type Details Definition
store_id integer Primary key.
References store.store_id.
Store identifier.
the_year integer Primary key. Year.
unit_sales integer Units sold of the store.
unit_sales_prev integer Units sold of the store the previous year.
sales decimal Store sales.
sales_prev decimal Store sales the previous year.
sales_pct decimal Store sales percentage.
sales_avg decimal Store sales month average.
sales_[mon] decimal Store sales per month of year.
Example: sales_jan, sales_feb...
cost decimal Store cost.
cost_prev decimal Store cost the previous year.
cost_pct decimal Store cost percentage.
cost_avg decimal Store cost month average.
cost_[mon] decimal Store cost per month of year.
Example: cost_jan, cost_feb...
profit decimal Store profit = sales - cost.
promotion_sales decimal Store sales when promotion is applied.
promotion_sales_prev decimal Store sales when promotion is applied of previous year.
sales_count integer Sales counter.

sales_fact_month_store stores yearly information about stores and also monthly sales and costs.

Store Sales by Month: sales_fact_yearmonth_store
Column Type Details Definition
store_id integer Primary key.
References store.store_id.
Store identifier.
the_year integer Primary key. Year.
the_month varchar Month's name.
month_of_year integer Primary key. Month's number.
unit_sales integer Units sold of the store.
unit_sales_prev integer Units sold of the store the previous month.
sales decimal Store sales.
sales_prev decimal Store sales the previous month.
sales_pct decimal Store sales percentage.
sales_avg decimal Store sales day average.
cost decimal Store cost.
cost_prev decimal Store cost the previous month.
cost_pct decimal Store cost percentage.
cost_avg decimal Store cost day average.
profit decimal Store profit = sales - cost.
promotion_sales decimal Store sales when promotion is applied.
promotion_sales_prev decimal Store sales when promotion is applied of previous month.
sales_count integer Sales counter.

sales_fact_yearmonth_store provides monthly information about stores.

2.6 Other tables

In this section all the tables associated with store will be placed here.

<table name='district'>
    
    <column name='district_id'      type='serial'                   />
    <column name='district'         type='varchar'      size='30'   />
    <column name='state_province'   type='varchar'      size='30'   />
    <column name='country'          type='varchar'      size='30'   />
    
    <primary name='p_district'  columns='district_id'               />
	
</table>
<table name='region'>
        
    <!-- COLUMNS -->
    <column name='region_id'                    type='integer'                  required='y'/>        
    <column name='sales_city'                   type='varchar'      size='30'               />
    <column name='sales_state_province'         type='varchar'      size='30'               />
    <column name='sales_district'               type='varchar'      size='30'               />
    <column name='sales_region'                 type='varchar'      size='30'               />
    <column name='sales_country'                type='varchar'      size='30'               />
    <column name='sales_district_id'            type='integer'                              />
    
    <!-- INDEXES -->
    <primary name='p_region'        columns='region_id'                                     />
    
    <!-- FOREIGN KEY --> 
    <foreign name='f_region_1'      columns='sales_district_id'     references='district'   refcols='district_id'/>
    
</table>
Copy
-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri Jun 21 16:45:56 CEST 2019 Engine: informix
-- district
-- **************************************************************************
CREATE TABLE district (
    district_id serial,
    district varchar(30),
    state_province varchar(30),
    country varchar(30)
);
ALTER TABLE district LOCK MODE (ROW);

-- Index to avoid automatic named by database (named with pk name).
CREATE UNIQUE INDEX p_district ON district(district_id);
ALTER TABLE district
 ADD CONSTRAINT 
 PRIMARY KEY (district_id)
 CONSTRAINT p_district;

-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri Jun 21 17:03:30 CEST 2019 Engine: informix
-- region
-- **************************************************************************
CREATE TABLE region (
    region_id integer not null,
    sales_city varchar(30),
    sales_state_province varchar(30),
    sales_district varchar(30),
    sales_region varchar(30),
    sales_country varchar(30),
    sales_district_id integer
);
ALTER TABLE region LOCK MODE (ROW);

-- Index to avoid automatic named by database (named with pk name).
CREATE UNIQUE INDEX p_region ON region(region_id);
ALTER TABLE region
 ADD CONSTRAINT 
 PRIMARY KEY (region_id)
 CONSTRAINT p_region;

CREATE  INDEX f_region_1 ON region(sales_district_id);
ALTER TABLE region
 ADD CONSTRAINT  FOREIGN KEY (sales_district_id)
 REFERENCES district(district_id)
 CONSTRAINT f_region_1;
Copy
-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri Jun 21 16:46:03 CEST 2019 Engine: postgres
-- district
-- **************************************************************************
CREATE TABLE district (
    district_id serial,
    district varchar(30),
    state_province varchar(30),
    country varchar(30)
) WITH OIDS;

-- Database Server on Postgres, DB2 will be created a index automatically with the same name of pk
ALTER TABLE district
 ADD CONSTRAINT p_district
 PRIMARY KEY (district_id);

-- **************************************************************************
-- DEISTER WebStudio XSQL-SCHEMA Fri Jun 21 17:03:41 CEST 2019 Engine: postgres
-- region
-- **************************************************************************
CREATE TABLE region (
    region_id integer not null,
    sales_city varchar(30),
    sales_state_province varchar(30),
    sales_district varchar(30),
    sales_region varchar(30),
    sales_country varchar(30),
    sales_district_id integer
) WITH OIDS;

-- Database Server on Postgres, DB2 will be created a index automatically with the same name of pk
ALTER TABLE region
 ADD CONSTRAINT p_region
 PRIMARY KEY (region_id);

CREATE  INDEX f_region_1 ON region(sales_district_id);
ALTER TABLE region
 ADD CONSTRAINT f_region_1 FOREIGN KEY (sales_district_id)
 REFERENCES district(district_id) DEFERRABLE INITIALLY IMMEDIATE ;
Districts: district
Column Type Details Definition
district_id serial Primary key. District identifier.
district varchar District name.
state_province varchar State province.
country varchar Country.

district stores location information about the districts.

Regions: region
Column Type Details Definition
region_id serial Primary key. Region identifier.
sales_city varchar City.
sales_state_province varchar State province.
sales_district varchar District.
sales_region varchar Sales region.
sales_country varchar Sales country.
sales_district_id integer References to district.district_id. District identifier.

region stores location information about the regions.

3 Triggers and procedures

The table sales_fact has three triggers to manage inserts, updates and deletes into the table and also three specific procedures that will help fill the tables sales_fact_month_customer, sales_fact_month_product, sales_fact_month_store and sales_fact_yearmonth_customer, sales_fact_yearmonth_product and sales_fact_yearmonth_store.

3.1 Insert

3.1.1 Trigger

When a new entry is inserted to sales_fact; sales_fact_month_customer, sales_fact_month_product and sales_fact_month_store update or insert their corresponding entries as well as sales_fact_yearmonth_customer, sales_fact_yearmonth_product and sales_fact_yearmonth_store.

<xsql-trigger
    name='sales_fact_ins'
    event='insert'
    table='sales_fact'
>
    <foreach-row>
        <execute-procedure name='sales_fact_act_ins'>
            <in>
                <param><nxt>product_id</nxt></param>
                <param><nxt>time_id</nxt></param>
                <param><nxt>customer_id</nxt></param>
                <param><nxt>promotion_id</nxt></param>
                <param><nxt>store_id</nxt></param>
                <param><nxt>store_sales</nxt></param>
                <param><nxt>store_cost</nxt></param>
                <param><nxt>unit_sales</nxt></param>
            </in>	
        </execute-procedure>	
    </foreach-row>
</xsql-trigger>
-- **************************************************************************
-- sales_fact_ins
-- DEISTER WebStudio XSQL-TRIGGER Wed Nov 25 22:41:31 CET 2020 Engine: ids
-- **************************************************************************

3.1.2 Procedure

The procedure will update already existing rows on sales_fact_month_* and sales_fact_yearmonth_* or insert new rows. As the tables have columns for previous months or years, when inserting it will search for a previous year or month to get the values it needs. If found, it will fill in those fields, if not a 0 will be placed in them. The *_prev columns for the following year or month are also updated (if existing) with the corresponding values of the current year.

<procedure name='sales_fact_act_ins'>
   	
    <args>
        <arg name='p_product_id'                    like='sales_fact.product_id'                            />
        <arg name='p_time_id'                       like='sales_fact.time_id'                               />
        <arg name='p_customer_id'                   like='sales_fact.customer_id'                           />
        <arg name='p_promotion_id'                  like='sales_fact.promotion_id'                          />
        <arg name='p_store_id'                      like='sales_fact.store_id'                              />
        <arg name='p_store_sales'                   like='sales_fact.store_sales'                           />
        <arg name='p_store_cost'                    like='sales_fact.store_cost'                            />
        <arg name='p_unit_sales'                    like='sales_fact.unit_sales'                            />
    </args>
    
    <define>
        <variable name='m_month_of_year'            like='time_by_day.month_of_year'                        />
        <variable name='m_month_of_year_prev'       like='time_by_day.month_of_year'                        />
        <variable name='m_the_month'                like='time_by_day.the_month'                            />
        <variable name='m_the_month_prev'           like='time_by_day.the_month'                            />
        <variable name='m_the_year'                 like='time_by_day.the_year'                             />
        <variable name='m_the_year_prev'            like='time_by_day.the_year'                             />
        <variable name='m_unit_sales_prev'          like='sales_fact_month_customer.unit_sales_prev'        />
        <variable name='m_sales_prev'               like='sales_fact_month_customer.sales_prev'             />
        <variable name='m_cost_prev'                like='sales_fact_month_customer.cost_prev'              />
        <variable name='m_promotion_sales_prev'     like='sales_fact_month_customer.promotion_sales_prev'   />
        <variable name='m_sales_count_prev'         like='sales_fact_month_customer.sales_count_prev'       />
        
        <variable name='m_count'                    type='integer'                                          />
        <variable name='m_promotion_cost'           like='sales_fact_month_product.promotion_sales'         />
    </define>

	<body>
   		
   		<!-- If promotion is applied, sales are added -->
        <if>
            <expr>
                p_promotion_id &gt; 0
            </expr>
            <then>
                <set name='m_promotion_cost'>p_store_sales</set>
            </then>
            <else>
                <set name='m_promotion_cost'>0.0</set>
            </else>
        </if>
   		
   	    <!-- Gets month, month number and year -->
        <select>
            <columns>
                month_of_year, the_month, the_year
            </columns>
            <into>m_month_of_year, m_the_month, m_the_year</into>
            FROM time_by_day
            WHERE time_id = p_time_id
        </select>

        <!--    CUSTOMER     -->
        
        <!-- Counter for number of entries in sales_fact_month_* for previous year, if greater than 0 will set previous year data into variables, if 0 will insert 0 -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_month_customer
            WHERE
                customer_id = p_customer_id     AND
                the_year = m_the_year - 1
        </select>
        
        <!-- Selects unit_sales-prev, sales_prev, cost_prev, promotion_sales_prev and sales_count_prev for actual year -->
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                <select>
                    <columns>
                        unit_sales, sales, cost, promotion_sales, sales_count
                    </columns>
                    <into>m_unit_sales_prev, m_sales_prev, m_cost_prev, m_promotion_sales_prev, m_sales_count_prev</into>
                    FROM sales_fact_month_customer
                    WHERE 
                        customer_id = p_customer_id     AND
                        the_year = m_the_year - 1
                </select>
            </then>
            <else>
                <set name='m_unit_sales_prev'>0</set>
                <set name='m_sales_prev'>0.0</set>
                <set name='m_cost_prev'>0.0</set>
                <set name='m_promotion_sales_prev'>0.0</set>
                <set name='m_sales_count_prev'>0</set>
            </else>
        </if>

        <!-- Counter for number of entries in sales_fact_month_* for actual year, if 0 will insert, if greater will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_month_customer
            WHERE
                customer_id = p_customer_id     AND
                the_year = m_the_year
        </select>

        <!-- Insert or Update year on sales_fact_month_customer -->
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                
                <if>
                    <expr>
                        m_month_of_year == 1
                    </expr>
                    <then>
                        
                        <update table='sales_fact_month_customer'>
                            <column name='unit_sales'>unit_sales + p_unit_sales</column>
                            <column name='sales'>sales + p_store_sales</column>
                            <column name='sales_jan'>sales_jan + p_store_sales</column>
                            <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                            <column name='cost'>cost + p_store_cost</column>
                            <column name='cost_jan'>cost_jan + p_store_cost</column>
                            <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                            <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                            <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                            <column name='sales_count'>sales_count + 1</column>
                            <where>
                                customer_id = p_customer_id     AND
                                the_year = m_the_year
                            </where>
                        </update>
    
                    </then>
                    <else-if>
                        <expr>
                            m_month_of_year == 2
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales + p_unit_sales</column>
                                <column name='sales'>sales + p_store_sales</column>
                                <column name='sales_feb'>sales_feb + p_store_sales</column>
                                <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                                <column name='cost'>cost + p_store_cost</column>
                                <column name='cost_feb'>cost_feb + p_store_cost</column>
                                <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                                <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    customer_id = p_customer_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 3
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales + p_unit_sales</column>
                                <column name='sales'>sales + p_store_sales</column>
                                <column name='sales_mar'>sales_mar + p_store_sales</column>
                                <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                                <column name='cost'>cost + p_store_cost</column>
                                <column name='cost_mar'>cost_mar + p_store_cost</column>
                                <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                                <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    customer_id = p_customer_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 4
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales + p_unit_sales</column>
                                <column name='sales'>sales + p_store_sales</column>
                                <column name='sales_apr'>sales_apr + p_store_sales</column>
                                <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                                <column name='cost'>cost + p_store_cost</column>
                                <column name='cost_apr'>cost_apr + p_store_cost</column>
                                <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                                <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    customer_id = p_customer_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 5
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales + p_unit_sales</column>
                                <column name='sales'>sales + p_store_sales</column>
                                <column name='sales_may'>sales_may + p_store_sales</column>
                                <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                                <column name='cost'>cost + p_store_cost</column>
                                <column name='cost_may'>cost_may + p_store_cost</column>
                                <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                                <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    customer_id = p_customer_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 6
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales + p_unit_sales</column>
                                <column name='sales'>sales + p_store_sales</column>
                                <column name='sales_jun'>sales_jun + p_store_sales</column>
                                <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                                <column name='cost'>cost + p_store_cost</column>
                                <column name='cost_jun'>cost_jun + p_store_cost</column>
                                <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                                <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    customer_id = p_customer_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 7
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales + p_unit_sales</column>
                                <column name='sales'>sales + p_store_sales</column>
                                <column name='sales_jul'>sales_jul + p_store_sales</column>
                                <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                                <column name='cost'>cost + p_store_cost</column>
                                <column name='cost_jul'>cost_jul + p_store_cost</column>
                                <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                                <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    customer_id = p_customer_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 8
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales + p_unit_sales</column>
                                <column name='sales'>sales + p_store_sales</column>
                                <column name='sales_ago'>sales_ago + p_store_sales</column>
                                <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                                <column name='cost'>cost + p_store_cost</column>
                                <column name='cost_ago'>cost_ago + p_store_cost</column>
                                <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                                <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    customer_id = p_customer_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 9
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales + p_unit_sales</column>
                                <column name='sales'>sales + p_store_sales</column>
                                <column name='sales_sep'>sales_sep + p_store_sales</column>
                                <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                                <column name='cost'>cost + p_store_cost</column>
                                <column name='cost_sep'>cost_sep + p_store_cost</column>
                                <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                                <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    customer_id = p_customer_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 10
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales + p_unit_sales</column>
                                <column name='sales'>sales + p_store_sales</column>
                                <column name='sales_oct'>sales_oct + p_store_sales</column>
                                <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                                <column name='cost'>cost + p_store_cost</column>
                                <column name='cost_oct'>cost_oct + p_store_cost</column>
                                <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                                <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    customer_id = p_customer_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 11
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales + p_unit_sales</column>
                                <column name='sales'>sales + p_store_sales</column>
                                <column name='sales_nov'>sales_nov + p_store_sales</column>
                                <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                                <column name='cost'>cost + p_store_cost</column>
                                <column name='cost_nov'>cost_nov + p_store_cost</column>
                                <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                                <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    customer_id = p_customer_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 12
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales + p_unit_sales</column>
                                <column name='sales'>sales + p_store_sales</column>
                                <column name='sales_dec'>sales_dec + p_store_sales</column>
                                <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                                <column name='cost'>cost + p_store_cost</column>
                                <column name='cost_dec'>cost_dec + p_store_cost</column>
                                <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                                <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    customer_id = p_customer_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                </if>
                
            </then>
            <else>
                <if>
                    <expr>
                        m_month_of_year == 1
                    </expr>
                    <then>
                    
                        <insert table='sales_fact_month_customer'>
                            <column name='customer_id'>p_customer_id</column>
                            <column name='the_year'>m_the_year</column>
                            <column name='unit_sales'>p_unit_sales</column>
                            <column name='unit_sales_prev'>m_unit_sales_prev</column>
                            <column name='sales'>p_store_sales</column>
                            <column name='sales_prev'>m_sales_prev</column>
                            <column name='sales_jan'>p_store_sales</column>
                            <column name='sales_avg'>p_store_cost/12.0</column>
                            <column name='cost'>p_store_cost</column>
                            <column name='cost_jan'>p_store_cost</column>
                            <column name='cost_prev'>m_cost_prev</column>
                            <column name='cost_avg'>p_store_cost/12.0</column>
                            <column name='profit'>p_store_sales - p_store_cost</column>
                            <column name='promotion_sales'>m_promotion_cost</column>
                            <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                            <column name='sales_count'>1</column>
                            <column name='sales_count_prev'>m_sales_count_prev</column>
                        </insert>

                    </then>
                    <else-if>
                        <expr>
                            m_month_of_year == 2
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_customer'>
                                <column name='customer_id'>p_customer_id</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_feb'>p_store_sales</column>
                                <column name='sales_avg'>p_store_cost/12.0</column>
                                <column name='cost'>p_store_cost</column>
                                <column name='cost_feb'>p_store_cost</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost/12.0</column>
                                <column name='profit'>p_store_sales - p_store_cost</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 3
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_customer'>
                                <column name='customer_id'>p_customer_id</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_mar'>p_store_sales</column>
                                <column name='sales_avg'>p_store_cost/12.0</column>
                                <column name='cost'>p_store_cost</column>
                                <column name='cost_mar'>p_store_cost</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost/12.0</column>
                                <column name='profit'>p_store_sales - p_store_cost</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 4
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_customer'>
                                <column name='customer_id'>p_customer_id</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_apr'>p_store_sales</column>
                                <column name='sales_avg'>p_store_cost/12.0</column>
                                <column name='cost'>p_store_cost</column>
                                <column name='cost_apr'>p_store_cost</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost/12.0</column>
                                <column name='profit'>p_store_sales - p_store_cost</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 5
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_customer'>
                                <column name='customer_id'>p_customer_id</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_may'>p_store_sales</column>
                                <column name='sales_avg'>p_store_cost/12.0</column>
                                <column name='cost'>p_store_cost</column>
                                <column name='cost_may'>p_store_cost</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost/12.0</column>
                                <column name='profit'>p_store_sales - p_store_cost</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 6
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_customer'>
                                <column name='customer_id'>p_customer_id</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_jun'>p_store_sales</column>
                                <column name='sales_avg'>p_store_cost/12.0</column>
                                <column name='cost'>p_store_cost</column>
                                <column name='cost_jun'>p_store_cost</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost/12.0</column>
                                <column name='profit'>p_store_sales - p_store_cost</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 7
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_customer'>
                                <column name='customer_id'>p_customer_id</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_jul'>p_store_sales</column>
                                <column name='sales_avg'>p_store_cost/12.0</column>
                                <column name='cost'>p_store_cost</column>
                                <column name='cost_jul'>p_store_cost</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost/12.0</column>
                                <column name='profit'>p_store_sales - p_store_cost</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 8
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_customer'>
                                <column name='customer_id'>p_customer_id</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_ago'>p_store_sales</column>
                                <column name='sales_avg'>p_store_cost/12.0</column>
                                <column name='cost'>p_store_cost</column>
                                <column name='cost_ago'>p_store_cost</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost/12.0</column>
                                <column name='profit'>p_store_sales - p_store_cost</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 9
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_customer'>
                                <column name='customer_id'>p_customer_id</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_sep'>p_store_sales</column>
                                <column name='sales_avg'>p_store_cost/12.0</column>
                                <column name='cost'>p_store_cost</column>
                                <column name='cost_sep'>p_store_cost</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost/12.0</column>
                                <column name='profit'>p_store_sales - p_store_cost</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 10
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_customer'>
                                <column name='customer_id'>p_customer_id</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_oct'>p_store_sales</column>
                                <column name='sales_avg'>p_store_cost/12.0</column>
                                <column name='cost'>p_store_cost</column>
                                <column name='cost_oct'>p_store_cost</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost/12.0</column>
                                <column name='profit'>p_store_sales - p_store_cost</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 11
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_customer'>
                                <column name='customer_id'>p_customer_id</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_nov'>p_store_sales</column>
                                <column name='sales_avg'>p_store_cost/12.0</column>
                                <column name='cost'>p_store_cost</column>
                                <column name='cost_nov'>p_store_cost</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost/12.0</column>
                                <column name='profit'>p_store_sales - p_store_cost</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 12
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_customer'>
                                <column name='customer_id'>p_customer_id</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_dec'>p_store_sales</column>
                                <column name='sales_avg'>p_store_cost/12.0</column>
                                <column name='cost'>p_store_cost</column>
                                <column name='cost_dec'>p_store_cost</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost/12.0</column>
                                <column name='profit'>p_store_sales - p_store_cost</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                </if>

            </else>
        </if>

        <!-- Updates percentage in sales_fact_month_* for actual year-->
        <if>
            <expr>
                m_sales_prev != 0 AND m_cost_prev != 0
            </expr>
            <then>
                <update table='sales_fact_month_customer'>
                    <column name='sales_pct'>ROUND(((((sales)/(sales_prev))-1)*100),2)</column>
                    <column name='cost_pct'>ROUND(((((cost)/(cost_prev))-1)*100),2)</column>
                    <where>
                        customer_id = p_customer_id    AND
                        the_year = m_the_year
                    </where>
                </update>
            </then>
            <else-if>
                <expr>
                    m_sales_prev == 0 AND m_cost_prev != 0
                </expr>
                <then>
                    <update table='sales_fact_month_customer'>
                        <column name='sales_pct'>100</column>
                        <column name='cost_pct'>ROUND(((((cost)/(cost_prev))-1)*100),2)</column>
                        <where>
                            customer_id = p_customer_id    AND
                            the_year = m_the_year
                        </where>
                    </update>
                </then>
            </else-if>
            <else-if>
                <expr>
                    m_sales_prev != 0 AND m_cost_prev == 0
                </expr>
                <then>
                    <update table='sales_fact_month_customer'>
                        <column name='sales_pct'>ROUND(((((sales)/(sales_prev))-1)*100),2)</column>
                        <column name='cost_pct'>100</column>
                        <where>
                            customer_id = p_customer_id    AND
                            the_year = m_the_year
                        </where>
                    </update>
                </then>
            </else-if>
            <else-if>
                <expr>
                    m_sales_prev == 0 AND m_cost_prev == 0
                </expr>
                <then>
                    <update table='sales_fact_month_customer'>
                        <column name='sales_pct'>100</column>
                        <column name='cost_pct'>100</column>
                        <where>
                            customer_id = p_customer_id    AND
                            the_year = m_the_year
                        </where>
                    </update>
                </then>
            </else-if>
        </if>

        <!-- Counter for number of entries in sales_fact_month_* for year + 1, if 0 will insert, if greater will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_month_customer
            WHERE
                customer_id = p_customer_id     AND
                the_year = m_the_year + 1
        </select>

        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                <!-- Selects sales_prev and cost_prev to choose the percentage -->
                <select>
                    <columns>
                        sales, cost
                    </columns>
                    <into>m_sales_prev, m_cost_prev</into>
                    FROM sales_fact_month_customer
                    WHERE 
                        customer_id = p_customer_id       AND
                        the_year = m_the_year
                </select>
                
                <!-- Updates percentage in actual year -->
                <if>
                    <expr>
                        m_sales_prev != 0 AND m_cost_prev != 0
                    </expr>
                    <then>
                        <update table='sales_fact_month_customer'>
                            <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales</column>
                            <column name='sales_prev'>sales_prev + p_store_sales</column>
                            <column name='cost_prev'>cost_prev + p_store_cost</column>
                            <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                            <column name='sales_count_prev'>sales_count_prev + 1</column>
                            <column name='sales_pct'>ROUND(((((sales)/(sales_prev + p_store_sales))+1)*100),2)</column>
                            <column name='cost_pct'>ROUND(((((cost)/(cost_prev + p_store_cost))-1)*100),2)</column>
                            <where>
                                customer_id = p_customer_id    AND
                                the_year = m_the_year + 1
                            </where>
                        </update>
                    </then>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev != 0
                        </expr>
                        <then>
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales</column>
                                <column name='sales_prev'>sales_prev + p_store_sales</column>
                                <column name='cost_prev'>cost_prev + p_store_cost</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev + 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>ROUND(((((cost)/(cost_prev + p_store_cost))-1)*100),2)</column>
                                <where>
                                    customer_id = p_customer_id    AND
                                    the_year = m_the_year + 1
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev != 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales</column>
                                <column name='sales_prev'>sales_prev + p_store_sales</column>
                                <column name='cost_prev'>cost_prev + p_store_cost</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev + 1</column>
                                <column name='sales_pct'>ROUND(((((sales)/(sales_prev + p_store_sales))-1)*100),2)</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    customer_id = p_customer_id    AND
                                    the_year = m_the_year + 1
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales</column>
                                <column name='sales_prev'>sales_prev + p_store_sales</column>
                                <column name='cost_prev'>cost_prev + p_store_cost</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev + 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    customer_id = p_customer_id    AND
                                    the_year = m_the_year + 1
                                </where>
                            </update>
                        </then>
                    </else-if>
                </if>
            </then>
        </if>

        <!--         CUSTOMER SALES_FACT_YEARMONTH        -->

        <!-- Month - 1 [Year - 1] -->
        <if>
            <expr>
                m_month_of_year == 1
            </expr>
            <then>
                <set name='m_month_of_year_prev'>12</set>
                <set name='m_the_year_prev'>m_the_year - 1</set>
            </then>
            <else>
                <set name='m_month_of_year_prev'>m_month_of_year - 1</set>
                <set name='m_the_year_prev'>m_the_year</set>
            </else>
        </if>

        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_yearmonth_customer
            WHERE
                customer_id = p_customer_id     AND
                the_year = m_the_year_prev AND
                month_of_year = m_month_of_year_prev
        </select>
        
        <!-- Selects unit_sales-prev, sales_prev, cost_prev, promotion_sales_prev and sales_count_prev for actual year -->
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                <select>
                    <columns>
                        unit_sales, sales, cost, promotion_sales, sales_count
                    </columns>
                    <into>m_unit_sales_prev, m_sales_prev, m_cost_prev, m_promotion_sales_prev, m_sales_count_prev</into>
                    FROM sales_fact_yearmonth_customer
                    WHERE 
                        customer_id = p_customer_id     AND
                        the_year = m_the_year_prev AND
                        month_of_year = m_month_of_year_prev
                </select>
            </then>
            <else>
                <set name='m_unit_sales_prev'>0</set>
                <set name='m_sales_prev'>0.0</set>
                <set name='m_cost_prev'>0.0</set>
                <set name='m_promotion_sales_prev'>0.0</set>
                <set name='m_sales_count_prev'>0</set>
            </else>
        </if>

        <!-- Counter for number of entries in sales_fact_yearmonth_* for actual year, if 0 will insert, if greater will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_yearmonth_customer
            WHERE
                customer_id = p_customer_id     AND
                the_year = m_the_year AND
                month_of_year = m_month_of_year
        </select>
        
        <!-- Insert or Update on sales_fact_yearmonth_customer -->
        <if>
            <expr>
               m_count &gt; 0
            </expr>
            <then>
                <update table='sales_fact_yearmonth_customer'>
                    <column name='unit_sales'>unit_sales + p_unit_sales</column>
                    <column name='sales'>sales + p_store_sales</column>
                    <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                    <column name='cost'>cost + p_store_cost</column>
                    <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                    <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                    <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                    <column name='sales_count'>sales_count + 1</column>
                    <where>
                        customer_id = p_customer_id     AND
                        the_year = m_the_year AND
                        month_of_year = m_month_of_year
                    </where>
                </update>
            </then>
            <else>
                <insert table='sales_fact_yearmonth_customer'>
                    <column name='customer_id'>p_customer_id</column>
                    <column name='the_year'>m_the_year</column>
                    <column name='the_month'>m_the_month</column>
                    <column name='month_of_year'>m_month_of_year</column>
                    <column name='unit_sales'>p_unit_sales</column>
                    <column name='unit_sales_prev'>m_unit_sales_prev</column>
                    <column name='sales'>p_store_sales</column>
                    <column name='sales_prev'>m_sales_prev</column>
                    <column name='sales_avg'>p_store_cost/12.0</column>
                    <column name='cost'>p_store_cost</column>
                    <column name='cost_prev'>m_cost_prev</column>
                    <column name='cost_avg'>p_store_cost/12.0</column>
                    <column name='profit'>p_store_sales - p_store_cost</column>
                    <column name='promotion_sales'>m_promotion_cost</column>
                    <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                    <column name='sales_count'>1</column>
                    <column name='sales_count_prev'>m_sales_count_prev</column>
                </insert>
            </else>
        </if>

        <if>
            <expr>
                m_sales_prev != 0 AND m_cost_prev != 0
            </expr>
            <then>
                <update table='sales_fact_yearmonth_customer'>
                    <column name='sales_pct'>ROUND(((((sales)/(sales_prev))-1)*100),2)</column>
                    <column name='cost_pct'>ROUND(((((cost)/(cost_prev))-1)*100),2)</column>
                    <where>
                        customer_id = p_customer_id     AND
                        the_year = m_the_year AND
                        month_of_year = m_month_of_year
                    </where>
                </update>
            </then>
            <else-if>
                <expr>
                    m_sales_prev == 0 AND m_cost_prev != 0
                </expr>
                <then>
                    <update table='sales_fact_yearmonth_customer'>
                        <column name='sales_pct'>100</column>
                        <column name='cost_pct'>ROUND(((((cost)/(cost_prev))-1)*100),2)</column>
                        <where>
                            customer_id = p_customer_id     AND
                            the_year = m_the_year AND
                            month_of_year = m_month_of_year
                        </where>
                    </update>
                </then>
            </else-if>
            <else-if>
                <expr>
                    m_sales_prev != 0 AND m_cost_prev == 0
                </expr>
                <then>
                    <update table='sales_fact_yearmonth_customer'>
                        <column name='sales_pct'>ROUND(((((sales)/(sales_prev))-1)*100),2)</column>
                        <column name='cost_pct'>100</column>
                        <where>
                            customer_id = p_customer_id     AND
                            the_year = m_the_year AND
                            month_of_year = m_month_of_year
                        </where>
                    </update>
                </then>
            </else-if>
            <else-if>
                <expr>
                    m_sales_prev == 0 AND m_cost_prev == 0
                </expr>
                <then>
                    <update table='sales_fact_yearmonth_customer'>
                        <column name='sales_pct'>100</column>
                        <column name='cost_pct'>100</column>
                        <where>
                            customer_id = p_customer_id     AND
                            the_year = m_the_year AND
                            month_of_year = m_month_of_year
                        </where>
                    </update>
                </then>
            </else-if>
        </if>

        <!-- Month + 1 [Year + 1] -->
        <if>
            <expr>
                m_month_of_year == 12
            </expr>
            <then>
                <set name='m_month_of_year_prev'>1</set>
                <set name='m_the_year_prev'>m_the_year + 1</set>
            </then>
            <else>
                <set name='m_month_of_year_prev'>m_month_of_year + 1</set>
                <set name='m_the_year_prev'>m_the_year</set>
            </else>
        </if>

        <!-- Counter for number of entries in sales_fact_yearmonth_* for previous year, if greater than 0 will set previous year data into variables, if 0 will insert 0 -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_yearmonth_customer
            WHERE
                customer_id = p_customer_id     AND
                the_year = m_the_year_prev AND
                month_of_year = m_month_of_year_prev
        </select>
        
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>

                <!-- Select sales_prev and cost_prev from month +1 [and year +1] -->
                <select>
                    <columns>
                        sales, cost
                    </columns>
                    <into>m_sales_prev, m_cost_prev</into>
                    FROM sales_fact_yearmonth_customer
                    WHERE 
                        customer_id = p_customer_id       AND
                        the_year = m_the_year AND
                        month_of_year = m_month_of_year
                </select>
                
                <!-- Updates sales_fact_year_month_* for month + 1 [and year + 1] with percentages -->
                <if>
                    <expr>
                        m_sales_prev != 0 AND m_cost_prev != 0
                    </expr>
                    <then>
                        <update table='sales_fact_yearmonth_customer'>
                            <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales</column>
                            <column name='sales_prev'>sales_prev + p_store_sales</column>
                            <column name='cost_prev'>cost_prev + p_store_cost</column>
                            <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                            <column name='sales_count_prev'>sales_count_prev + 1</column>
                            <column name='sales_pct'>ROUND(((((sales)/(sales_prev + p_store_sales))-1)*100),2)</column>
                            <column name='cost_pct'>ROUND(((((cost)/(cost_prev + p_store_cost))-1)*100),2)</column>
                            <where>
                                customer_id = p_customer_id    AND
                                the_year = m_the_year_prev AND
                                month_of_year = m_month_of_year_prev
                            </where>
                        </update>
                    </then>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev != 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_customer'>
                                <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales</column>
                                <column name='sales_prev'>sales_prev + p_store_sales</column>
                                <column name='cost_prev'>cost_prev + p_store_cost</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev + 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>ROUND(((((cost)/(cost_prev + p_store_cost))-1)*100),2)</column>
                                <where>
                                    customer_id = p_customer_id    AND
                                    the_year = m_the_year_prev AND
                                    month_of_year = m_month_of_year_prev
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev != 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_customer'>
                                <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales</column>
                                <column name='sales_prev'>sales_prev + p_store_sales</column>
                                <column name='cost_prev'>cost_prev + p_store_cost</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev + 1</column>
                                <column name='sales_pct'>ROUND(((((sales)/(sales_prev + p_store_sales))-1)*100),2)</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    customer_id = p_customer_id    AND
                                    the_year = m_the_year_prev AND
                                    month_of_year = m_month_of_year_prev
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_customer'>
                                <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales</column>
                                <column name='sales_prev'>sales_prev + p_store_sales</column>
                                <column name='cost_prev'>cost_prev + p_store_cost</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev + 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    customer_id = p_customer_id    AND
                                    the_year = m_the_year_prev AND
                                    month_of_year = m_month_of_year_prev
                                </where>
                            </update>
                        </then>
                    </else-if>
                </if>
            </then>
        </if>
        
        <!--    store     -->
        
        <!-- Counter for number of entries in sales_fact_month_* for previous year, if greater than 0 will set previous year data into variables, if 0 will insert 0 -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_month_store
            WHERE
                store_id = p_store_id     AND
                the_year = m_the_year - 1
        </select>
        
        <!-- Selects unit_sales-prev, sales_prev, cost_prev, promotion_sales_prev and sales_count_prev for actual year -->
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                <select>
                    <columns>
                        unit_sales, sales, cost, promotion_sales, sales_count
                    </columns>
                    <into>m_unit_sales_prev, m_sales_prev, m_cost_prev, m_promotion_sales_prev, m_sales_count_prev</into>
                    FROM sales_fact_month_store
                    WHERE 
                        store_id = p_store_id     AND
                        the_year = m_the_year - 1
                </select>
            </then>
            <else>
                <set name='m_unit_sales_prev'>0</set>
                <set name='m_sales_prev'>0.0</set>
                <set name='m_cost_prev'>0.0</set>
                <set name='m_promotion_sales_prev'>0.0</set>
                <set name='m_sales_count_prev'>0</set>
            </else>
        </if>

        <!-- Counter for number of entries in sales_fact_month_* for actual year, if 0 will insert, if greater will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_month_store
            WHERE
                store_id = p_store_id     AND
                the_year = m_the_year
        </select>

        <!-- Insert or Update year on sales_fact_month_store -->
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                
                <if>
                    <expr>
                        m_month_of_year == 1
                    </expr>
                    <then>
                        
                        <update table='sales_fact_month_store'>
                            <column name='unit_sales'>unit_sales + p_unit_sales</column>
                            <column name='sales'>sales + p_store_sales</column>
                            <column name='sales_jan'>sales_jan + p_store_sales</column>
                            <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                            <column name='cost'>cost + p_store_cost</column>
                            <column name='cost_jan'>cost_jan + p_store_cost</column>
                            <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                            <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                            <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                            <column name='sales_count'>sales_count + 1</column>
                            <where>
                                store_id = p_store_id     AND
                                the_year = m_the_year
                            </where>
                        </update>
    
                    </then>
                    <else-if>
                        <expr>
                            m_month_of_year == 2
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales + p_unit_sales</column>
                                <column name='sales'>sales + p_store_sales</column>
                                <column name='sales_feb'>sales_feb + p_store_sales</column>
                                <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                                <column name='cost'>cost + p_store_cost</column>
                                <column name='cost_feb'>cost_feb + p_store_cost</column>
                                <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                                <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    store_id = p_store_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 3
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales + p_unit_sales</column>
                                <column name='sales'>sales + p_store_sales</column>
                                <column name='sales_mar'>sales_mar + p_store_sales</column>
                                <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                                <column name='cost'>cost + p_store_cost</column>
                                <column name='cost_mar'>cost_mar + p_store_cost</column>
                                <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                                <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    store_id = p_store_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 4
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales + p_unit_sales</column>
                                <column name='sales'>sales + p_store_sales</column>
                                <column name='sales_apr'>sales_apr + p_store_sales</column>
                                <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                                <column name='cost'>cost + p_store_cost</column>
                                <column name='cost_apr'>cost_apr + p_store_cost</column>
                                <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                                <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    store_id = p_store_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 5
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales + p_unit_sales</column>
                                <column name='sales'>sales + p_store_sales</column>
                                <column name='sales_may'>sales_may + p_store_sales</column>
                                <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                                <column name='cost'>cost + p_store_cost</column>
                                <column name='cost_may'>cost_may + p_store_cost</column>
                                <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                                <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    store_id = p_store_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 6
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales + p_unit_sales</column>
                                <column name='sales'>sales + p_store_sales</column>
                                <column name='sales_jun'>sales_jun + p_store_sales</column>
                                <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                                <column name='cost'>cost + p_store_cost</column>
                                <column name='cost_jun'>cost_jun + p_store_cost</column>
                                <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                                <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    store_id = p_store_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 7
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales + p_unit_sales</column>
                                <column name='sales'>sales + p_store_sales</column>
                                <column name='sales_jul'>sales_jul + p_store_sales</column>
                                <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                                <column name='cost'>cost + p_store_cost</column>
                                <column name='cost_jul'>cost_jul + p_store_cost</column>
                                <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                                <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    store_id = p_store_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 8
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales + p_unit_sales</column>
                                <column name='sales'>sales + p_store_sales</column>
                                <column name='sales_ago'>sales_ago + p_store_sales</column>
                                <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                                <column name='cost'>cost + p_store_cost</column>
                                <column name='cost_ago'>cost_ago + p_store_cost</column>
                                <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                                <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    store_id = p_store_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 9
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales + p_unit_sales</column>
                                <column name='sales'>sales + p_store_sales</column>
                                <column name='sales_sep'>sales_sep + p_store_sales</column>
                                <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                                <column name='cost'>cost + p_store_cost</column>
                                <column name='cost_sep'>cost_sep + p_store_cost</column>
                                <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                                <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    store_id = p_store_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 10
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales + p_unit_sales</column>
                                <column name='sales'>sales + p_store_sales</column>
                                <column name='sales_oct'>sales_oct + p_store_sales</column>
                                <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                                <column name='cost'>cost + p_store_cost</column>
                                <column name='cost_oct'>cost_oct + p_store_cost</column>
                                <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                                <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    store_id = p_store_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 11
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales + p_unit_sales</column>
                                <column name='sales'>sales + p_store_sales</column>
                                <column name='sales_nov'>sales_nov + p_store_sales</column>
                                <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                                <column name='cost'>cost + p_store_cost</column>
                                <column name='cost_nov'>cost_nov + p_store_cost</column>
                                <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                                <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    store_id = p_store_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 12
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales + p_unit_sales</column>
                                <column name='sales'>sales + p_store_sales</column>
                                <column name='sales_dec'>sales_dec + p_store_sales</column>
                                <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                                <column name='cost'>cost + p_store_cost</column>
                                <column name='cost_dec'>cost_dec + p_store_cost</column>
                                <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                                <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    store_id = p_store_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                </if>
                
            </then>
            <else>
                <if>
                    <expr>
                        m_month_of_year == 1
                    </expr>
                    <then>
                    
                        <insert table='sales_fact_month_store'>
                            <column name='store_id'>p_store_id</column>
                            <column name='the_year'>m_the_year</column>
                            <column name='unit_sales'>p_unit_sales</column>
                            <column name='unit_sales_prev'>m_unit_sales_prev</column>
                            <column name='sales'>p_store_sales</column>
                            <column name='sales_prev'>m_sales_prev</column>
                            <column name='sales_jan'>p_store_sales</column>
                            <column name='sales_avg'>p_store_cost/12.0</column>
                            <column name='cost'>p_store_cost</column>
                            <column name='cost_jan'>p_store_cost</column>
                            <column name='cost_prev'>m_cost_prev</column>
                            <column name='cost_avg'>p_store_cost/12.0</column>
                            <column name='profit'>p_store_sales - p_store_cost</column>
                            <column name='promotion_sales'>m_promotion_cost</column>
                            <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                            <column name='sales_count'>1</column>
                            <column name='sales_count_prev'>m_sales_count_prev</column>
                        </insert>

                    </then>
                    <else-if>
                        <expr>
                            m_month_of_year == 2
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_store'>
                                <column name='store_id'>p_store_id</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_feb'>p_store_sales</column>
                                <column name='sales_avg'>p_store_cost/12.0</column>
                                <column name='cost'>p_store_cost</column>
                                <column name='cost_feb'>p_store_cost</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost/12.0</column>
                                <column name='profit'>p_store_sales - p_store_cost</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 3
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_store'>
                                <column name='store_id'>p_store_id</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_mar'>p_store_sales</column>
                                <column name='sales_avg'>p_store_cost/12.0</column>
                                <column name='cost'>p_store_cost</column>
                                <column name='cost_mar'>p_store_cost</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost/12.0</column>
                                <column name='profit'>p_store_sales - p_store_cost</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 4
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_store'>
                                <column name='store_id'>p_store_id</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_apr'>p_store_sales</column>
                                <column name='sales_avg'>p_store_cost/12.0</column>
                                <column name='cost'>p_store_cost</column>
                                <column name='cost_apr'>p_store_cost</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost/12.0</column>
                                <column name='profit'>p_store_sales - p_store_cost</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 5
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_store'>
                                <column name='store_id'>p_store_id</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_may'>p_store_sales</column>
                                <column name='sales_avg'>p_store_cost/12.0</column>
                                <column name='cost'>p_store_cost</column>
                                <column name='cost_may'>p_store_cost</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost/12.0</column>
                                <column name='profit'>p_store_sales - p_store_cost</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 6
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_store'>
                                <column name='store_id'>p_store_id</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_jun'>p_store_sales</column>
                                <column name='sales_avg'>p_store_cost/12.0</column>
                                <column name='cost'>p_store_cost</column>
                                <column name='cost_jun'>p_store_cost</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost/12.0</column>
                                <column name='profit'>p_store_sales - p_store_cost</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 7
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_store'>
                                <column name='store_id'>p_store_id</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_jul'>p_store_sales</column>
                                <column name='sales_avg'>p_store_cost/12.0</column>
                                <column name='cost'>p_store_cost</column>
                                <column name='cost_jul'>p_store_cost</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost/12.0</column>
                                <column name='profit'>p_store_sales - p_store_cost</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 8
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_store'>
                                <column name='store_id'>p_store_id</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_ago'>p_store_sales</column>
                                <column name='sales_avg'>p_store_cost/12.0</column>
                                <column name='cost'>p_store_cost</column>
                                <column name='cost_ago'>p_store_cost</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost/12.0</column>
                                <column name='profit'>p_store_sales - p_store_cost</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 9
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_store'>
                                <column name='store_id'>p_store_id</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_sep'>p_store_sales</column>
                                <column name='sales_avg'>p_store_cost/12.0</column>
                                <column name='cost'>p_store_cost</column>
                                <column name='cost_sep'>p_store_cost</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost/12.0</column>
                                <column name='profit'>p_store_sales - p_store_cost</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 10
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_store'>
                                <column name='store_id'>p_store_id</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_oct'>p_store_sales</column>
                                <column name='sales_avg'>p_store_cost/12.0</column>
                                <column name='cost'>p_store_cost</column>
                                <column name='cost_oct'>p_store_cost</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost/12.0</column>
                                <column name='profit'>p_store_sales - p_store_cost</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 11
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_store'>
                                <column name='store_id'>p_store_id</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_nov'>p_store_sales</column>
                                <column name='sales_avg'>p_store_cost/12.0</column>
                                <column name='cost'>p_store_cost</column>
                                <column name='cost_nov'>p_store_cost</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost/12.0</column>
                                <column name='profit'>p_store_sales - p_store_cost</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 12
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_store'>
                                <column name='store_id'>p_store_id</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_dec'>p_store_sales</column>
                                <column name='sales_avg'>p_store_cost/12.0</column>
                                <column name='cost'>p_store_cost</column>
                                <column name='cost_dec'>p_store_cost</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost/12.0</column>
                                <column name='profit'>p_store_sales - p_store_cost</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                </if>

            </else>
        </if>

        <!-- Updates percentage in sales_fact_month_* for actual year-->
        <if>
            <expr>
                m_sales_prev != 0 AND m_cost_prev != 0
            </expr>
            <then>
                <update table='sales_fact_month_store'>
                    <column name='sales_pct'>ROUND(((((sales)/(sales_prev))-1)*100),2)</column>
                    <column name='cost_pct'>ROUND(((((cost)/(cost_prev))-1)*100),2)</column>
                    <where>
                        store_id = p_store_id    AND
                        the_year = m_the_year
                    </where>
                </update>
            </then>
            <else-if>
                <expr>
                    m_sales_prev == 0 AND m_cost_prev != 0
                </expr>
                <then>
                    <update table='sales_fact_month_store'>
                        <column name='sales_pct'>100</column>
                        <column name='cost_pct'>ROUND(((((cost)/(cost_prev))-1)*100),2)</column>
                        <where>
                            store_id = p_store_id    AND
                            the_year = m_the_year
                        </where>
                    </update>
                </then>
            </else-if>
            <else-if>
                <expr>
                    m_sales_prev != 0 AND m_cost_prev == 0
                </expr>
                <then>
                    <update table='sales_fact_month_store'>
                        <column name='sales_pct'>ROUND(((((sales)/(sales_prev))-1)*100),2)</column>
                        <column name='cost_pct'>100</column>
                        <where>
                            store_id = p_store_id    AND
                            the_year = m_the_year
                        </where>
                    </update>
                </then>
            </else-if>
            <else-if>
                <expr>
                    m_sales_prev == 0 AND m_cost_prev == 0
                </expr>
                <then>
                    <update table='sales_fact_month_store'>
                        <column name='sales_pct'>100</column>
                        <column name='cost_pct'>100</column>
                        <where>
                            store_id = p_store_id    AND
                            the_year = m_the_year
                        </where>
                    </update>
                </then>
            </else-if>
        </if>

        <!-- Counter for number of entries in sales_fact_month_* for year + 1, if 0 will insert, if greater will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_month_store
            WHERE
                store_id = p_store_id     AND
                the_year = m_the_year + 1
        </select>

        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                <!-- Selects sales_prev and cost_prev to choose the percentage -->
                <select>
                    <columns>
                        sales, cost
                    </columns>
                    <into>m_sales_prev, m_cost_prev</into>
                    FROM sales_fact_month_store
                    WHERE 
                        store_id = p_store_id       AND
                        the_year = m_the_year
                </select>
                
                <!-- Updates percentage in actual year -->
                <if>
                    <expr>
                        m_sales_prev != 0 AND m_cost_prev != 0
                    </expr>
                    <then>
                        <update table='sales_fact_month_store'>
                            <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales</column>
                            <column name='sales_prev'>sales_prev + p_store_sales</column>
                            <column name='cost_prev'>cost_prev + p_store_cost</column>
                            <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                            <column name='sales_count_prev'>sales_count_prev + 1</column>
                            <column name='sales_pct'>ROUND(((((sales)/(sales_prev + p_store_sales))+1)*100),2)</column>
                            <column name='cost_pct'>ROUND(((((cost)/(cost_prev + p_store_cost))-1)*100),2)</column>
                            <where>
                                store_id = p_store_id    AND
                                the_year = m_the_year + 1
                            </where>
                        </update>
                    </then>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev != 0
                        </expr>
                        <then>
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales</column>
                                <column name='sales_prev'>sales_prev + p_store_sales</column>
                                <column name='cost_prev'>cost_prev + p_store_cost</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev + 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>ROUND(((((cost)/(cost_prev + p_store_cost))-1)*100),2)</column>
                                <where>
                                    store_id = p_store_id    AND
                                    the_year = m_the_year + 1
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev != 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales</column>
                                <column name='sales_prev'>sales_prev + p_store_sales</column>
                                <column name='cost_prev'>cost_prev + p_store_cost</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev + 1</column>
                                <column name='sales_pct'>ROUND(((((sales)/(sales_prev + p_store_sales))-1)*100),2)</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    store_id = p_store_id    AND
                                    the_year = m_the_year + 1
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales</column>
                                <column name='sales_prev'>sales_prev + p_store_sales</column>
                                <column name='cost_prev'>cost_prev + p_store_cost</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev + 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    store_id = p_store_id    AND
                                    the_year = m_the_year + 1
                                </where>
                            </update>
                        </then>
                    </else-if>
                </if>
            </then>
        </if>

        <!--         store SALES_FACT_YEARMONTH        -->

        <!-- Month - 1 [Year - 1] -->
        <if>
            <expr>
                m_month_of_year == 1
            </expr>
            <then>
                <set name='m_month_of_year_prev'>12</set>
                <set name='m_the_year_prev'>m_the_year - 1</set>
            </then>
            <else>
                <set name='m_month_of_year_prev'>m_month_of_year - 1</set>
                <set name='m_the_year_prev'>m_the_year</set>
            </else>
        </if>

        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_yearmonth_store
            WHERE
                store_id = p_store_id     AND
                the_year = m_the_year_prev AND
                month_of_year = m_month_of_year_prev
        </select>
        
        <!-- Selects unit_sales-prev, sales_prev, cost_prev, promotion_sales_prev and sales_count_prev for actual year -->
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                <select>
                    <columns>
                        unit_sales, sales, cost, promotion_sales, sales_count
                    </columns>
                    <into>m_unit_sales_prev, m_sales_prev, m_cost_prev, m_promotion_sales_prev, m_sales_count_prev</into>
                    FROM sales_fact_yearmonth_store
                    WHERE 
                        store_id = p_store_id     AND
                        the_year = m_the_year_prev AND
                        month_of_year = m_month_of_year_prev
                </select>
            </then>
            <else>
                <set name='m_unit_sales_prev'>0</set>
                <set name='m_sales_prev'>0.0</set>
                <set name='m_cost_prev'>0.0</set>
                <set name='m_promotion_sales_prev'>0.0</set>
                <set name='m_sales_count_prev'>0</set>
            </else>
        </if>

        <!-- Counter for number of entries in sales_fact_yearmonth_* for actual year, if 0 will insert, if greater will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_yearmonth_store
            WHERE
                store_id = p_store_id     AND
                the_year = m_the_year AND
                month_of_year = m_month_of_year
        </select>
        
        <!-- Insert or Update on sales_fact_yearmonth_store -->
        <if>
            <expr>
               m_count &gt; 0
            </expr>
            <then>
                <update table='sales_fact_yearmonth_store'>
                    <column name='unit_sales'>unit_sales + p_unit_sales</column>
                    <column name='sales'>sales + p_store_sales</column>
                    <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                    <column name='cost'>cost + p_store_cost</column>
                    <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                    <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                    <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                    <column name='sales_count'>sales_count + 1</column>
                    <where>
                        store_id = p_store_id     AND
                        the_year = m_the_year AND
                        month_of_year = m_month_of_year
                    </where>
                </update>
            </then>
            <else>
                <insert table='sales_fact_yearmonth_store'>
                    <column name='store_id'>p_store_id</column>
                    <column name='the_year'>m_the_year</column>
                    <column name='the_month'>m_the_month</column>
                    <column name='month_of_year'>m_month_of_year</column>
                    <column name='unit_sales'>p_unit_sales</column>
                    <column name='unit_sales_prev'>m_unit_sales_prev</column>
                    <column name='sales'>p_store_sales</column>
                    <column name='sales_prev'>m_sales_prev</column>
                    <column name='sales_avg'>p_store_cost/12.0</column>
                    <column name='cost'>p_store_cost</column>
                    <column name='cost_prev'>m_cost_prev</column>
                    <column name='cost_avg'>p_store_cost/12.0</column>
                    <column name='profit'>p_store_sales - p_store_cost</column>
                    <column name='promotion_sales'>m_promotion_cost</column>
                    <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                    <column name='sales_count'>1</column>
                    <column name='sales_count_prev'>m_sales_count_prev</column>
                </insert>
            </else>
        </if>

        <if>
            <expr>
                m_sales_prev != 0 AND m_cost_prev != 0
            </expr>
            <then>
                <update table='sales_fact_yearmonth_store'>
                    <column name='sales_pct'>ROUND(((((sales)/(sales_prev))-1)*100),2)</column>
                    <column name='cost_pct'>ROUND(((((cost)/(cost_prev))-1)*100),2)</column>
                    <where>
                        store_id = p_store_id     AND
                        the_year = m_the_year AND
                        month_of_year = m_month_of_year
                    </where>
                </update>
            </then>
            <else-if>
                <expr>
                    m_sales_prev == 0 AND m_cost_prev != 0
                </expr>
                <then>
                    <update table='sales_fact_yearmonth_store'>
                        <column name='sales_pct'>100</column>
                        <column name='cost_pct'>ROUND(((((cost)/(cost_prev))-1)*100),2)</column>
                        <where>
                            store_id = p_store_id     AND
                            the_year = m_the_year AND
                            month_of_year = m_month_of_year
                        </where>
                    </update>
                </then>
            </else-if>
            <else-if>
                <expr>
                    m_sales_prev != 0 AND m_cost_prev == 0
                </expr>
                <then>
                    <update table='sales_fact_yearmonth_store'>
                        <column name='sales_pct'>ROUND(((((sales)/(sales_prev))-1)*100),2)</column>
                        <column name='cost_pct'>100</column>
                        <where>
                            store_id = p_store_id     AND
                            the_year = m_the_year AND
                            month_of_year = m_month_of_year
                        </where>
                    </update>
                </then>
            </else-if>
            <else-if>
                <expr>
                    m_sales_prev == 0 AND m_cost_prev == 0
                </expr>
                <then>
                    <update table='sales_fact_yearmonth_store'>
                        <column name='sales_pct'>100</column>
                        <column name='cost_pct'>100</column>
                        <where>
                            store_id = p_store_id     AND
                            the_year = m_the_year AND
                            month_of_year = m_month_of_year
                        </where>
                    </update>
                </then>
            </else-if>
        </if>

        <!-- Month + 1 [Year + 1] -->
        <if>
            <expr>
                m_month_of_year == 12
            </expr>
            <then>
                <set name='m_month_of_year_prev'>1</set>
                <set name='m_the_year_prev'>m_the_year + 1</set>
            </then>
            <else>
                <set name='m_month_of_year_prev'>m_month_of_year + 1</set>
                <set name='m_the_year_prev'>m_the_year</set>
            </else>
        </if>

        <!-- Counter for number of entries in sales_fact_yearmonth_* for previous year, if greater than 0 will set previous year data into variables, if 0 will insert 0 -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_yearmonth_store
            WHERE
                store_id = p_store_id     AND
                the_year = m_the_year_prev AND
                month_of_year = m_month_of_year_prev
        </select>
        
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>

                <!-- Select sales_prev and cost_prev from month +1 [and year +1] -->
                <select>
                    <columns>
                        sales, cost
                    </columns>
                    <into>m_sales_prev, m_cost_prev</into>
                    FROM sales_fact_yearmonth_store
                    WHERE 
                        store_id = p_store_id       AND
                        the_year = m_the_year AND
                        month_of_year = m_month_of_year
                </select>
                
                <!-- Updates sales_fact_year_month_* for month + 1 [and year + 1] with percentages -->
                <if>
                    <expr>
                        m_sales_prev != 0 AND m_cost_prev != 0
                    </expr>
                    <then>
                        <update table='sales_fact_yearmonth_store'>
                            <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales</column>
                            <column name='sales_prev'>sales_prev + p_store_sales</column>
                            <column name='cost_prev'>cost_prev + p_store_cost</column>
                            <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                            <column name='sales_count_prev'>sales_count_prev + 1</column>
                            <column name='sales_pct'>ROUND(((((sales)/(sales_prev + p_store_sales))-1)*100),2)</column>
                            <column name='cost_pct'>ROUND(((((cost)/(cost_prev + p_store_cost))-1)*100),2)</column>
                            <where>
                                store_id = p_store_id    AND
                                the_year = m_the_year_prev AND
                                month_of_year = m_month_of_year_prev
                            </where>
                        </update>
                    </then>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev != 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_store'>
                                <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales</column>
                                <column name='sales_prev'>sales_prev + p_store_sales</column>
                                <column name='cost_prev'>cost_prev + p_store_cost</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev + 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>ROUND(((((cost)/(cost_prev + p_store_cost))-1)*100),2)</column>
                                <where>
                                    store_id = p_store_id    AND
                                    the_year = m_the_year_prev AND
                                    month_of_year = m_month_of_year_prev
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev != 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_store'>
                                <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales</column>
                                <column name='sales_prev'>sales_prev + p_store_sales</column>
                                <column name='cost_prev'>cost_prev + p_store_cost</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev + 1</column>
                                <column name='sales_pct'>ROUND(((((sales)/(sales_prev + p_store_sales))-1)*100),2)</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    store_id = p_store_id    AND
                                    the_year = m_the_year_prev AND
                                    month_of_year = m_month_of_year_prev
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_store'>
                                <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales</column>
                                <column name='sales_prev'>sales_prev + p_store_sales</column>
                                <column name='cost_prev'>cost_prev + p_store_cost</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev + 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    store_id = p_store_id    AND
                                    the_year = m_the_year_prev AND
                                    month_of_year = m_month_of_year_prev
                                </where>
                            </update>
                        </then>
                    </else-if>
                </if>
            </then>
        </if>
        
        <!--    product     -->
        
        <!-- Counter for number of entries in sales_fact_month_* for previous year, if greater than 0 will set previous year data into variables, if 0 will insert 0 -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_month_product
            WHERE
                product_id = p_product_id     AND
                the_year = m_the_year - 1
        </select>
        
        <!-- Selects unit_sales-prev, sales_prev, cost_prev, promotion_sales_prev and sales_count_prev for actual year -->
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                <select>
                    <columns>
                        unit_sales, sales, cost, promotion_sales, sales_count
                    </columns>
                    <into>m_unit_sales_prev, m_sales_prev, m_cost_prev, m_promotion_sales_prev, m_sales_count_prev</into>
                    FROM sales_fact_month_product
                    WHERE 
                        product_id = p_product_id     AND
                        the_year = m_the_year - 1
                </select>
            </then>
            <else>
                <set name='m_unit_sales_prev'>0</set>
                <set name='m_sales_prev'>0.0</set>
                <set name='m_cost_prev'>0.0</set>
                <set name='m_promotion_sales_prev'>0.0</set>
                <set name='m_sales_count_prev'>0</set>
            </else>
        </if>

        <!-- Counter for number of entries in sales_fact_month_* for actual year, if 0 will insert, if greater will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_month_product
            WHERE
                product_id = p_product_id     AND
                the_year = m_the_year
        </select>

        <!-- Insert or Update year on sales_fact_month_product -->
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                
                <if>
                    <expr>
                        m_month_of_year == 1
                    </expr>
                    <then>
                        
                        <update table='sales_fact_month_product'>
                            <column name='unit_sales'>unit_sales + p_unit_sales</column>
                            <column name='sales'>sales + p_store_sales</column>
                            <column name='sales_jan'>sales_jan + p_store_sales</column>
                            <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                            <column name='cost'>cost + p_store_cost</column>
                            <column name='cost_jan'>cost_jan + p_store_cost</column>
                            <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                            <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                            <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                            <column name='sales_count'>sales_count + 1</column>
                            <where>
                                product_id = p_product_id     AND
                                the_year = m_the_year
                            </where>
                        </update>
    
                    </then>
                    <else-if>
                        <expr>
                            m_month_of_year == 2
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales + p_unit_sales</column>
                                <column name='sales'>sales + p_store_sales</column>
                                <column name='sales_feb'>sales_feb + p_store_sales</column>
                                <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                                <column name='cost'>cost + p_store_cost</column>
                                <column name='cost_feb'>cost_feb + p_store_cost</column>
                                <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                                <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    product_id = p_product_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 3
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales + p_unit_sales</column>
                                <column name='sales'>sales + p_store_sales</column>
                                <column name='sales_mar'>sales_mar + p_store_sales</column>
                                <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                                <column name='cost'>cost + p_store_cost</column>
                                <column name='cost_mar'>cost_mar + p_store_cost</column>
                                <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                                <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    product_id = p_product_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 4
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales + p_unit_sales</column>
                                <column name='sales'>sales + p_store_sales</column>
                                <column name='sales_apr'>sales_apr + p_store_sales</column>
                                <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                                <column name='cost'>cost + p_store_cost</column>
                                <column name='cost_apr'>cost_apr + p_store_cost</column>
                                <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                                <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    product_id = p_product_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 5
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales + p_unit_sales</column>
                                <column name='sales'>sales + p_store_sales</column>
                                <column name='sales_may'>sales_may + p_store_sales</column>
                                <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                                <column name='cost'>cost + p_store_cost</column>
                                <column name='cost_may'>cost_may + p_store_cost</column>
                                <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                                <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    product_id = p_product_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 6
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales + p_unit_sales</column>
                                <column name='sales'>sales + p_store_sales</column>
                                <column name='sales_jun'>sales_jun + p_store_sales</column>
                                <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                                <column name='cost'>cost + p_store_cost</column>
                                <column name='cost_jun'>cost_jun + p_store_cost</column>
                                <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                                <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    product_id = p_product_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 7
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales + p_unit_sales</column>
                                <column name='sales'>sales + p_store_sales</column>
                                <column name='sales_jul'>sales_jul + p_store_sales</column>
                                <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                                <column name='cost'>cost + p_store_cost</column>
                                <column name='cost_jul'>cost_jul + p_store_cost</column>
                                <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                                <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    product_id = p_product_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 8
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales + p_unit_sales</column>
                                <column name='sales'>sales + p_store_sales</column>
                                <column name='sales_ago'>sales_ago + p_store_sales</column>
                                <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                                <column name='cost'>cost + p_store_cost</column>
                                <column name='cost_ago'>cost_ago + p_store_cost</column>
                                <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                                <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    product_id = p_product_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 9
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales + p_unit_sales</column>
                                <column name='sales'>sales + p_store_sales</column>
                                <column name='sales_sep'>sales_sep + p_store_sales</column>
                                <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                                <column name='cost'>cost + p_store_cost</column>
                                <column name='cost_sep'>cost_sep + p_store_cost</column>
                                <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                                <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    product_id = p_product_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 10
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales + p_unit_sales</column>
                                <column name='sales'>sales + p_store_sales</column>
                                <column name='sales_oct'>sales_oct + p_store_sales</column>
                                <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                                <column name='cost'>cost + p_store_cost</column>
                                <column name='cost_oct'>cost_oct + p_store_cost</column>
                                <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                                <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    product_id = p_product_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 11
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales + p_unit_sales</column>
                                <column name='sales'>sales + p_store_sales</column>
                                <column name='sales_nov'>sales_nov + p_store_sales</column>
                                <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                                <column name='cost'>cost + p_store_cost</column>
                                <column name='cost_nov'>cost_nov + p_store_cost</column>
                                <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                                <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    product_id = p_product_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 12
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales + p_unit_sales</column>
                                <column name='sales'>sales + p_store_sales</column>
                                <column name='sales_dec'>sales_dec + p_store_sales</column>
                                <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                                <column name='cost'>cost + p_store_cost</column>
                                <column name='cost_dec'>cost_dec + p_store_cost</column>
                                <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                                <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    product_id = p_product_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                </if>
                
            </then>
            <else>
                <if>
                    <expr>
                        m_month_of_year == 1
                    </expr>
                    <then>
                    
                        <insert table='sales_fact_month_product'>
                            <column name='product_id'>p_product_id</column>
                            <column name='the_year'>m_the_year</column>
                            <column name='unit_sales'>p_unit_sales</column>
                            <column name='unit_sales_prev'>m_unit_sales_prev</column>
                            <column name='sales'>p_store_sales</column>
                            <column name='sales_prev'>m_sales_prev</column>
                            <column name='sales_jan'>p_store_sales</column>
                            <column name='sales_avg'>p_store_cost/12.0</column>
                            <column name='cost'>p_store_cost</column>
                            <column name='cost_jan'>p_store_cost</column>
                            <column name='cost_prev'>m_cost_prev</column>
                            <column name='cost_avg'>p_store_cost/12.0</column>
                            <column name='profit'>p_store_sales - p_store_cost</column>
                            <column name='promotion_sales'>m_promotion_cost</column>
                            <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                            <column name='sales_count'>1</column>
                            <column name='sales_count_prev'>m_sales_count_prev</column>
                        </insert>

                    </then>
                    <else-if>
                        <expr>
                            m_month_of_year == 2
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_product'>
                                <column name='product_id'>p_product_id</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_feb'>p_store_sales</column>
                                <column name='sales_avg'>p_store_cost/12.0</column>
                                <column name='cost'>p_store_cost</column>
                                <column name='cost_feb'>p_store_cost</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost/12.0</column>
                                <column name='profit'>p_store_sales - p_store_cost</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 3
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_product'>
                                <column name='product_id'>p_product_id</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_mar'>p_store_sales</column>
                                <column name='sales_avg'>p_store_cost/12.0</column>
                                <column name='cost'>p_store_cost</column>
                                <column name='cost_mar'>p_store_cost</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost/12.0</column>
                                <column name='profit'>p_store_sales - p_store_cost</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 4
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_product'>
                                <column name='product_id'>p_product_id</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_apr'>p_store_sales</column>
                                <column name='sales_avg'>p_store_cost/12.0</column>
                                <column name='cost'>p_store_cost</column>
                                <column name='cost_apr'>p_store_cost</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost/12.0</column>
                                <column name='profit'>p_store_sales - p_store_cost</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 5
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_product'>
                                <column name='product_id'>p_product_id</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_may'>p_store_sales</column>
                                <column name='sales_avg'>p_store_cost/12.0</column>
                                <column name='cost'>p_store_cost</column>
                                <column name='cost_may'>p_store_cost</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost/12.0</column>
                                <column name='profit'>p_store_sales - p_store_cost</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 6
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_product'>
                                <column name='product_id'>p_product_id</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_jun'>p_store_sales</column>
                                <column name='sales_avg'>p_store_cost/12.0</column>
                                <column name='cost'>p_store_cost</column>
                                <column name='cost_jun'>p_store_cost</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost/12.0</column>
                                <column name='profit'>p_store_sales - p_store_cost</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 7
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_product'>
                                <column name='product_id'>p_product_id</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_jul'>p_store_sales</column>
                                <column name='sales_avg'>p_store_cost/12.0</column>
                                <column name='cost'>p_store_cost</column>
                                <column name='cost_jul'>p_store_cost</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost/12.0</column>
                                <column name='profit'>p_store_sales - p_store_cost</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 8
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_product'>
                                <column name='product_id'>p_product_id</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_ago'>p_store_sales</column>
                                <column name='sales_avg'>p_store_cost/12.0</column>
                                <column name='cost'>p_store_cost</column>
                                <column name='cost_ago'>p_store_cost</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost/12.0</column>
                                <column name='profit'>p_store_sales - p_store_cost</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 9
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_product'>
                                <column name='product_id'>p_product_id</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_sep'>p_store_sales</column>
                                <column name='sales_avg'>p_store_cost/12.0</column>
                                <column name='cost'>p_store_cost</column>
                                <column name='cost_sep'>p_store_cost</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost/12.0</column>
                                <column name='profit'>p_store_sales - p_store_cost</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 10
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_product'>
                                <column name='product_id'>p_product_id</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_oct'>p_store_sales</column>
                                <column name='sales_avg'>p_store_cost/12.0</column>
                                <column name='cost'>p_store_cost</column>
                                <column name='cost_oct'>p_store_cost</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost/12.0</column>
                                <column name='profit'>p_store_sales - p_store_cost</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 11
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_product'>
                                <column name='product_id'>p_product_id</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_nov'>p_store_sales</column>
                                <column name='sales_avg'>p_store_cost/12.0</column>
                                <column name='cost'>p_store_cost</column>
                                <column name='cost_nov'>p_store_cost</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost/12.0</column>
                                <column name='profit'>p_store_sales - p_store_cost</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 12
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_product'>
                                <column name='product_id'>p_product_id</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_dec'>p_store_sales</column>
                                <column name='sales_avg'>p_store_cost/12.0</column>
                                <column name='cost'>p_store_cost</column>
                                <column name='cost_dec'>p_store_cost</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost/12.0</column>
                                <column name='profit'>p_store_sales - p_store_cost</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                </if>

            </else>
        </if>

        <!-- Updates percentage in sales_fact_month_* for actual year-->
        <if>
            <expr>
                m_sales_prev != 0 AND m_cost_prev != 0
            </expr>
            <then>
                <update table='sales_fact_month_product'>
                    <column name='sales_pct'>ROUND(((((sales)/(sales_prev))-1)*100),2)</column>
                    <column name='cost_pct'>ROUND(((((cost)/(cost_prev))-1)*100),2)</column>
                    <where>
                        product_id = p_product_id    AND
                        the_year = m_the_year
                    </where>
                </update>
            </then>
            <else-if>
                <expr>
                    m_sales_prev == 0 AND m_cost_prev != 0
                </expr>
                <then>
                    <update table='sales_fact_month_product'>
                        <column name='sales_pct'>100</column>
                        <column name='cost_pct'>ROUND(((((cost)/(cost_prev))-1)*100),2)</column>
                        <where>
                            product_id = p_product_id    AND
                            the_year = m_the_year
                        </where>
                    </update>
                </then>
            </else-if>
            <else-if>
                <expr>
                    m_sales_prev != 0 AND m_cost_prev == 0
                </expr>
                <then>
                    <update table='sales_fact_month_product'>
                        <column name='sales_pct'>ROUND(((((sales)/(sales_prev))-1)*100),2)</column>
                        <column name='cost_pct'>100</column>
                        <where>
                            product_id = p_product_id    AND
                            the_year = m_the_year
                        </where>
                    </update>
                </then>
            </else-if>
            <else-if>
                <expr>
                    m_sales_prev == 0 AND m_cost_prev == 0
                </expr>
                <then>
                    <update table='sales_fact_month_product'>
                        <column name='sales_pct'>100</column>
                        <column name='cost_pct'>100</column>
                        <where>
                            product_id = p_product_id    AND
                            the_year = m_the_year
                        </where>
                    </update>
                </then>
            </else-if>
        </if>

        <!-- Counter for number of entries in sales_fact_month_* for year + 1, if 0 will insert, if greater will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_month_product
            WHERE
                product_id = p_product_id     AND
                the_year = m_the_year + 1
        </select>

        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                <!-- Selects sales_prev and cost_prev to choose the percentage -->
                <select>
                    <columns>
                        sales, cost
                    </columns>
                    <into>m_sales_prev, m_cost_prev</into>
                    FROM sales_fact_month_product
                    WHERE 
                        product_id = p_product_id       AND
                        the_year = m_the_year
                </select>
                
                <!-- Updates percentage in actual year -->
                <if>
                    <expr>
                        m_sales_prev != 0 AND m_cost_prev != 0
                    </expr>
                    <then>
                        <update table='sales_fact_month_product'>
                            <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales</column>
                            <column name='sales_prev'>sales_prev + p_store_sales</column>
                            <column name='cost_prev'>cost_prev + p_store_cost</column>
                            <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                            <column name='sales_count_prev'>sales_count_prev + 1</column>
                            <column name='sales_pct'>ROUND(((((sales)/(sales_prev + p_store_sales))+1)*100),2)</column>
                            <column name='cost_pct'>ROUND(((((cost)/(cost_prev + p_store_cost))-1)*100),2)</column>
                            <where>
                                product_id = p_product_id    AND
                                the_year = m_the_year + 1
                            </where>
                        </update>
                    </then>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev != 0
                        </expr>
                        <then>
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales</column>
                                <column name='sales_prev'>sales_prev + p_store_sales</column>
                                <column name='cost_prev'>cost_prev + p_store_cost</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev + 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>ROUND(((((cost)/(cost_prev + p_store_cost))-1)*100),2)</column>
                                <where>
                                    product_id = p_product_id    AND
                                    the_year = m_the_year + 1
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev != 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales</column>
                                <column name='sales_prev'>sales_prev + p_store_sales</column>
                                <column name='cost_prev'>cost_prev + p_store_cost</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev + 1</column>
                                <column name='sales_pct'>ROUND(((((sales)/(sales_prev + p_store_sales))-1)*100),2)</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    product_id = p_product_id    AND
                                    the_year = m_the_year + 1
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales</column>
                                <column name='sales_prev'>sales_prev + p_store_sales</column>
                                <column name='cost_prev'>cost_prev + p_store_cost</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev + 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    product_id = p_product_id    AND
                                    the_year = m_the_year + 1
                                </where>
                            </update>
                        </then>
                    </else-if>
                </if>
            </then>
        </if>

        <!--         product SALES_FACT_YEARMONTH        -->

        <!-- Month - 1 [Year - 1] -->
        <if>
            <expr>
                m_month_of_year == 1
            </expr>
            <then>
                <set name='m_month_of_year_prev'>12</set>
                <set name='m_the_year_prev'>m_the_year - 1</set>
            </then>
            <else>
                <set name='m_month_of_year_prev'>m_month_of_year - 1</set>
                <set name='m_the_year_prev'>m_the_year</set>
            </else>
        </if>

        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_yearmonth_product
            WHERE
                product_id = p_product_id     AND
                the_year = m_the_year_prev AND
                month_of_year = m_month_of_year_prev
        </select>
        
        <!-- Selects unit_sales-prev, sales_prev, cost_prev, promotion_sales_prev and sales_count_prev for actual year -->
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                <select>
                    <columns>
                        unit_sales, sales, cost, promotion_sales, sales_count
                    </columns>
                    <into>m_unit_sales_prev, m_sales_prev, m_cost_prev, m_promotion_sales_prev, m_sales_count_prev</into>
                    FROM sales_fact_yearmonth_product
                    WHERE 
                        product_id = p_product_id     AND
                        the_year = m_the_year_prev AND
                        month_of_year = m_month_of_year_prev
                </select>
            </then>
            <else>
                <set name='m_unit_sales_prev'>0</set>
                <set name='m_sales_prev'>0.0</set>
                <set name='m_cost_prev'>0.0</set>
                <set name='m_promotion_sales_prev'>0.0</set>
                <set name='m_sales_count_prev'>0</set>
            </else>
        </if>

        <!-- Counter for number of entries in sales_fact_yearmonth_* for actual year, if 0 will insert, if greater will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_yearmonth_product
            WHERE
                product_id = p_product_id     AND
                the_year = m_the_year AND
                month_of_year = m_month_of_year
        </select>
        
        <!-- Insert or Update on sales_fact_yearmonth_product -->
        <if>
            <expr>
               m_count &gt; 0
            </expr>
            <then>
                <update table='sales_fact_yearmonth_product'>
                    <column name='unit_sales'>unit_sales + p_unit_sales</column>
                    <column name='sales'>sales + p_store_sales</column>
                    <column name='sales_avg'>sales_avg + p_store_cost/12.0</column>
                    <column name='cost'>cost + p_store_cost</column>
                    <column name='cost_avg'>cost_avg + p_store_cost/12.0</column>
                    <column name='profit'>profit + (p_store_sales - p_store_cost)</column>
                    <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                    <column name='sales_count'>sales_count + 1</column>
                    <where>
                        product_id = p_product_id     AND
                        the_year = m_the_year AND
                        month_of_year = m_month_of_year
                    </where>
                </update>
            </then>
            <else>
                <insert table='sales_fact_yearmonth_product'>
                    <column name='product_id'>p_product_id</column>
                    <column name='the_year'>m_the_year</column>
                    <column name='the_month'>m_the_month</column>
                    <column name='month_of_year'>m_month_of_year</column>
                    <column name='unit_sales'>p_unit_sales</column>
                    <column name='unit_sales_prev'>m_unit_sales_prev</column>
                    <column name='sales'>p_store_sales</column>
                    <column name='sales_prev'>m_sales_prev</column>
                    <column name='sales_avg'>p_store_cost/12.0</column>
                    <column name='cost'>p_store_cost</column>
                    <column name='cost_prev'>m_cost_prev</column>
                    <column name='cost_avg'>p_store_cost/12.0</column>
                    <column name='profit'>p_store_sales - p_store_cost</column>
                    <column name='promotion_sales'>m_promotion_cost</column>
                    <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                    <column name='sales_count'>1</column>
                    <column name='sales_count_prev'>m_sales_count_prev</column>
                </insert>
            </else>
        </if>

        <if>
            <expr>
                m_sales_prev != 0 AND m_cost_prev != 0
            </expr>
            <then>
                <update table='sales_fact_yearmonth_product'>
                    <column name='sales_pct'>ROUND(((((sales)/(sales_prev))-1)*100),2)</column>
                    <column name='cost_pct'>ROUND(((((cost)/(cost_prev))-1)*100),2)</column>
                    <where>
                        product_id = p_product_id     AND
                        the_year = m_the_year AND
                        month_of_year = m_month_of_year
                    </where>
                </update>
            </then>
            <else-if>
                <expr>
                    m_sales_prev == 0 AND m_cost_prev != 0
                </expr>
                <then>
                    <update table='sales_fact_yearmonth_product'>
                        <column name='sales_pct'>100</column>
                        <column name='cost_pct'>ROUND(((((cost)/(cost_prev))-1)*100),2)</column>
                        <where>
                            product_id = p_product_id     AND
                            the_year = m_the_year AND
                            month_of_year = m_month_of_year
                        </where>
                    </update>
                </then>
            </else-if>
            <else-if>
                <expr>
                    m_sales_prev != 0 AND m_cost_prev == 0
                </expr>
                <then>
                    <update table='sales_fact_yearmonth_product'>
                        <column name='sales_pct'>ROUND(((((sales)/(sales_prev))-1)*100),2)</column>
                        <column name='cost_pct'>100</column>
                        <where>
                            product_id = p_product_id     AND
                            the_year = m_the_year AND
                            month_of_year = m_month_of_year
                        </where>
                    </update>
                </then>
            </else-if>
            <else-if>
                <expr>
                    m_sales_prev == 0 AND m_cost_prev == 0
                </expr>
                <then>
                    <update table='sales_fact_yearmonth_product'>
                        <column name='sales_pct'>100</column>
                        <column name='cost_pct'>100</column>
                        <where>
                            product_id = p_product_id     AND
                            the_year = m_the_year AND
                            month_of_year = m_month_of_year
                        </where>
                    </update>
                </then>
            </else-if>
        </if>

        <!-- Month + 1 [Year + 1] -->
        <if>
            <expr>
                m_month_of_year == 12
            </expr>
            <then>
                <set name='m_month_of_year_prev'>1</set>
                <set name='m_the_year_prev'>m_the_year + 1</set>
            </then>
            <else>
                <set name='m_month_of_year_prev'>m_month_of_year + 1</set>
                <set name='m_the_year_prev'>m_the_year</set>
            </else>
        </if>

        <!-- Counter for number of entries in sales_fact_yearmonth_* for previous year, if greater than 0 will set previous year data into variables, if 0 will insert 0 -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_yearmonth_product
            WHERE
                product_id = p_product_id     AND
                the_year = m_the_year_prev AND
                month_of_year = m_month_of_year_prev
        </select>
        
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>

                <!-- Select sales_prev and cost_prev from month +1 [and year +1] -->
                <select>
                    <columns>
                        sales, cost
                    </columns>
                    <into>m_sales_prev, m_cost_prev</into>
                    FROM sales_fact_yearmonth_product
                    WHERE 
                        product_id = p_product_id       AND
                        the_year = m_the_year AND
                        month_of_year = m_month_of_year
                </select>
                
                <!-- Updates sales_fact_year_month_* for month + 1 [and year + 1] with percentages -->
                <if>
                    <expr>
                        m_sales_prev != 0 AND m_cost_prev != 0
                    </expr>
                    <then>
                        <update table='sales_fact_yearmonth_product'>
                            <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales</column>
                            <column name='sales_prev'>sales_prev + p_store_sales</column>
                            <column name='cost_prev'>cost_prev + p_store_cost</column>
                            <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                            <column name='sales_count_prev'>sales_count_prev + 1</column>
                            <column name='sales_pct'>ROUND(((((sales)/(sales_prev + p_store_sales))-1)*100),2)</column>
                            <column name='cost_pct'>ROUND(((((cost)/(cost_prev + p_store_cost))-1)*100),2)</column>
                            <where>
                                product_id = p_product_id    AND
                                the_year = m_the_year_prev AND
                                month_of_year = m_month_of_year_prev
                            </where>
                        </update>
                    </then>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev != 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_product'>
                                <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales</column>
                                <column name='sales_prev'>sales_prev + p_store_sales</column>
                                <column name='cost_prev'>cost_prev + p_store_cost</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev + 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>ROUND(((((cost)/(cost_prev + p_store_cost))-1)*100),2)</column>
                                <where>
                                    product_id = p_product_id    AND
                                    the_year = m_the_year_prev AND
                                    month_of_year = m_month_of_year_prev
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev != 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_product'>
                                <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales</column>
                                <column name='sales_prev'>sales_prev + p_store_sales</column>
                                <column name='cost_prev'>cost_prev + p_store_cost</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev + 1</column>
                                <column name='sales_pct'>ROUND(((((sales)/(sales_prev + p_store_sales))-1)*100),2)</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    product_id = p_product_id    AND
                                    the_year = m_the_year_prev AND
                                    month_of_year = m_month_of_year_prev
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_product'>
                                <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales</column>
                                <column name='sales_prev'>sales_prev + p_store_sales</column>
                                <column name='cost_prev'>cost_prev + p_store_cost</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev + 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    product_id = p_product_id    AND
                                    the_year = m_the_year_prev AND
                                    month_of_year = m_month_of_year_prev
                                </where>
                            </update>
                        </then>
                    </else-if>
                </if>
            </then>
        </if>
    </body>
</procedure>
-- **************************************************************************
-- sales_fact_act_ins
-- DEISTER WebStudio XSQL-UDFUNC  -  Engine: ids
-- **************************************************************************

3.2 Update

3.2.1 Trigger

When a new entry is updated in sales_fact; sales_fact_month_customer, sales_fact_month_product and sales_fact_month_store update the old corresponding entries and update or insert the new corresponding entries. The same happens to sales_fact_yearmonth_customer, sales_fact_yearmonth_product and sales_fact_yearmonth_store.

<xsql-trigger
    name='sales_fact_upd'
    event='update'
    table='sales_fact'
>
    <foreach-row>
        <execute-procedure name='sales_fact_act_upd'>
            <in>
                <param><nxt>product_id</nxt></param>
                <param><prv>product_id</prv></param>
                <param><nxt>time_id</nxt></param>
                <param><prv>time_id</prv></param>
                <param><nxt>customer_id</nxt></param>
                <param><prv>customer_id</prv></param>
                <param><nxt>promotion_id</nxt></param>
                <param><prv>promotion_id</prv></param>
                <param><nxt>store_id</nxt></param>
                <param><prv>store_id</prv></param>
                <param><nxt>store_sales</nxt></param>
                <param><prv>store_sales</prv></param>
                <param><nxt>store_cost</nxt></param>
                <param><prv>store_cost</prv></param>
                <param><nxt>unit_sales</nxt></param>
                <param><prv>unit_sales</prv></param>
            </in>	
        </execute-procedure>	
    </foreach-row>
</xsql-trigger>
-- **************************************************************************
-- sales_fact_upd
-- DEISTER WebStudio XSQL-TRIGGER Wed Nov 25 22:41:31 CET 2020 Engine: ids
-- **************************************************************************

3.2.2 Procedure

Depending on what column is modified, the procedure will either modify the product tables, the store tables, the customer tables or all of them. When doing the modification, it will update the old row to the proper values. Then, it will update or insert a year row in each or the corresponding sales_fact_month_* and also a month row in the respective sales_fact_yearmonth_*. As the tables have columns for previous months or years, when inserting it will search for a previous year or month to get the values it needs. If found, it will fill in those fields, if not a 0 will be placed in them. The *_prev columns for the following year or month are also updated (if existing) with the corresponding values of the current year.

<procedure name='sales_fact_act_upd'>
   	
    <args>
        <arg name='p_product_id_new'            like='sales_fact.product_id'                            />
        <arg name='p_product_id_old'            like='sales_fact.product_id'                            />
        <arg name='p_time_id_new'               like='sales_fact.time_id'                               />
        <arg name='p_time_id_old'               like='sales_fact.time_id'                               />
        <arg name='p_customer_id_new'           like='sales_fact.customer_id'                           />
        <arg name='p_customer_id_old'           like='sales_fact.customer_id'                           />
        <arg name='p_promotion_id_new'          like='sales_fact.promotion_id'                          />
        <arg name='p_promotion_id_old'          like='sales_fact.promotion_id'                          />
        <arg name='p_store_id_new'              like='sales_fact.store_id'                              />
        <arg name='p_store_id_old'              like='sales_fact.store_id'                              />
        <arg name='p_store_sales_new'           like='sales_fact.store_sales'                           />
        <arg name='p_store_sales_old'           like='sales_fact.store_sales'                           />
        <arg name='p_store_cost_new'            like='sales_fact.store_cost'                            />
        <arg name='p_store_cost_old'            like='sales_fact.store_cost'                            />
        <arg name='p_unit_sales_new'            like='sales_fact.unit_sales'                            />
        <arg name='p_unit_sales_old'            like='sales_fact.unit_sales'                            />
    </args> 
        
    <define>    
        <variable name='m_month_of_year'        like='time_by_day.month_of_year'                        />
        <variable name='m_month_of_year_prev'   like='time_by_day.month_of_year'                        />
        <variable name='m_the_month'            like='time_by_day.the_month'                            />
        <variable name='m_the_month_prev'       like='time_by_day.the_month'                            />
        <variable name='m_the_year'             like='time_by_day.the_year'                             />
        <variable name='m_the_year_prev'        like='time_by_day.the_year'                             />
            
        <variable name='m_count'                type='integer'                                          />
            
        <variable name='m_unit_sales_prev'      like='sales_fact_month_customer.unit_sales_prev'        />
        <variable name='m_sales_prev'           like='sales_fact_month_customer.sales_prev'             />
        <variable name='m_cost_prev'            like='sales_fact_month_customer.cost_prev'              />
        <variable name='m_promotion_sales_prev' like='sales_fact_month_customer.promotion_sales_prev'   />
        <variable name='m_sales_count_prev'     like='sales_fact_month_customer.sales_count_prev'       />
        
        <variable name='m_promotion_cost'       like='sales_fact_month_customer.promotion_sales'        />
    </define>

	<body>
	    
        <!-- 		OLD 		 -->
        
        <!-- Gets month, month number and year -->
        <select>
            <columns>
                month_of_year, the_month, the_year
            </columns>
            <into>m_month_of_year, m_the_month, m_the_year</into>
            FROM time_by_day
            WHERE time_id = p_time_id_old
        </select>

        <!-- If promotion is applied, sales are substracted -->
        <if>
            <expr>
                p_promotion_id_old &gt; 0
            </expr>
            <then>
                <set name='m_promotion_cost'>p_store_sales_old</set>
            </then>
            <else>
                <set name='m_promotion_cost'>0.0</set>
            </else>
        </if>
        
        
        
        <!--        CUSTOMER        -->

        <!--         STORE SALES_FACT_MONTH        -->

        <!-- Counter for number of entries in sales_fact_month_* for actual year, if 0 will insert, if greater will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_month_customer
            WHERE
                customer_id = p_customer_id_old     AND
                the_year = m_the_year
        </select>

        <!-- Update on sales_fact_month_customer for actual year-->
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                
                <if>
                    <expr>
                        m_month_of_year == 1
                    </expr>
                    <then>
                        
                        <update table='sales_fact_month_customer'>
                            <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                            <column name='sales'>sales - p_store_sales_old</column>
                            <column name='sales_jan'>sales_jan - p_store_sales_old</column>
                            <column name='sales_avg'>sales_avg - p_store_cost_old/12</column>
                            <column name='cost'>cost - p_store_cost_old</column>
                            <column name='cost_jan'>cost_jan - p_store_cost_old</column>
                            <column name='cost_avg'>cost_avg - p_store_cost_old/12</column>
                            <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                            <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                            <column name='sales_count'>sales_count - 1</column>

                            <where>
                                customer_id = p_customer_id_old     AND
                                the_year = m_the_year
                            </where>
                        </update>
    
                    </then>
                    <else-if>
                        <expr>
                            m_month_of_year == 2
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_feb'>sales_feb - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_feb'>cost_feb - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    customer_id = p_customer_id_old     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 3
                        </expr>
                        <then>
                            
                            
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_mar'>sales_mar - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_mar'>cost_mar - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    customer_id = p_customer_id_old     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 4
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_apr'>sales_apr - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_apr'>cost_apr - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    customer_id = p_customer_id_old     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 5
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_may'>sales_may - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_may'>cost_may - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    customer_id = p_customer_id_old     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 6
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_jun'>sales_jun - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_jun'>cost_jun - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    customer_id = p_customer_id_old     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 7
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_jul'>sales_jul - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_jul'>cost_jul - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    customer_id = p_customer_id_old     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 8
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_ago'>sales_ago - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_ago'>cost_ago - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    customer_id = p_customer_id_old     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 9
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_sep'>sales_sep - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_sep'>cost_sep - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    customer_id = p_customer_id_old     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 10
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_oct'>sales_oct - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_oct'>cost_oct - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    customer_id = p_customer_id_old     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 11
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_nov'>sales_nov - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_nov'>cost_nov - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    customer_id = p_customer_id_old     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 12
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_feb'>sales_feb - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_dec'>cost_dec - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    customer_id = p_customer_id_old     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                </if>
                
            </then>
        </if>

        <!-- Counter for number of entries in sales_fact_month_* for year - 1, if greater than 0 will set previous year data into variables, if 0 will insert 0 -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_month_customer
            WHERE
                customer_id = p_customer_id_old     AND
                the_year = m_the_year - 1
        </select>
        
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                <select>
                    <columns>
                        sales, cost
                    </columns>
                    <into>m_sales_prev, m_cost_prev</into>
                    FROM sales_fact_month_customer
                    WHERE 
                        customer_id = p_customer_id_old     AND
                        the_year = m_the_year - 1
                </select>
            </then>
            <else>
                <set name='m_sales_prev'>0.0</set>
                <set name='m_cost_prev'>0.0</set>
            </else>
        </if>

        <!-- Updates percentage in sales_fact_month_* actual year -->
        <if>
            <expr>
                m_sales_prev != 0 AND m_cost_prev != 0
            </expr>
            <then>
                <update table='sales_fact_month_customer'>
                    <column name='sales_pct'>ROUND(((((sales)/(sales_prev))-1)*100),2)</column>
                    <column name='cost_pct'>ROUND(((((cost)/(cost_prev))-1)*100),2)</column>
                    <where>
                        customer_id = p_customer_id_old    AND
                        the_year = m_the_year
                    </where>
                </update>
            </then>
            <else-if>
                <expr>
                    m_sales_prev == 0 AND m_cost_prev != 0
                </expr>
                <then>
                    <update table='sales_fact_month_customer'>
                        <column name='sales_pct'>100</column>
                        <column name='cost_pct'>ROUND(((((cost)/(cost_prev))-1)*100),2)</column>
                        <where>
                            customer_id = p_customer_id_old    AND
                            the_year = m_the_year
                        </where>
                    </update>
                </then>
            </else-if>
            <else-if>
                <expr>
                    m_sales_prev != 0 AND m_cost_prev == 0
                </expr>
                <then>
                    <update table='sales_fact_month_customer'>
                        <column name='sales_pct'>ROUND(((((sales)/(sales_prev))-1)*100),2)</column>
                        <column name='cost_pct'>100</column>
                        <where>
                            customer_id = p_customer_id_old    AND
                            the_year = m_the_year
                        </where>
                    </update>
                </then>
            </else-if>
            <else-if>
                <expr>
                    m_sales_prev == 0 AND m_cost_prev == 0
                </expr>
                <then>
                    <update table='sales_fact_month_customer'>
                        <column name='sales_pct'>100</column>
                        <column name='cost_pct'>100</column>
                        <where>
                            customer_id = p_customer_id_old    AND
                            the_year = m_the_year
                        </where>
                    </update>
                </then>
            </else-if>
        </if>
        
        <!-- Counter for number of entries in sales_fact_month_* for year + 1, if 0 will insert, if greater will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_month_customer
            WHERE
                customer_id = p_customer_id_old     AND
                the_year = m_the_year + 1
        </select>

        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>

                <!-- Selects sales_prev and cost_prev to choose the percentage -->
                <select>
                    <columns>
                        sales, cost
                    </columns>
                    <into>m_sales_prev, m_cost_prev</into>
                    FROM sales_fact_month_customer
                    WHERE 
                        customer_id = p_customer_id_old       AND
                        the_year = m_the_year
                </select>
                
                <!-- Updates percentage in actual year -->
                <if>
                    <expr>
                        m_sales_prev != 0 AND m_cost_prev != 0
                    </expr>
                    <then>
                        <update table='sales_fact_month_customer'>
                            <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales_old</column>
                            <column name='sales_prev'>sales_prev - p_store_sales_old</column>
                            <column name='cost_prev'>cost_prev - p_store_cost_old</column>
                            <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                            <column name='sales_count_prev'>sales_count_prev - 1</column>
                            <column name='sales_pct'>ROUND(((((sales)/(sales_prev - p_store_sales_old))-1)*100),2)</column>
                            <column name='cost_pct'>ROUND(((((cost)/(cost_prev - p_store_cost_old))-1)*100),2)</column>
                            <where>
                                customer_id = p_customer_id_old    AND
                                the_year = m_the_year + 1
                            </where>
                        </update>
                    </then>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev != 0
                        </expr>
                        <then>
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales_old</column>
                                <column name='sales_prev'>sales_prev - p_store_sales_old</column>
                                <column name='cost_prev'>cost_prev - p_store_cost_old</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev - 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>ROUND(((((cost)/(cost_prev - p_store_cost_old))-1)*100),2)</column>
                                <where>
                                    customer_id = p_customer_id_old    AND
                                    the_year = m_the_year + 1
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev != 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales_old</column>
                                <column name='sales_prev'>sales_prev - p_store_sales_old</column>
                                <column name='cost_prev'>cost_prev - p_store_cost_old</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev - 1</column>
                                <column name='sales_pct'>ROUND(((((sales)/(sales_prev - p_store_sales_old))-1)*100),2)</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    customer_id = p_customer_id_old    AND
                                    the_year = m_the_year + 1
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales_old</column>
                                <column name='sales_prev'>sales_prev - p_store_sales_old</column>
                                <column name='cost_prev'>cost_prev - p_store_cost_old</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev - 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    customer_id = p_customer_id_old    AND
                                    the_year = m_the_year + 1
                                </where>
                            </update>
                        </then>
                    </else-if>
                </if>
            </then>
        </if>

        <!--         CUSTOMER SALES_FACT_YEARMONTH        -->

        <!-- Month - 1 [Year - 1] -->
        <if>
            <expr>
                m_month_of_year == 1
            </expr>
            <then>
                <set name='m_month_of_year_prev'>12</set>
                <set name='m_the_year_prev'>m_the_year - 1</set>
            </then>
            <else>
                <set name='m_month_of_year_prev'>m_month_of_year - 1</set>
                <set name='m_the_year_prev'>m_the_year</set>
            </else>
        </if>
        
        <!-- Counter for number of entries in sales_fact_yearmonth_* for previous year, if greater than 0 will set previous year data into variables, if 0 will insert 0 -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_yearmonth_customer
            WHERE
                customer_id = p_customer_id_old     AND
                the_year = m_the_year_prev AND
                month_of_year = m_month_of_year_prev
        </select>
        
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                <select>
                    <columns>
                        sales, cost
                    </columns>
                    <into>m_sales_prev, m_cost_prev</into>
                    FROM sales_fact_yearmonth_customer
                    WHERE 
                            customer_id = p_customer_id_old     AND
                            month_of_year = m_month_of_year_prev AND
                            the_year = m_the_year_prev
                </select>
            </then>
            <else>
                <set name='m_sales_prev'>0.0</set>
                <set name='m_cost_prev'>0.0</set>
            </else>
        </if>

        <!-- Counter for number of entries in sales_fact_yearmonth_* for actual month and year, if greater than 0 will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_yearmonth_customer
            WHERE
                customer_id = p_customer_id_old     AND
                the_year = m_the_year AND
                month_of_year = m_month_of_year
        </select>

        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                <!-- Updates percentage in sales_fact_month_* -->
                <if>
                    <expr>
                        m_sales_prev != 0 AND m_cost_prev != 0
                    </expr>
                    <then>
                        <update table='sales_fact_yearmonth_customer'>
                            <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                            <column name='sales'>sales - p_store_sales_old</column>
                            <column name='sales_avg'>sales_avg - p_store_cost_old/12.0</column>
                            <column name='cost'>cost - p_store_cost_old</column>
                            <column name='cost_avg'>cost_avg - p_store_cost_old/12.0</column>
                            <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                            <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                            <column name='sales_count'>sales_count - 1</column>
                            <column name='sales_pct'>ROUND(((((sales - p_store_sales_old)/(sales_prev))-1)*100),2)</column>
                            <column name='cost_pct'>ROUND(((((cost - p_store_cost_old)/(cost_prev))-1)*100),2)</column>
                            <where>
                                customer_id = p_customer_id_old     AND
                                the_year = m_the_year AND
                                month_of_year = m_month_of_year
                            </where>
                        </update>
                    </then>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev != 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_customer'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12.0</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12.0</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>ROUND(((((cost - p_store_cost_old)/(cost_prev))-1)*100),2)</column>
                                <where>
                                    customer_id = p_customer_id_old     AND
                                    the_year = m_the_year AND
                                    month_of_year = m_month_of_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev != 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_customer'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12.0</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12.0</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>
                                    <column name='sales_pct'>ROUND(((((sales - p_store_sales_old)/(sales_prev))-1)*100),2)</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    customer_id = p_customer_id_old     AND
                                    the_year = m_the_year AND
                                    month_of_year = m_month_of_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_customer'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12.0</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12.0</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    customer_id = p_customer_id_old     AND
                                    the_year = m_the_year AND
                                    month_of_year = m_month_of_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                </if>
            </then>
        </if>

        <!-- Month + 1 [Year + 1] -->
        <if>
            <expr>
                m_month_of_year == 12
            </expr>
            <then>
                <set name='m_month_of_year_prev'>1</set>
                <set name='m_the_year_prev'>m_the_year + 1</set>
            </then>
            <else>
                <set name='m_month_of_year_prev'>m_month_of_year + 1</set>
                <set name='m_the_year_prev'>m_the_year</set>
            </else>
        </if>
        
        <!-- Counter for number of entries in sales_fact_yearmonth_* for month + 1 [and year + 1], if 0 will insert, if greater will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_yearmonth_customer
            WHERE
                customer_id = p_customer_id_old     AND
                the_year = m_the_year_prev AND
                month_of_year = m_month_of_year_prev
        </select>
        
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>

                <!-- Select sales_prev and cost_prev from month +1 [and year +1] -->
                <select>
                    <columns>
                        sales, cost
                    </columns>
                    <into>m_sales_prev, m_cost_prev</into>
                    FROM sales_fact_yearmonth_customer
                    WHERE 
                        customer_id = p_customer_id_old       AND
                        the_year = m_the_year AND
                        month_of_year = m_month_of_year
                </select>
                
                <!-- Updates sales_fact_year_month_* for month + 1 [and year + 1] with percentages -->
                <if>
                    <expr>
                        m_sales_prev != 0 AND m_cost_prev != 0
                    </expr>
                    <then>
                        <update table='sales_fact_yearmonth_customer'>
                            <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales_old</column>
                            <column name='sales_prev'>sales_prev - p_store_sales_old</column>
                            <column name='cost_prev'>cost_prev - p_store_cost_old</column>
                            <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                            <column name='sales_count_prev'>sales_count_prev - 1</column>
                            <column name='sales_pct'>ROUND(((((sales)/(sales_prev - p_store_sales_old))-1)*100),2)</column>
                            <column name='cost_pct'>ROUND(((((cost)/(cost_prev - p_store_cost_old))-1)*100),2)</column>
                            <where>
                                customer_id = p_customer_id_old    AND
                                the_year = m_the_year_prev AND
                                month_of_year = m_month_of_year_prev
                            </where>
                        </update>
                    </then>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev != 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_customer'>
                                <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales_old</column>
                                <column name='sales_prev'>sales_prev - p_store_sales_old</column>
                                <column name='cost_prev'>cost_prev - p_store_cost_old</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev - 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>ROUND(((((cost)/(cost_prev - p_store_cost_old))-1)*100),2)</column>
                                <where>
                                    customer_id = p_customer_id_old    AND
                                    the_year = m_the_year_prev AND
                                    month_of_year = m_month_of_year_prev
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev != 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_customer'>
                                <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales_old</column>
                                <column name='sales_prev'>sales_prev - p_store_sales_old</column>
                                <column name='cost_prev'>cost_prev - p_store_cost_old</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev - 1</column>
                                <column name='sales_pct'>ROUND(((((sales)/(sales_prev - p_store_sales_old))-1)*100),2)</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    customer_id = p_customer_id_old    AND
                                    the_year = m_the_year_prev AND
                                    month_of_year = m_month_of_year_prev
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_customer'>
                                <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales_old</column>
                                <column name='sales_prev'>sales_prev - p_store_sales_old</column>
                                <column name='cost_prev'>cost_prev - p_store_cost_old</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev - 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    customer_id = p_customer_id_old    AND
                                    the_year = m_the_year_prev AND
                                    month_of_year = m_month_of_year_prev
                                </where>
                            </update>
                        </then>
                    </else-if>
                </if>
            </then>
        </if>
        
        <!--        store        -->

        <!--         STORE SALES_FACT_MONTH        -->

        <!-- Counter for number of entries in sales_fact_month_* for actual year, if 0 will insert, if greater will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_month_store
            WHERE
                store_id = p_store_id_old     AND
                the_year = m_the_year
        </select>

        <!-- Update on sales_fact_month_store for actual year-->
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                
                <if>
                    <expr>
                        m_month_of_year == 1
                    </expr>
                    <then>
                        
                        <update table='sales_fact_month_store'>
                            <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                            <column name='sales'>sales - p_store_sales_old</column>
                            <column name='sales_jan'>sales_jan - p_store_sales_old</column>
                            <column name='sales_avg'>sales_avg - p_store_cost_old/12</column>
                            <column name='cost'>cost - p_store_cost_old</column>
                            <column name='cost_jan'>cost_jan - p_store_cost_old</column>
                            <column name='cost_avg'>cost_avg - p_store_cost_old/12</column>
                            <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                            <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                            <column name='sales_count'>sales_count - 1</column>

                            <where>
                                store_id = p_store_id_old     AND
                                the_year = m_the_year
                            </where>
                        </update>
    
                    </then>
                    <else-if>
                        <expr>
                            m_month_of_year == 2
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_feb'>sales_feb - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_feb'>cost_feb - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    store_id = p_store_id_old     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 3
                        </expr>
                        <then>
                            
                            
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_mar'>sales_mar - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_mar'>cost_mar - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    store_id = p_store_id_old     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 4
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_apr'>sales_apr - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_apr'>cost_apr - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    store_id = p_store_id_old     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 5
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_may'>sales_may - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_may'>cost_may - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    store_id = p_store_id_old     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 6
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_jun'>sales_jun - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_jun'>cost_jun - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    store_id = p_store_id_old     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 7
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_jul'>sales_jul - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_jul'>cost_jul - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    store_id = p_store_id_old     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 8
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_ago'>sales_ago - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_ago'>cost_ago - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    store_id = p_store_id_old     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 9
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_sep'>sales_sep - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_sep'>cost_sep - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    store_id = p_store_id_old     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 10
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_oct'>sales_oct - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_oct'>cost_oct - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    store_id = p_store_id_old     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 11
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_nov'>sales_nov - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_nov'>cost_nov - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    store_id = p_store_id_old     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 12
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_feb'>sales_feb - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_dec'>cost_dec - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    store_id = p_store_id_old     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                </if>
                
            </then>
        </if>

        <!-- Counter for number of entries in sales_fact_month_* for year - 1, if greater than 0 will set previous year data into variables, if 0 will insert 0 -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_month_store
            WHERE
                store_id = p_store_id_old     AND
                the_year = m_the_year - 1
        </select>
        
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                <select>
                    <columns>
                        sales, cost
                    </columns>
                    <into>m_sales_prev, m_cost_prev</into>
                    FROM sales_fact_month_store
                    WHERE 
                        store_id = p_store_id_old     AND
                        the_year = m_the_year - 1
                </select>
            </then>
            <else>
                <set name='m_sales_prev'>0.0</set>
                <set name='m_cost_prev'>0.0</set>
            </else>
        </if>

        <!-- Updates percentage in sales_fact_month_* actual year -->
        <if>
            <expr>
                m_sales_prev != 0 AND m_cost_prev != 0
            </expr>
            <then>
                <update table='sales_fact_month_store'>
                    <column name='sales_pct'>ROUND(((((sales)/(sales_prev))-1)*100),2)</column>
                    <column name='cost_pct'>ROUND(((((cost)/(cost_prev))-1)*100),2)</column>
                    <where>
                        store_id = p_store_id_old    AND
                        the_year = m_the_year
                    </where>
                </update>
            </then>
            <else-if>
                <expr>
                    m_sales_prev == 0 AND m_cost_prev != 0
                </expr>
                <then>
                    <update table='sales_fact_month_store'>
                        <column name='sales_pct'>100</column>
                        <column name='cost_pct'>ROUND(((((cost)/(cost_prev))-1)*100),2)</column>
                        <where>
                            store_id = p_store_id_old    AND
                            the_year = m_the_year
                        </where>
                    </update>
                </then>
            </else-if>
            <else-if>
                <expr>
                    m_sales_prev != 0 AND m_cost_prev == 0
                </expr>
                <then>
                    <update table='sales_fact_month_store'>
                        <column name='sales_pct'>ROUND(((((sales)/(sales_prev))-1)*100),2)</column>
                        <column name='cost_pct'>100</column>
                        <where>
                            store_id = p_store_id_old    AND
                            the_year = m_the_year
                        </where>
                    </update>
                </then>
            </else-if>
            <else-if>
                <expr>
                    m_sales_prev == 0 AND m_cost_prev == 0
                </expr>
                <then>
                    <update table='sales_fact_month_store'>
                        <column name='sales_pct'>100</column>
                        <column name='cost_pct'>100</column>
                        <where>
                            store_id = p_store_id_old    AND
                            the_year = m_the_year
                        </where>
                    </update>
                </then>
            </else-if>
        </if>
        
        <!-- Counter for number of entries in sales_fact_month_* for year + 1, if 0 will insert, if greater will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_month_store
            WHERE
                store_id = p_store_id_old     AND
                the_year = m_the_year + 1
        </select>

        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>

                <!-- Selects sales_prev and cost_prev to choose the percentage -->
                <select>
                    <columns>
                        sales, cost
                    </columns>
                    <into>m_sales_prev, m_cost_prev</into>
                    FROM sales_fact_month_store
                    WHERE 
                        store_id = p_store_id_old       AND
                        the_year = m_the_year
                </select>
                
                <!-- Updates percentage in actual year -->
                <if>
                    <expr>
                        m_sales_prev != 0 AND m_cost_prev != 0
                    </expr>
                    <then>
                        <update table='sales_fact_month_store'>
                            <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales_old</column>
                            <column name='sales_prev'>sales_prev - p_store_sales_old</column>
                            <column name='cost_prev'>cost_prev - p_store_cost_old</column>
                            <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                            <column name='sales_count_prev'>sales_count_prev - 1</column>
                            <column name='sales_pct'>ROUND(((((sales)/(sales_prev - p_store_sales_old))-1)*100),2)</column>
                            <column name='cost_pct'>ROUND(((((cost)/(cost_prev - p_store_cost_old))-1)*100),2)</column>
                            <where>
                                store_id = p_store_id_old    AND
                                the_year = m_the_year + 1
                            </where>
                        </update>
                    </then>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev != 0
                        </expr>
                        <then>
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales_old</column>
                                <column name='sales_prev'>sales_prev - p_store_sales_old</column>
                                <column name='cost_prev'>cost_prev - p_store_cost_old</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev - 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>ROUND(((((cost)/(cost_prev - p_store_cost_old))-1)*100),2)</column>
                                <where>
                                    store_id = p_store_id_old    AND
                                    the_year = m_the_year + 1
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev != 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales_old</column>
                                <column name='sales_prev'>sales_prev - p_store_sales_old</column>
                                <column name='cost_prev'>cost_prev - p_store_cost_old</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev - 1</column>
                                <column name='sales_pct'>ROUND(((((sales)/(sales_prev - p_store_sales_old))-1)*100),2)</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    store_id = p_store_id_old    AND
                                    the_year = m_the_year + 1
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales_old</column>
                                <column name='sales_prev'>sales_prev - p_store_sales_old</column>
                                <column name='cost_prev'>cost_prev - p_store_cost_old</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev - 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    store_id = p_store_id_old    AND
                                    the_year = m_the_year + 1
                                </where>
                            </update>
                        </then>
                    </else-if>
                </if>
            </then>
        </if>

        <!--         store SALES_FACT_YEARMONTH        -->

        <!-- Month - 1 [Year - 1] -->
        <if>
            <expr>
                m_month_of_year == 1
            </expr>
            <then>
                <set name='m_month_of_year_prev'>12</set>
                <set name='m_the_year_prev'>m_the_year - 1</set>
            </then>
            <else>
                <set name='m_month_of_year_prev'>m_month_of_year - 1</set>
                <set name='m_the_year_prev'>m_the_year</set>
            </else>
        </if>
        
        <!-- Counter for number of entries in sales_fact_yearmonth_* for previous year, if greater than 0 will set previous year data into variables, if 0 will insert 0 -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_yearmonth_store
            WHERE
                store_id = p_store_id_old     AND
                the_year = m_the_year_prev AND
                month_of_year = m_month_of_year_prev
        </select>
        
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                <select>
                    <columns>
                        sales, cost
                    </columns>
                    <into>m_sales_prev, m_cost_prev</into>
                    FROM sales_fact_yearmonth_store
                    WHERE 
                            store_id = p_store_id_old     AND
                            month_of_year = m_month_of_year_prev AND
                            the_year = m_the_year_prev
                </select>
            </then>
            <else>
                <set name='m_sales_prev'>0.0</set>
                <set name='m_cost_prev'>0.0</set>
            </else>
        </if>

        <!-- Counter for number of entries in sales_fact_yearmonth_* for actual month and year, if greater than 0 will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_yearmonth_store
            WHERE
                store_id = p_store_id_old     AND
                the_year = m_the_year AND
                month_of_year = m_month_of_year
        </select>

        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                <!-- Updates percentage in sales_fact_month_* -->
                <if>
                    <expr>
                        m_sales_prev != 0 AND m_cost_prev != 0
                    </expr>
                    <then>
                        <update table='sales_fact_yearmonth_store'>
                            <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                            <column name='sales'>sales - p_store_sales_old</column>
                            <column name='sales_avg'>sales_avg - p_store_cost_old/12.0</column>
                            <column name='cost'>cost - p_store_cost_old</column>
                            <column name='cost_avg'>cost_avg - p_store_cost_old/12.0</column>
                            <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                            <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                            <column name='sales_count'>sales_count - 1</column>
                            <column name='sales_pct'>ROUND(((((sales - p_store_sales_old)/(sales_prev))-1)*100),2)</column>
                            <column name='cost_pct'>ROUND(((((cost - p_store_cost_old)/(cost_prev))-1)*100),2)</column>
                            <where>
                                store_id = p_store_id_old     AND
                                the_year = m_the_year AND
                                month_of_year = m_month_of_year
                            </where>
                        </update>
                    </then>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev != 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_store'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12.0</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12.0</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>ROUND(((((cost - p_store_cost_old)/(cost_prev))-1)*100),2)</column>
                                <where>
                                    store_id = p_store_id_old     AND
                                    the_year = m_the_year AND
                                    month_of_year = m_month_of_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev != 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_store'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12.0</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12.0</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>
                                    <column name='sales_pct'>ROUND(((((sales - p_store_sales_old)/(sales_prev))-1)*100),2)</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    store_id = p_store_id_old     AND
                                    the_year = m_the_year AND
                                    month_of_year = m_month_of_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_store'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12.0</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12.0</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    store_id = p_store_id_old     AND
                                    the_year = m_the_year AND
                                    month_of_year = m_month_of_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                </if>
            </then>
        </if>

        <!-- Month + 1 [Year + 1] -->
        <if>
            <expr>
                m_month_of_year == 12
            </expr>
            <then>
                <set name='m_month_of_year_prev'>1</set>
                <set name='m_the_year_prev'>m_the_year + 1</set>
            </then>
            <else>
                <set name='m_month_of_year_prev'>m_month_of_year + 1</set>
                <set name='m_the_year_prev'>m_the_year</set>
            </else>
        </if>
        
        <!-- Counter for number of entries in sales_fact_yearmonth_* for month + 1 [and year + 1], if 0 will insert, if greater will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_yearmonth_store
            WHERE
                store_id = p_store_id_old     AND
                the_year = m_the_year_prev AND
                month_of_year = m_month_of_year_prev
        </select>
        
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>

                <!-- Select sales_prev and cost_prev from month +1 [and year +1] -->
                <select>
                    <columns>
                        sales, cost
                    </columns>
                    <into>m_sales_prev, m_cost_prev</into>
                    FROM sales_fact_yearmonth_store
                    WHERE 
                        store_id = p_store_id_old       AND
                        the_year = m_the_year AND
                        month_of_year = m_month_of_year
                </select>
                
                <!-- Updates sales_fact_year_month_* for month + 1 [and year + 1] with percentages -->
                <if>
                    <expr>
                        m_sales_prev != 0 AND m_cost_prev != 0
                    </expr>
                    <then>
                        <update table='sales_fact_yearmonth_store'>
                            <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales_old</column>
                            <column name='sales_prev'>sales_prev - p_store_sales_old</column>
                            <column name='cost_prev'>cost_prev - p_store_cost_old</column>
                            <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                            <column name='sales_count_prev'>sales_count_prev - 1</column>
                            <column name='sales_pct'>ROUND(((((sales)/(sales_prev - p_store_sales_old))-1)*100),2)</column>
                            <column name='cost_pct'>ROUND(((((cost)/(cost_prev - p_store_cost_old))-1)*100),2)</column>
                            <where>
                                store_id = p_store_id_old    AND
                                the_year = m_the_year_prev AND
                                month_of_year = m_month_of_year_prev
                            </where>
                        </update>
                    </then>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev != 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_store'>
                                <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales_old</column>
                                <column name='sales_prev'>sales_prev - p_store_sales_old</column>
                                <column name='cost_prev'>cost_prev - p_store_cost_old</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev - 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>ROUND(((((cost)/(cost_prev - p_store_cost_old))-1)*100),2)</column>
                                <where>
                                    store_id = p_store_id_old    AND
                                    the_year = m_the_year_prev AND
                                    month_of_year = m_month_of_year_prev
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev != 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_store'>
                                <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales_old</column>
                                <column name='sales_prev'>sales_prev - p_store_sales_old</column>
                                <column name='cost_prev'>cost_prev - p_store_cost_old</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev - 1</column>
                                <column name='sales_pct'>ROUND(((((sales)/(sales_prev - p_store_sales_old))-1)*100),2)</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    store_id = p_store_id_old    AND
                                    the_year = m_the_year_prev AND
                                    month_of_year = m_month_of_year_prev
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_store'>
                                <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales_old</column>
                                <column name='sales_prev'>sales_prev - p_store_sales_old</column>
                                <column name='cost_prev'>cost_prev - p_store_cost_old</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev - 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    store_id = p_store_id_old    AND
                                    the_year = m_the_year_prev AND
                                    month_of_year = m_month_of_year_prev
                                </where>
                            </update>
                        </then>
                    </else-if>
                </if>
            </then>
        </if>
        
        <!--        product        -->

        <!--         STORE SALES_FACT_MONTH        -->

        <!-- Counter for number of entries in sales_fact_month_* for actual year, if 0 will insert, if greater will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_month_product
            WHERE
                product_id = p_product_id_old     AND
                the_year = m_the_year
        </select>

        <!-- Update on sales_fact_month_product for actual year-->
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                
                <if>
                    <expr>
                        m_month_of_year == 1
                    </expr>
                    <then>
                        
                        <update table='sales_fact_month_product'>
                            <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                            <column name='sales'>sales - p_store_sales_old</column>
                            <column name='sales_jan'>sales_jan - p_store_sales_old</column>
                            <column name='sales_avg'>sales_avg - p_store_cost_old/12</column>
                            <column name='cost'>cost - p_store_cost_old</column>
                            <column name='cost_jan'>cost_jan - p_store_cost_old</column>
                            <column name='cost_avg'>cost_avg - p_store_cost_old/12</column>
                            <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                            <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                            <column name='sales_count'>sales_count - 1</column>

                            <where>
                                product_id = p_product_id_old     AND
                                the_year = m_the_year
                            </where>
                        </update>
    
                    </then>
                    <else-if>
                        <expr>
                            m_month_of_year == 2
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_feb'>sales_feb - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_feb'>cost_feb - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    product_id = p_product_id_old     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 3
                        </expr>
                        <then>
                            
                            
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_mar'>sales_mar - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_mar'>cost_mar - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    product_id = p_product_id_old     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 4
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_apr'>sales_apr - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_apr'>cost_apr - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    product_id = p_product_id_old     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 5
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_may'>sales_may - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_may'>cost_may - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    product_id = p_product_id_old     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 6
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_jun'>sales_jun - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_jun'>cost_jun - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    product_id = p_product_id_old     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 7
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_jul'>sales_jul - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_jul'>cost_jul - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    product_id = p_product_id_old     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 8
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_ago'>sales_ago - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_ago'>cost_ago - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    product_id = p_product_id_old     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 9
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_sep'>sales_sep - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_sep'>cost_sep - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    product_id = p_product_id_old     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 10
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_oct'>sales_oct - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_oct'>cost_oct - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    product_id = p_product_id_old     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 11
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_nov'>sales_nov - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_nov'>cost_nov - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    product_id = p_product_id_old     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 12
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_feb'>sales_feb - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_dec'>cost_dec - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    product_id = p_product_id_old     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                </if>
                
            </then>
        </if>

        <!-- Counter for number of entries in sales_fact_month_* for year - 1, if greater than 0 will set previous year data into variables, if 0 will insert 0 -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_month_product
            WHERE
                product_id = p_product_id_old     AND
                the_year = m_the_year - 1
        </select>
        
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                <select>
                    <columns>
                        sales, cost
                    </columns>
                    <into>m_sales_prev, m_cost_prev</into>
                    FROM sales_fact_month_product
                    WHERE 
                        product_id = p_product_id_old     AND
                        the_year = m_the_year - 1
                </select>
            </then>
            <else>
                <set name='m_sales_prev'>0.0</set>
                <set name='m_cost_prev'>0.0</set>
            </else>
        </if>

        <!-- Updates percentage in sales_fact_month_* actual year -->
        <if>
            <expr>
                m_sales_prev != 0 AND m_cost_prev != 0
            </expr>
            <then>
                <update table='sales_fact_month_product'>
                    <column name='sales_pct'>ROUND(((((sales)/(sales_prev))-1)*100),2)</column>
                    <column name='cost_pct'>ROUND(((((cost)/(cost_prev))-1)*100),2)</column>
                    <where>
                        product_id = p_product_id_old    AND
                        the_year = m_the_year
                    </where>
                </update>
            </then>
            <else-if>
                <expr>
                    m_sales_prev == 0 AND m_cost_prev != 0
                </expr>
                <then>
                    <update table='sales_fact_month_product'>
                        <column name='sales_pct'>100</column>
                        <column name='cost_pct'>ROUND(((((cost)/(cost_prev))-1)*100),2)</column>
                        <where>
                            product_id = p_product_id_old    AND
                            the_year = m_the_year
                        </where>
                    </update>
                </then>
            </else-if>
            <else-if>
                <expr>
                    m_sales_prev != 0 AND m_cost_prev == 0
                </expr>
                <then>
                    <update table='sales_fact_month_product'>
                        <column name='sales_pct'>ROUND(((((sales)/(sales_prev))-1)*100),2)</column>
                        <column name='cost_pct'>100</column>
                        <where>
                            product_id = p_product_id_old    AND
                            the_year = m_the_year
                        </where>
                    </update>
                </then>
            </else-if>
            <else-if>
                <expr>
                    m_sales_prev == 0 AND m_cost_prev == 0
                </expr>
                <then>
                    <update table='sales_fact_month_product'>
                        <column name='sales_pct'>100</column>
                        <column name='cost_pct'>100</column>
                        <where>
                            product_id = p_product_id_old    AND
                            the_year = m_the_year
                        </where>
                    </update>
                </then>
            </else-if>
        </if>
        
        <!-- Counter for number of entries in sales_fact_month_* for year + 1, if 0 will insert, if greater will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_month_product
            WHERE
                product_id = p_product_id_old     AND
                the_year = m_the_year + 1
        </select>

        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>

                <!-- Selects sales_prev and cost_prev to choose the percentage -->
                <select>
                    <columns>
                        sales, cost
                    </columns>
                    <into>m_sales_prev, m_cost_prev</into>
                    FROM sales_fact_month_product
                    WHERE 
                        product_id = p_product_id_old       AND
                        the_year = m_the_year
                </select>
                
                <!-- Updates percentage in actual year -->
                <if>
                    <expr>
                        m_sales_prev != 0 AND m_cost_prev != 0
                    </expr>
                    <then>
                        <update table='sales_fact_month_product'>
                            <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales_old</column>
                            <column name='sales_prev'>sales_prev - p_store_sales_old</column>
                            <column name='cost_prev'>cost_prev - p_store_cost_old</column>
                            <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                            <column name='sales_count_prev'>sales_count_prev - 1</column>
                            <column name='sales_pct'>ROUND(((((sales)/(sales_prev - p_store_sales_old))-1)*100),2)</column>
                            <column name='cost_pct'>ROUND(((((cost)/(cost_prev - p_store_cost_old))-1)*100),2)</column>
                            <where>
                                product_id = p_product_id_old    AND
                                the_year = m_the_year + 1
                            </where>
                        </update>
                    </then>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev != 0
                        </expr>
                        <then>
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales_old</column>
                                <column name='sales_prev'>sales_prev - p_store_sales_old</column>
                                <column name='cost_prev'>cost_prev - p_store_cost_old</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev - 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>ROUND(((((cost)/(cost_prev - p_store_cost_old))-1)*100),2)</column>
                                <where>
                                    product_id = p_product_id_old    AND
                                    the_year = m_the_year + 1
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev != 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales_old</column>
                                <column name='sales_prev'>sales_prev - p_store_sales_old</column>
                                <column name='cost_prev'>cost_prev - p_store_cost_old</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev - 1</column>
                                <column name='sales_pct'>ROUND(((((sales)/(sales_prev - p_store_sales_old))-1)*100),2)</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    product_id = p_product_id_old    AND
                                    the_year = m_the_year + 1
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales_old</column>
                                <column name='sales_prev'>sales_prev - p_store_sales_old</column>
                                <column name='cost_prev'>cost_prev - p_store_cost_old</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev - 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    product_id = p_product_id_old    AND
                                    the_year = m_the_year + 1
                                </where>
                            </update>
                        </then>
                    </else-if>
                </if>
            </then>
        </if>

        <!--         product SALES_FACT_YEARMONTH        -->

        <!-- Month - 1 [Year - 1] -->
        <if>
            <expr>
                m_month_of_year == 1
            </expr>
            <then>
                <set name='m_month_of_year_prev'>12</set>
                <set name='m_the_year_prev'>m_the_year - 1</set>
            </then>
            <else>
                <set name='m_month_of_year_prev'>m_month_of_year - 1</set>
                <set name='m_the_year_prev'>m_the_year</set>
            </else>
        </if>
        
        <!-- Counter for number of entries in sales_fact_yearmonth_* for previous year, if greater than 0 will set previous year data into variables, if 0 will insert 0 -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_yearmonth_product
            WHERE
                product_id = p_product_id_old     AND
                the_year = m_the_year_prev AND
                month_of_year = m_month_of_year_prev
        </select>
        
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                <select>
                    <columns>
                        sales, cost
                    </columns>
                    <into>m_sales_prev, m_cost_prev</into>
                    FROM sales_fact_yearmonth_product
                    WHERE 
                            product_id = p_product_id_old     AND
                            month_of_year = m_month_of_year_prev AND
                            the_year = m_the_year_prev
                </select>
            </then>
            <else>
                <set name='m_sales_prev'>0.0</set>
                <set name='m_cost_prev'>0.0</set>
            </else>
        </if>

        <!-- Counter for number of entries in sales_fact_yearmonth_* for actual month and year, if greater than 0 will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_yearmonth_product
            WHERE
                product_id = p_product_id_old     AND
                the_year = m_the_year AND
                month_of_year = m_month_of_year
        </select>

        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                <!-- Updates percentage in sales_fact_month_* -->
                <if>
                    <expr>
                        m_sales_prev != 0 AND m_cost_prev != 0
                    </expr>
                    <then>
                        <update table='sales_fact_yearmonth_product'>
                            <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                            <column name='sales'>sales - p_store_sales_old</column>
                            <column name='sales_avg'>sales_avg - p_store_cost_old/12.0</column>
                            <column name='cost'>cost - p_store_cost_old</column>
                            <column name='cost_avg'>cost_avg - p_store_cost_old/12.0</column>
                            <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                            <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                            <column name='sales_count'>sales_count - 1</column>
                            <column name='sales_pct'>ROUND(((((sales - p_store_sales_old)/(sales_prev))-1)*100),2)</column>
                            <column name='cost_pct'>ROUND(((((cost - p_store_cost_old)/(cost_prev))-1)*100),2)</column>
                            <where>
                                product_id = p_product_id_old     AND
                                the_year = m_the_year AND
                                month_of_year = m_month_of_year
                            </where>
                        </update>
                    </then>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev != 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_product'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12.0</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12.0</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>ROUND(((((cost - p_store_cost_old)/(cost_prev))-1)*100),2)</column>
                                <where>
                                    product_id = p_product_id_old     AND
                                    the_year = m_the_year AND
                                    month_of_year = m_month_of_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev != 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_product'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12.0</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12.0</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>
                                    <column name='sales_pct'>ROUND(((((sales - p_store_sales_old)/(sales_prev))-1)*100),2)</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    product_id = p_product_id_old     AND
                                    the_year = m_the_year AND
                                    month_of_year = m_month_of_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_product'>
                                <column name='unit_sales'>unit_sales - p_unit_sales_old</column>
                                <column name='sales'>sales - p_store_sales_old</column>
                                <column name='sales_avg'>sales_avg - p_store_cost_old/12.0</column>
                                <column name='cost'>cost - p_store_cost_old</column>
                                <column name='cost_avg'>cost_avg - p_store_cost_old/12.0</column>
                                <column name='profit'>profit - (p_store_sales_old - p_store_cost_old)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    product_id = p_product_id_old     AND
                                    the_year = m_the_year AND
                                    month_of_year = m_month_of_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                </if>
            </then>
        </if>

        <!-- Month + 1 [Year + 1] -->
        <if>
            <expr>
                m_month_of_year == 12
            </expr>
            <then>
                <set name='m_month_of_year_prev'>1</set>
                <set name='m_the_year_prev'>m_the_year + 1</set>
            </then>
            <else>
                <set name='m_month_of_year_prev'>m_month_of_year + 1</set>
                <set name='m_the_year_prev'>m_the_year</set>
            </else>
        </if>
        
        <!-- Counter for number of entries in sales_fact_yearmonth_* for month + 1 [and year + 1], if 0 will insert, if greater will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_yearmonth_product
            WHERE
                product_id = p_product_id_old     AND
                the_year = m_the_year_prev AND
                month_of_year = m_month_of_year_prev
        </select>
        
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>

                <!-- Select sales_prev and cost_prev from month +1 [and year +1] -->
                <select>
                    <columns>
                        sales, cost
                    </columns>
                    <into>m_sales_prev, m_cost_prev</into>
                    FROM sales_fact_yearmonth_product
                    WHERE 
                        product_id = p_product_id_old       AND
                        the_year = m_the_year AND
                        month_of_year = m_month_of_year
                </select>
                
                <!-- Updates sales_fact_year_month_* for month + 1 [and year + 1] with percentages -->
                <if>
                    <expr>
                        m_sales_prev != 0 AND m_cost_prev != 0
                    </expr>
                    <then>
                        <update table='sales_fact_yearmonth_product'>
                            <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales_old</column>
                            <column name='sales_prev'>sales_prev - p_store_sales_old</column>
                            <column name='cost_prev'>cost_prev - p_store_cost_old</column>
                            <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                            <column name='sales_count_prev'>sales_count_prev - 1</column>
                            <column name='sales_pct'>ROUND(((((sales)/(sales_prev - p_store_sales_old))-1)*100),2)</column>
                            <column name='cost_pct'>ROUND(((((cost)/(cost_prev - p_store_cost_old))-1)*100),2)</column>
                            <where>
                                product_id = p_product_id_old    AND
                                the_year = m_the_year_prev AND
                                month_of_year = m_month_of_year_prev
                            </where>
                        </update>
                    </then>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev != 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_product'>
                                <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales_old</column>
                                <column name='sales_prev'>sales_prev - p_store_sales_old</column>
                                <column name='cost_prev'>cost_prev - p_store_cost_old</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev - 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>ROUND(((((cost)/(cost_prev - p_store_cost_old))-1)*100),2)</column>
                                <where>
                                    product_id = p_product_id_old    AND
                                    the_year = m_the_year_prev AND
                                    month_of_year = m_month_of_year_prev
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev != 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_product'>
                                <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales_old</column>
                                <column name='sales_prev'>sales_prev - p_store_sales_old</column>
                                <column name='cost_prev'>cost_prev - p_store_cost_old</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev - 1</column>
                                <column name='sales_pct'>ROUND(((((sales)/(sales_prev - p_store_sales_old))-1)*100),2)</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    product_id = p_product_id_old    AND
                                    the_year = m_the_year_prev AND
                                    month_of_year = m_month_of_year_prev
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_product'>
                                <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales_old</column>
                                <column name='sales_prev'>sales_prev - p_store_sales_old</column>
                                <column name='cost_prev'>cost_prev - p_store_cost_old</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev - 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    product_id = p_product_id_old    AND
                                    the_year = m_the_year_prev AND
                                    month_of_year = m_month_of_year_prev
                                </where>
                            </update>
                        </then>
                    </else-if>
                </if>
            </then>
        </if>
        
        
        
        
        
        
        
        <!--         NEW       -->
        
        <!-- If promotion is applied, sales are added -->
        <if>
            <expr>
                p_promotion_id_new &gt; 0
            </expr>
            <then>
                <set name='m_promotion_cost'>p_store_sales_new</set>
            </then>
            <else>
                <set name='m_promotion_cost'>0.0</set>
            </else>
        </if>
        
        <!-- Gets month, month number and year -->
        <select>
            <columns>
                month_of_year, the_month, the_year
            </columns>
            <into>m_month_of_year, m_the_month, m_the_year</into>
            FROM time_by_day
            WHERE time_id = p_time_id_new
        </select>

        <!--    CUSTOMER     -->
        
        <!-- Counter for number of entries in sales_fact_month_* for previous year, if greater than 0 will set previous year data into variables, if 0 will insert 0 -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_month_customer
            WHERE
                customer_id = p_customer_id_new     AND
                the_year = m_the_year - 1
        </select>
        
        <!-- Selects unit_sales-prev, sales_prev, cost_prev, promotion_sales_prev and sales_count_prev for actual year -->
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                <select>
                    <columns>
                        unit_sales, sales, cost, promotion_sales, sales_count
                    </columns>
                    <into>m_unit_sales_prev, m_sales_prev, m_cost_prev, m_promotion_sales_prev, m_sales_count_prev</into>
                    FROM sales_fact_month_customer
                    WHERE 
                        customer_id = p_customer_id_new     AND
                        the_year = m_the_year - 1
                </select>
            </then>
            <else>
                <set name='m_unit_sales_prev'>0</set>
                <set name='m_sales_prev'>0.0</set>
                <set name='m_cost_prev'>0.0</set>
                <set name='m_promotion_sales_prev'>0.0</set>
                <set name='m_sales_count_prev'>0</set>
            </else>
        </if>

        <!-- Counter for number of entries in sales_fact_month_* for actual year, if 0 will insert, if greater will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_month_customer
            WHERE
                customer_id = p_customer_id_new     AND
                the_year = m_the_year
        </select>

        <!-- Insert or Update year on sales_fact_month_customer -->
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                
                <if>
                    <expr>
                        m_month_of_year == 1
                    </expr>
                    <then>
                        
                        <update table='sales_fact_month_customer'>
                            <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                            <column name='sales'>sales + p_store_sales_new</column>
                            <column name='sales_jan'>sales_jan + p_store_sales_new</column>
                            <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                            <column name='cost'>cost + p_store_cost_new</column>
                            <column name='cost_jan'>cost_jan + p_store_cost_new</column>
                            <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                            <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                            <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                            <column name='sales_count'>sales_count + 1</column>
                            <where>
                                customer_id = p_customer_id_new     AND
                                the_year = m_the_year
                            </where>
                        </update>
    
                    </then>
                    <else-if>
                        <expr>
                            m_month_of_year == 2
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                                <column name='sales'>sales + p_store_sales_new</column>
                                <column name='sales_feb'>sales_feb + p_store_sales_new</column>
                                <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                                <column name='cost'>cost + p_store_cost_new</column>
                                <column name='cost_feb'>cost_feb + p_store_cost_new</column>
                                <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                                <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    customer_id = p_customer_id_new     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 3
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                                <column name='sales'>sales + p_store_sales_new</column>
                                <column name='sales_mar'>sales_mar + p_store_sales_new</column>
                                <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                                <column name='cost'>cost + p_store_cost_new</column>
                                <column name='cost_mar'>cost_mar + p_store_cost_new</column>
                                <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                                <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    customer_id = p_customer_id_new     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 4
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                                <column name='sales'>sales + p_store_sales_new</column>
                                <column name='sales_apr'>sales_apr + p_store_sales_new</column>
                                <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                                <column name='cost'>cost + p_store_cost_new</column>
                                <column name='cost_apr'>cost_apr + p_store_cost_new</column>
                                <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                                <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    customer_id = p_customer_id_new     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 5
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                                <column name='sales'>sales + p_store_sales_new</column>
                                <column name='sales_may'>sales_may + p_store_sales_new</column>
                                <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                                <column name='cost'>cost + p_store_cost_new</column>
                                <column name='cost_may'>cost_may + p_store_cost_new</column>
                                <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                                <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    customer_id = p_customer_id_new     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 6
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                                <column name='sales'>sales + p_store_sales_new</column>
                                <column name='sales_jun'>sales_jun + p_store_sales_new</column>
                                <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                                <column name='cost'>cost + p_store_cost_new</column>
                                <column name='cost_jun'>cost_jun + p_store_cost_new</column>
                                <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                                <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    customer_id = p_customer_id_new     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 7
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                                <column name='sales'>sales + p_store_sales_new</column>
                                <column name='sales_jul'>sales_jul + p_store_sales_new</column>
                                <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                                <column name='cost'>cost + p_store_cost_new</column>
                                <column name='cost_jul'>cost_jul + p_store_cost_new</column>
                                <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                                <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    customer_id = p_customer_id_new     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 8
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                                <column name='sales'>sales + p_store_sales_new</column>
                                <column name='sales_ago'>sales_ago + p_store_sales_new</column>
                                <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                                <column name='cost'>cost + p_store_cost_new</column>
                                <column name='cost_ago'>cost_ago + p_store_cost_new</column>
                                <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                                <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    customer_id = p_customer_id_new     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 9
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                                <column name='sales'>sales + p_store_sales_new</column>
                                <column name='sales_sep'>sales_sep + p_store_sales_new</column>
                                <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                                <column name='cost'>cost + p_store_cost_new</column>
                                <column name='cost_sep'>cost_sep + p_store_cost_new</column>
                                <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                                <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    customer_id = p_customer_id_new     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 10
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                                <column name='sales'>sales + p_store_sales_new</column>
                                <column name='sales_oct'>sales_oct + p_store_sales_new</column>
                                <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                                <column name='cost'>cost + p_store_cost_new</column>
                                <column name='cost_oct'>cost_oct + p_store_cost_new</column>
                                <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                                <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    customer_id = p_customer_id_new     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 11
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                                <column name='sales'>sales + p_store_sales_new</column>
                                <column name='sales_nov'>sales_nov + p_store_sales_new</column>
                                <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                                <column name='cost'>cost + p_store_cost_new</column>
                                <column name='cost_nov'>cost_nov + p_store_cost_new</column>
                                <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                                <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    customer_id = p_customer_id_new     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 12
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                                <column name='sales'>sales + p_store_sales_new</column>
                                <column name='sales_dec'>sales_dec + p_store_sales_new</column>
                                <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                                <column name='cost'>cost + p_store_cost_new</column>
                                <column name='cost_dec'>cost_dec + p_store_cost_new</column>
                                <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                                <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    customer_id = p_customer_id_new     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                </if>
                
            </then>
            <else>
                <if>
                    <expr>
                        m_month_of_year == 1
                    </expr>
                    <then>
                    
                        <insert table='sales_fact_month_customer'>
                            <column name='customer_id'>p_customer_id_new</column>
                            <column name='the_year'>m_the_year</column>
                            <column name='unit_sales'>p_unit_sales_new</column>
                            <column name='unit_sales_prev'>m_unit_sales_prev</column>
                            <column name='sales'>p_store_sales_new</column>
                            <column name='sales_prev'>m_sales_prev</column>
                            <column name='sales_jan'>p_store_sales_new</column>
                            <column name='sales_avg'>p_store_cost_new/12.0</column>
                            <column name='cost'>p_store_cost_new</column>
                            <column name='cost_jan'>p_store_cost_new</column>
                            <column name='cost_prev'>m_cost_prev</column>
                            <column name='cost_avg'>p_store_cost_new/12.0</column>
                            <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                            <column name='promotion_sales'>m_promotion_cost</column>
                            <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                            <column name='sales_count'>1</column>
                            <column name='sales_count_prev'>m_sales_count_prev</column>
                        </insert>

                    </then>
                    <else-if>
                        <expr>
                            m_month_of_year == 2
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_customer'>
                                <column name='customer_id'>p_customer_id_new</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales_new</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales_new</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_feb'>p_store_sales_new</column>
                                <column name='sales_avg'>p_store_cost_new/12.0</column>
                                <column name='cost'>p_store_cost_new</column>
                                <column name='cost_feb'>p_store_cost_new</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost_new/12.0</column>
                                <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 3
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_customer'>
                                <column name='customer_id'>p_customer_id_new</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales_new</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales_new</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_mar'>p_store_sales_new</column>
                                <column name='sales_avg'>p_store_cost_new/12.0</column>
                                <column name='cost'>p_store_cost_new</column>
                                <column name='cost_mar'>p_store_cost_new</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost_new/12.0</column>
                                <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 4
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_customer'>
                                <column name='customer_id'>p_customer_id_new</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales_new</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales_new</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_apr'>p_store_sales_new</column>
                                <column name='sales_avg'>p_store_cost_new/12.0</column>
                                <column name='cost'>p_store_cost_new</column>
                                <column name='cost_apr'>p_store_cost_new</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost_new/12.0</column>
                                <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 5
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_customer'>
                                <column name='customer_id'>p_customer_id_new</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales_new</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales_new</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_may'>p_store_sales_new</column>
                                <column name='sales_avg'>p_store_cost_new/12.0</column>
                                <column name='cost'>p_store_cost_new</column>
                                <column name='cost_may'>p_store_cost_new</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost_new/12.0</column>
                                <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 6
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_customer'>
                                <column name='customer_id'>p_customer_id_new</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales_new</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales_new</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_jun'>p_store_sales_new</column>
                                <column name='sales_avg'>p_store_cost_new/12.0</column>
                                <column name='cost'>p_store_cost_new</column>
                                <column name='cost_jun'>p_store_cost_new</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost_new/12.0</column>
                                <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 7
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_customer'>
                                <column name='customer_id'>p_customer_id_new</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales_new</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales_new</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_jul'>p_store_sales_new</column>
                                <column name='sales_avg'>p_store_cost_new/12.0</column>
                                <column name='cost'>p_store_cost_new</column>
                                <column name='cost_jul'>p_store_cost_new</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost_new/12.0</column>
                                <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 8
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_customer'>
                                <column name='customer_id'>p_customer_id_new</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales_new</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales_new</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_ago'>p_store_sales_new</column>
                                <column name='sales_avg'>p_store_cost_new/12.0</column>
                                <column name='cost'>p_store_cost_new</column>
                                <column name='cost_ago'>p_store_cost_new</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost_new/12.0</column>
                                <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 9
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_customer'>
                                <column name='customer_id'>p_customer_id_new</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales_new</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales_new</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_sep'>p_store_sales_new</column>
                                <column name='sales_avg'>p_store_cost_new/12.0</column>
                                <column name='cost'>p_store_cost_new</column>
                                <column name='cost_sep'>p_store_cost_new</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost_new/12.0</column>
                                <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 10
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_customer'>
                                <column name='customer_id'>p_customer_id_new</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales_new</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales_new</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_oct'>p_store_sales_new</column>
                                <column name='sales_avg'>p_store_cost_new/12.0</column>
                                <column name='cost'>p_store_cost_new</column>
                                <column name='cost_oct'>p_store_cost_new</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost_new/12.0</column>
                                <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 11
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_customer'>
                                <column name='customer_id'>p_customer_id_new</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales_new</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales_new</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_nov'>p_store_sales_new</column>
                                <column name='sales_avg'>p_store_cost_new/12.0</column>
                                <column name='cost'>p_store_cost_new</column>
                                <column name='cost_nov'>p_store_cost_new</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost_new/12.0</column>
                                <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 12
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_customer'>
                                <column name='customer_id'>p_customer_id_new</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales_new</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales_new</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_dec'>p_store_sales_new</column>
                                <column name='sales_avg'>p_store_cost_new/12.0</column>
                                <column name='cost'>p_store_cost_new</column>
                                <column name='cost_dec'>p_store_cost_new</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost_new/12.0</column>
                                <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                </if>

            </else>
        </if>

        <!-- Updates percentage in sales_fact_month_* for actual year-->
        <if>
            <expr>
                m_sales_prev != 0 AND m_cost_prev != 0
            </expr>
            <then>
                <update table='sales_fact_month_customer'>
                    <column name='sales_pct'>ROUND(((((sales)/(sales_prev))-1)*100),2)</column>
                    <column name='cost_pct'>ROUND(((((cost)/(cost_prev))-1)*100),2)</column>
                    <where>
                        customer_id = p_customer_id_new    AND
                        the_year = m_the_year
                    </where>
                </update>
            </then>
            <else-if>
                <expr>
                    m_sales_prev == 0 AND m_cost_prev != 0
                </expr>
                <then>
                    <update table='sales_fact_month_customer'>
                        <column name='sales_pct'>100</column>
                        <column name='cost_pct'>ROUND(((((cost)/(cost_prev))-1)*100),2)</column>
                        <where>
                            customer_id = p_customer_id_new    AND
                            the_year = m_the_year
                        </where>
                    </update>
                </then>
            </else-if>
            <else-if>
                <expr>
                    m_sales_prev != 0 AND m_cost_prev == 0
                </expr>
                <then>
                    <update table='sales_fact_month_customer'>
                        <column name='sales_pct'>ROUND(((((sales)/(sales_prev))-1)*100),2)</column>
                        <column name='cost_pct'>100</column>
                        <where>
                            customer_id = p_customer_id_new    AND
                            the_year = m_the_year
                        </where>
                    </update>
                </then>
            </else-if>
            <else-if>
                <expr>
                    m_sales_prev == 0 AND m_cost_prev == 0
                </expr>
                <then>
                    <update table='sales_fact_month_customer'>
                        <column name='sales_pct'>100</column>
                        <column name='cost_pct'>100</column>
                        <where>
                            customer_id = p_customer_id_new    AND
                            the_year = m_the_year
                        </where>
                    </update>
                </then>
            </else-if>
        </if>

        <!-- Counter for number of entries in sales_fact_month_* for year + 1, if 0 will insert, if greater will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_month_customer
            WHERE
                customer_id = p_customer_id_new     AND
                the_year = m_the_year + 1
        </select>

        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                <!-- Selects sales_prev and cost_prev to choose the percentage -->
                <select>
                    <columns>
                        sales, cost
                    </columns>
                    <into>m_sales_prev, m_cost_prev</into>
                    FROM sales_fact_month_customer
                    WHERE 
                        customer_id = p_customer_id_new       AND
                        the_year = m_the_year
                </select>
                
                <!-- Updates percentage in actual year -->
                <if>
                    <expr>
                        m_sales_prev != 0 AND m_cost_prev != 0
                    </expr>
                    <then>
                        <update table='sales_fact_month_customer'>
                            <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales_new</column>
                            <column name='sales_prev'>sales_prev + p_store_sales_new</column>
                            <column name='cost_prev'>cost_prev + p_store_cost_new</column>
                            <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                            <column name='sales_count_prev'>sales_count_prev + 1</column>
                            <column name='sales_pct'>ROUND(((((sales)/(sales_prev + p_store_sales_new))+1)*100),2)</column>
                            <column name='cost_pct'>ROUND(((((cost)/(cost_prev + p_store_cost_new))-1)*100),2)</column>
                            <where>
                                customer_id = p_customer_id_new    AND
                                the_year = m_the_year + 1
                            </where>
                        </update>
                    </then>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev != 0
                        </expr>
                        <then>
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales_new</column>
                                <column name='sales_prev'>sales_prev + p_store_sales_new</column>
                                <column name='cost_prev'>cost_prev + p_store_cost_new</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev + 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>ROUND(((((cost)/(cost_prev + p_store_cost_new))-1)*100),2)</column>
                                <where>
                                    customer_id = p_customer_id_new    AND
                                    the_year = m_the_year + 1
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev != 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales_new</column>
                                <column name='sales_prev'>sales_prev + p_store_sales_new</column>
                                <column name='cost_prev'>cost_prev + p_store_cost_new</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev + 1</column>
                                <column name='sales_pct'>ROUND(((((sales)/(sales_prev + p_store_sales_new))-1)*100),2)</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    customer_id = p_customer_id_new    AND
                                    the_year = m_the_year + 1
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales_new</column>
                                <column name='sales_prev'>sales_prev + p_store_sales_new</column>
                                <column name='cost_prev'>cost_prev + p_store_cost_new</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev + 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    customer_id = p_customer_id_new    AND
                                    the_year = m_the_year + 1
                                </where>
                            </update>
                        </then>
                    </else-if>
                </if>
            </then>
        </if>

        <!--         CUSTOMER SALES_FACT_YEARMONTH        -->

        <!-- Month - 1 [Year - 1] -->
        <if>
            <expr>
                m_month_of_year == 1
            </expr>
            <then>
                <set name='m_month_of_year_prev'>12</set>
                <set name='m_the_year_prev'>m_the_year - 1</set>
            </then>
            <else>
                <set name='m_month_of_year_prev'>m_month_of_year - 1</set>
                <set name='m_the_year_prev'>m_the_year</set>
            </else>
        </if>

        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_yearmonth_customer
            WHERE
                customer_id = p_customer_id_new     AND
                the_year = m_the_year_prev AND
                month_of_year = m_month_of_year_prev
        </select>
        
        <!-- Selects unit_sales-prev, sales_prev, cost_prev, promotion_sales_prev and sales_count_prev for actual year -->
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                <select>
                    <columns>
                        unit_sales, sales, cost, promotion_sales, sales_count
                    </columns>
                    <into>m_unit_sales_prev, m_sales_prev, m_cost_prev, m_promotion_sales_prev, m_sales_count_prev</into>
                    FROM sales_fact_yearmonth_customer
                    WHERE 
                        customer_id = p_customer_id_new     AND
                        the_year = m_the_year_prev AND
                        month_of_year = m_month_of_year_prev
                </select>
            </then>
            <else>
                <set name='m_unit_sales_prev'>0</set>
                <set name='m_sales_prev'>0.0</set>
                <set name='m_cost_prev'>0.0</set>
                <set name='m_promotion_sales_prev'>0.0</set>
                <set name='m_sales_count_prev'>0</set>
            </else>
        </if>

        <!-- Counter for number of entries in sales_fact_yearmonth_* for actual year, if 0 will insert, if greater will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_yearmonth_customer
            WHERE
                customer_id = p_customer_id_new     AND
                the_year = m_the_year AND
                month_of_year = m_month_of_year
        </select>
        
        <!-- Insert or Update on sales_fact_yearmonth_customer -->
        <if>
            <expr>
               m_count &gt; 0
            </expr>
            <then>
                <update table='sales_fact_yearmonth_customer'>
                    <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                    <column name='sales'>sales + p_store_sales_new</column>
                    <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                    <column name='cost'>cost + p_store_cost_new</column>
                    <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                    <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                    <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                    <column name='sales_count'>sales_count + 1</column>
                    <where>
                        customer_id = p_customer_id_new     AND
                        the_year = m_the_year AND
                        month_of_year = m_month_of_year
                    </where>
                </update>
            </then>
            <else>
                <insert table='sales_fact_yearmonth_customer'>
                    <column name='customer_id'>p_customer_id_new</column>
                    <column name='the_year'>m_the_year</column>
                    <column name='the_month'>m_the_month</column>
                    <column name='month_of_year'>m_month_of_year</column>
                    <column name='unit_sales'>p_unit_sales_new</column>
                    <column name='unit_sales_prev'>m_unit_sales_prev</column>
                    <column name='sales'>p_store_sales_new</column>
                    <column name='sales_prev'>m_sales_prev</column>
                    <column name='sales_avg'>p_store_cost_new/12.0</column>
                    <column name='cost'>p_store_cost_new</column>
                    <column name='cost_prev'>m_cost_prev</column>
                    <column name='cost_avg'>p_store_cost_new/12.0</column>
                    <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                    <column name='promotion_sales'>m_promotion_cost</column>
                    <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                    <column name='sales_count'>1</column>
                    <column name='sales_count_prev'>m_sales_count_prev</column>
                </insert>
            </else>
        </if>

        <if>
            <expr>
                m_sales_prev != 0 AND m_cost_prev != 0
            </expr>
            <then>
                <update table='sales_fact_yearmonth_customer'>
                    <column name='sales_pct'>ROUND(((((sales)/(sales_prev))-1)*100),2)</column>
                    <column name='cost_pct'>ROUND(((((cost)/(cost_prev))-1)*100),2)</column>
                    <where>
                        customer_id = p_customer_id_new     AND
                        the_year = m_the_year AND
                        month_of_year = m_month_of_year
                    </where>
                </update>
            </then>
            <else-if>
                <expr>
                    m_sales_prev == 0 AND m_cost_prev != 0
                </expr>
                <then>
                    <update table='sales_fact_yearmonth_customer'>
                        <column name='sales_pct'>100</column>
                        <column name='cost_pct'>ROUND(((((cost)/(cost_prev))-1)*100),2)</column>
                        <where>
                            customer_id = p_customer_id_new     AND
                            the_year = m_the_year AND
                            month_of_year = m_month_of_year
                        </where>
                    </update>
                </then>
            </else-if>
            <else-if>
                <expr>
                    m_sales_prev != 0 AND m_cost_prev == 0
                </expr>
                <then>
                    <update table='sales_fact_yearmonth_customer'>
                        <column name='sales_pct'>ROUND(((((sales)/(sales_prev))-1)*100),2)</column>
                        <column name='cost_pct'>100</column>
                        <where>
                            customer_id = p_customer_id_new     AND
                            the_year = m_the_year AND
                            month_of_year = m_month_of_year
                        </where>
                    </update>
                </then>
            </else-if>
            <else-if>
                <expr>
                    m_sales_prev == 0 AND m_cost_prev == 0
                </expr>
                <then>
                    <update table='sales_fact_yearmonth_customer'>
                        <column name='sales_pct'>100</column>
                        <column name='cost_pct'>100</column>
                        <where>
                            customer_id = p_customer_id_new     AND
                            the_year = m_the_year AND
                            month_of_year = m_month_of_year
                        </where>
                    </update>
                </then>
            </else-if>
        </if>

        <!-- Month + 1 [Year + 1] -->
        <if>
            <expr>
                m_month_of_year == 12
            </expr>
            <then>
                <set name='m_month_of_year_prev'>1</set>
                <set name='m_the_year_prev'>m_the_year + 1</set>
            </then>
            <else>
                <set name='m_month_of_year_prev'>m_month_of_year + 1</set>
                <set name='m_the_year_prev'>m_the_year</set>
            </else>
        </if>

        <!-- Counter for number of entries in sales_fact_yearmonth_* for previous year, if greater than 0 will set previous year data into variables, if 0 will insert 0 -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_yearmonth_customer
            WHERE
                customer_id = p_customer_id_new     AND
                the_year = m_the_year_prev AND
                month_of_year = m_month_of_year_prev
        </select>
        
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>

                <!-- Select sales_prev and cost_prev from month +1 [and year +1] -->
                <select>
                    <columns>
                        sales, cost
                    </columns>
                    <into>m_sales_prev, m_cost_prev</into>
                    FROM sales_fact_yearmonth_customer
                    WHERE 
                        customer_id = p_customer_id_new       AND
                        the_year = m_the_year AND
                        month_of_year = m_month_of_year
                </select>
                
                <!-- Updates sales_fact_year_month_* for month + 1 [and year + 1] with percentages -->
                <if>
                    <expr>
                        m_sales_prev != 0 AND m_cost_prev != 0
                    </expr>
                    <then>
                        <update table='sales_fact_yearmonth_customer'>
                            <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales_new</column>
                            <column name='sales_prev'>sales_prev + p_store_sales_new</column>
                            <column name='cost_prev'>cost_prev + p_store_cost_new</column>
                            <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                            <column name='sales_count_prev'>sales_count_prev + 1</column>
                            <column name='sales_pct'>ROUND(((((sales)/(sales_prev + p_store_sales_new))-1)*100),2)</column>
                            <column name='cost_pct'>ROUND(((((cost)/(cost_prev + p_store_cost_new))-1)*100),2)</column>
                            <where>
                                customer_id = p_customer_id_new    AND
                                the_year = m_the_year_prev AND
                                month_of_year = m_month_of_year_prev
                            </where>
                        </update>
                    </then>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev != 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_customer'>
                                <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales_new</column>
                                <column name='sales_prev'>sales_prev + p_store_sales_new</column>
                                <column name='cost_prev'>cost_prev + p_store_cost_new</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev + 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>ROUND(((((cost)/(cost_prev + p_store_cost_new))-1)*100),2)</column>
                                <where>
                                    customer_id = p_customer_id_new    AND
                                    the_year = m_the_year_prev AND
                                    month_of_year = m_month_of_year_prev
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev != 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_customer'>
                                <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales_new</column>
                                <column name='sales_prev'>sales_prev + p_store_sales_new</column>
                                <column name='cost_prev'>cost_prev + p_store_cost_new</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev + 1</column>
                                <column name='sales_pct'>ROUND(((((sales)/(sales_prev + p_store_sales_new))-1)*100),2)</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    customer_id = p_customer_id_new    AND
                                    the_year = m_the_year_prev AND
                                    month_of_year = m_month_of_year_prev
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_customer'>
                                <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales_new</column>
                                <column name='sales_prev'>sales_prev + p_store_sales_new</column>
                                <column name='cost_prev'>cost_prev + p_store_cost_new</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev + 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    customer_id = p_customer_id_new    AND
                                    the_year = m_the_year_prev AND
                                    month_of_year = m_month_of_year_prev
                                </where>
                            </update>
                        </then>
                    </else-if>
                </if>
            </then>
        </if>
        <!--    store     -->
        
        <!-- Counter for number of entries in sales_fact_month_* for previous year, if greater than 0 will set previous year data into variables, if 0 will insert 0 -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_month_store
            WHERE
                store_id = p_store_id_new     AND
                the_year = m_the_year - 1
        </select>
        
        <!-- Selects unit_sales-prev, sales_prev, cost_prev, promotion_sales_prev and sales_count_prev for actual year -->
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                <select>
                    <columns>
                        unit_sales, sales, cost, promotion_sales, sales_count
                    </columns>
                    <into>m_unit_sales_prev, m_sales_prev, m_cost_prev, m_promotion_sales_prev, m_sales_count_prev</into>
                    FROM sales_fact_month_store
                    WHERE 
                        store_id = p_store_id_new     AND
                        the_year = m_the_year - 1
                </select>
            </then>
            <else>
                <set name='m_unit_sales_prev'>0</set>
                <set name='m_sales_prev'>0.0</set>
                <set name='m_cost_prev'>0.0</set>
                <set name='m_promotion_sales_prev'>0.0</set>
                <set name='m_sales_count_prev'>0</set>
            </else>
        </if>

        <!-- Counter for number of entries in sales_fact_month_* for actual year, if 0 will insert, if greater will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_month_store
            WHERE
                store_id = p_store_id_new     AND
                the_year = m_the_year
        </select>

        <!-- Insert or Update year on sales_fact_month_store -->
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                
                <if>
                    <expr>
                        m_month_of_year == 1
                    </expr>
                    <then>
                        
                        <update table='sales_fact_month_store'>
                            <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                            <column name='sales'>sales + p_store_sales_new</column>
                            <column name='sales_jan'>sales_jan + p_store_sales_new</column>
                            <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                            <column name='cost'>cost + p_store_cost_new</column>
                            <column name='cost_jan'>cost_jan + p_store_cost_new</column>
                            <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                            <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                            <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                            <column name='sales_count'>sales_count + 1</column>
                            <where>
                                store_id = p_store_id_new     AND
                                the_year = m_the_year
                            </where>
                        </update>
    
                    </then>
                    <else-if>
                        <expr>
                            m_month_of_year == 2
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                                <column name='sales'>sales + p_store_sales_new</column>
                                <column name='sales_feb'>sales_feb + p_store_sales_new</column>
                                <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                                <column name='cost'>cost + p_store_cost_new</column>
                                <column name='cost_feb'>cost_feb + p_store_cost_new</column>
                                <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                                <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    store_id = p_store_id_new     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 3
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                                <column name='sales'>sales + p_store_sales_new</column>
                                <column name='sales_mar'>sales_mar + p_store_sales_new</column>
                                <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                                <column name='cost'>cost + p_store_cost_new</column>
                                <column name='cost_mar'>cost_mar + p_store_cost_new</column>
                                <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                                <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    store_id = p_store_id_new     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 4
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                                <column name='sales'>sales + p_store_sales_new</column>
                                <column name='sales_apr'>sales_apr + p_store_sales_new</column>
                                <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                                <column name='cost'>cost + p_store_cost_new</column>
                                <column name='cost_apr'>cost_apr + p_store_cost_new</column>
                                <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                                <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    store_id = p_store_id_new     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 5
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                                <column name='sales'>sales + p_store_sales_new</column>
                                <column name='sales_may'>sales_may + p_store_sales_new</column>
                                <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                                <column name='cost'>cost + p_store_cost_new</column>
                                <column name='cost_may'>cost_may + p_store_cost_new</column>
                                <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                                <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    store_id = p_store_id_new     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 6
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                                <column name='sales'>sales + p_store_sales_new</column>
                                <column name='sales_jun'>sales_jun + p_store_sales_new</column>
                                <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                                <column name='cost'>cost + p_store_cost_new</column>
                                <column name='cost_jun'>cost_jun + p_store_cost_new</column>
                                <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                                <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    store_id = p_store_id_new     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 7
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                                <column name='sales'>sales + p_store_sales_new</column>
                                <column name='sales_jul'>sales_jul + p_store_sales_new</column>
                                <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                                <column name='cost'>cost + p_store_cost_new</column>
                                <column name='cost_jul'>cost_jul + p_store_cost_new</column>
                                <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                                <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    store_id = p_store_id_new     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 8
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                                <column name='sales'>sales + p_store_sales_new</column>
                                <column name='sales_ago'>sales_ago + p_store_sales_new</column>
                                <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                                <column name='cost'>cost + p_store_cost_new</column>
                                <column name='cost_ago'>cost_ago + p_store_cost_new</column>
                                <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                                <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    store_id = p_store_id_new     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 9
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                                <column name='sales'>sales + p_store_sales_new</column>
                                <column name='sales_sep'>sales_sep + p_store_sales_new</column>
                                <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                                <column name='cost'>cost + p_store_cost_new</column>
                                <column name='cost_sep'>cost_sep + p_store_cost_new</column>
                                <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                                <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    store_id = p_store_id_new     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 10
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                                <column name='sales'>sales + p_store_sales_new</column>
                                <column name='sales_oct'>sales_oct + p_store_sales_new</column>
                                <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                                <column name='cost'>cost + p_store_cost_new</column>
                                <column name='cost_oct'>cost_oct + p_store_cost_new</column>
                                <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                                <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    store_id = p_store_id_new     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 11
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                                <column name='sales'>sales + p_store_sales_new</column>
                                <column name='sales_nov'>sales_nov + p_store_sales_new</column>
                                <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                                <column name='cost'>cost + p_store_cost_new</column>
                                <column name='cost_nov'>cost_nov + p_store_cost_new</column>
                                <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                                <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    store_id = p_store_id_new     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 12
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                                <column name='sales'>sales + p_store_sales_new</column>
                                <column name='sales_dec'>sales_dec + p_store_sales_new</column>
                                <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                                <column name='cost'>cost + p_store_cost_new</column>
                                <column name='cost_dec'>cost_dec + p_store_cost_new</column>
                                <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                                <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    store_id = p_store_id_new     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                </if>
                
            </then>
            <else>
                <if>
                    <expr>
                        m_month_of_year == 1
                    </expr>
                    <then>
                    
                        <insert table='sales_fact_month_store'>
                            <column name='store_id'>p_store_id_new</column>
                            <column name='the_year'>m_the_year</column>
                            <column name='unit_sales'>p_unit_sales_new</column>
                            <column name='unit_sales_prev'>m_unit_sales_prev</column>
                            <column name='sales'>p_store_sales_new</column>
                            <column name='sales_prev'>m_sales_prev</column>
                            <column name='sales_jan'>p_store_sales_new</column>
                            <column name='sales_avg'>p_store_cost_new/12.0</column>
                            <column name='cost'>p_store_cost_new</column>
                            <column name='cost_jan'>p_store_cost_new</column>
                            <column name='cost_prev'>m_cost_prev</column>
                            <column name='cost_avg'>p_store_cost_new/12.0</column>
                            <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                            <column name='promotion_sales'>m_promotion_cost</column>
                            <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                            <column name='sales_count'>1</column>
                            <column name='sales_count_prev'>m_sales_count_prev</column>
                        </insert>

                    </then>
                    <else-if>
                        <expr>
                            m_month_of_year == 2
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_store'>
                                <column name='store_id'>p_store_id_new</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales_new</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales_new</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_feb'>p_store_sales_new</column>
                                <column name='sales_avg'>p_store_cost_new/12.0</column>
                                <column name='cost'>p_store_cost_new</column>
                                <column name='cost_feb'>p_store_cost_new</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost_new/12.0</column>
                                <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 3
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_store'>
                                <column name='store_id'>p_store_id_new</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales_new</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales_new</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_mar'>p_store_sales_new</column>
                                <column name='sales_avg'>p_store_cost_new/12.0</column>
                                <column name='cost'>p_store_cost_new</column>
                                <column name='cost_mar'>p_store_cost_new</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost_new/12.0</column>
                                <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 4
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_store'>
                                <column name='store_id'>p_store_id_new</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales_new</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales_new</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_apr'>p_store_sales_new</column>
                                <column name='sales_avg'>p_store_cost_new/12.0</column>
                                <column name='cost'>p_store_cost_new</column>
                                <column name='cost_apr'>p_store_cost_new</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost_new/12.0</column>
                                <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 5
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_store'>
                                <column name='store_id'>p_store_id_new</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales_new</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales_new</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_may'>p_store_sales_new</column>
                                <column name='sales_avg'>p_store_cost_new/12.0</column>
                                <column name='cost'>p_store_cost_new</column>
                                <column name='cost_may'>p_store_cost_new</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost_new/12.0</column>
                                <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 6
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_store'>
                                <column name='store_id'>p_store_id_new</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales_new</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales_new</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_jun'>p_store_sales_new</column>
                                <column name='sales_avg'>p_store_cost_new/12.0</column>
                                <column name='cost'>p_store_cost_new</column>
                                <column name='cost_jun'>p_store_cost_new</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost_new/12.0</column>
                                <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 7
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_store'>
                                <column name='store_id'>p_store_id_new</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales_new</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales_new</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_jul'>p_store_sales_new</column>
                                <column name='sales_avg'>p_store_cost_new/12.0</column>
                                <column name='cost'>p_store_cost_new</column>
                                <column name='cost_jul'>p_store_cost_new</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost_new/12.0</column>
                                <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 8
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_store'>
                                <column name='store_id'>p_store_id_new</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales_new</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales_new</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_ago'>p_store_sales_new</column>
                                <column name='sales_avg'>p_store_cost_new/12.0</column>
                                <column name='cost'>p_store_cost_new</column>
                                <column name='cost_ago'>p_store_cost_new</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost_new/12.0</column>
                                <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 9
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_store'>
                                <column name='store_id'>p_store_id_new</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales_new</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales_new</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_sep'>p_store_sales_new</column>
                                <column name='sales_avg'>p_store_cost_new/12.0</column>
                                <column name='cost'>p_store_cost_new</column>
                                <column name='cost_sep'>p_store_cost_new</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost_new/12.0</column>
                                <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 10
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_store'>
                                <column name='store_id'>p_store_id_new</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales_new</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales_new</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_oct'>p_store_sales_new</column>
                                <column name='sales_avg'>p_store_cost_new/12.0</column>
                                <column name='cost'>p_store_cost_new</column>
                                <column name='cost_oct'>p_store_cost_new</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost_new/12.0</column>
                                <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 11
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_store'>
                                <column name='store_id'>p_store_id_new</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales_new</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales_new</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_nov'>p_store_sales_new</column>
                                <column name='sales_avg'>p_store_cost_new/12.0</column>
                                <column name='cost'>p_store_cost_new</column>
                                <column name='cost_nov'>p_store_cost_new</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost_new/12.0</column>
                                <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 12
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_store'>
                                <column name='store_id'>p_store_id_new</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales_new</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales_new</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_dec'>p_store_sales_new</column>
                                <column name='sales_avg'>p_store_cost_new/12.0</column>
                                <column name='cost'>p_store_cost_new</column>
                                <column name='cost_dec'>p_store_cost_new</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost_new/12.0</column>
                                <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                </if>

            </else>
        </if>

        <!-- Updates percentage in sales_fact_month_* for actual year-->
        <if>
            <expr>
                m_sales_prev != 0 AND m_cost_prev != 0
            </expr>
            <then>
                <update table='sales_fact_month_store'>
                    <column name='sales_pct'>ROUND(((((sales)/(sales_prev))-1)*100),2)</column>
                    <column name='cost_pct'>ROUND(((((cost)/(cost_prev))-1)*100),2)</column>
                    <where>
                        store_id = p_store_id_new    AND
                        the_year = m_the_year
                    </where>
                </update>
            </then>
            <else-if>
                <expr>
                    m_sales_prev == 0 AND m_cost_prev != 0
                </expr>
                <then>
                    <update table='sales_fact_month_store'>
                        <column name='sales_pct'>100</column>
                        <column name='cost_pct'>ROUND(((((cost)/(cost_prev))-1)*100),2)</column>
                        <where>
                            store_id = p_store_id_new    AND
                            the_year = m_the_year
                        </where>
                    </update>
                </then>
            </else-if>
            <else-if>
                <expr>
                    m_sales_prev != 0 AND m_cost_prev == 0
                </expr>
                <then>
                    <update table='sales_fact_month_store'>
                        <column name='sales_pct'>ROUND(((((sales)/(sales_prev))-1)*100),2)</column>
                        <column name='cost_pct'>100</column>
                        <where>
                            store_id = p_store_id_new    AND
                            the_year = m_the_year
                        </where>
                    </update>
                </then>
            </else-if>
            <else-if>
                <expr>
                    m_sales_prev == 0 AND m_cost_prev == 0
                </expr>
                <then>
                    <update table='sales_fact_month_store'>
                        <column name='sales_pct'>100</column>
                        <column name='cost_pct'>100</column>
                        <where>
                            store_id = p_store_id_new    AND
                            the_year = m_the_year
                        </where>
                    </update>
                </then>
            </else-if>
        </if>

        <!-- Counter for number of entries in sales_fact_month_* for year + 1, if 0 will insert, if greater will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_month_store
            WHERE
                store_id = p_store_id_new     AND
                the_year = m_the_year + 1
        </select>

        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                <!-- Selects sales_prev and cost_prev to choose the percentage -->
                <select>
                    <columns>
                        sales, cost
                    </columns>
                    <into>m_sales_prev, m_cost_prev</into>
                    FROM sales_fact_month_store
                    WHERE 
                        store_id = p_store_id_new       AND
                        the_year = m_the_year
                </select>
                
                <!-- Updates percentage in actual year -->
                <if>
                    <expr>
                        m_sales_prev != 0 AND m_cost_prev != 0
                    </expr>
                    <then>
                        <update table='sales_fact_month_store'>
                            <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales_new</column>
                            <column name='sales_prev'>sales_prev + p_store_sales_new</column>
                            <column name='cost_prev'>cost_prev + p_store_cost_new</column>
                            <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                            <column name='sales_count_prev'>sales_count_prev + 1</column>
                            <column name='sales_pct'>ROUND(((((sales)/(sales_prev + p_store_sales_new))+1)*100),2)</column>
                            <column name='cost_pct'>ROUND(((((cost)/(cost_prev + p_store_cost_new))-1)*100),2)</column>
                            <where>
                                store_id = p_store_id_new    AND
                                the_year = m_the_year + 1
                            </where>
                        </update>
                    </then>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev != 0
                        </expr>
                        <then>
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales_new</column>
                                <column name='sales_prev'>sales_prev + p_store_sales_new</column>
                                <column name='cost_prev'>cost_prev + p_store_cost_new</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev + 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>ROUND(((((cost)/(cost_prev + p_store_cost_new))-1)*100),2)</column>
                                <where>
                                    store_id = p_store_id_new    AND
                                    the_year = m_the_year + 1
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev != 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales_new</column>
                                <column name='sales_prev'>sales_prev + p_store_sales_new</column>
                                <column name='cost_prev'>cost_prev + p_store_cost_new</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev + 1</column>
                                <column name='sales_pct'>ROUND(((((sales)/(sales_prev + p_store_sales_new))-1)*100),2)</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    store_id = p_store_id_new    AND
                                    the_year = m_the_year + 1
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales_new</column>
                                <column name='sales_prev'>sales_prev + p_store_sales_new</column>
                                <column name='cost_prev'>cost_prev + p_store_cost_new</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev + 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    store_id = p_store_id_new    AND
                                    the_year = m_the_year + 1
                                </where>
                            </update>
                        </then>
                    </else-if>
                </if>
            </then>
        </if>

        <!--         store SALES_FACT_YEARMONTH        -->

        <!-- Month - 1 [Year - 1] -->
        <if>
            <expr>
                m_month_of_year == 1
            </expr>
            <then>
                <set name='m_month_of_year_prev'>12</set>
                <set name='m_the_year_prev'>m_the_year - 1</set>
            </then>
            <else>
                <set name='m_month_of_year_prev'>m_month_of_year - 1</set>
                <set name='m_the_year_prev'>m_the_year</set>
            </else>
        </if>

        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_yearmonth_store
            WHERE
                store_id = p_store_id_new     AND
                the_year = m_the_year_prev AND
                month_of_year = m_month_of_year_prev
        </select>
        
        <!-- Selects unit_sales-prev, sales_prev, cost_prev, promotion_sales_prev and sales_count_prev for actual year -->
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                <select>
                    <columns>
                        unit_sales, sales, cost, promotion_sales, sales_count
                    </columns>
                    <into>m_unit_sales_prev, m_sales_prev, m_cost_prev, m_promotion_sales_prev, m_sales_count_prev</into>
                    FROM sales_fact_yearmonth_store
                    WHERE 
                        store_id = p_store_id_new     AND
                        the_year = m_the_year_prev AND
                        month_of_year = m_month_of_year_prev
                </select>
            </then>
            <else>
                <set name='m_unit_sales_prev'>0</set>
                <set name='m_sales_prev'>0.0</set>
                <set name='m_cost_prev'>0.0</set>
                <set name='m_promotion_sales_prev'>0.0</set>
                <set name='m_sales_count_prev'>0</set>
            </else>
        </if>

        <!-- Counter for number of entries in sales_fact_yearmonth_* for actual year, if 0 will insert, if greater will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_yearmonth_store
            WHERE
                store_id = p_store_id_new     AND
                the_year = m_the_year AND
                month_of_year = m_month_of_year
        </select>
        
        <!-- Insert or Update on sales_fact_yearmonth_store -->
        <if>
            <expr>
               m_count &gt; 0
            </expr>
            <then>
                <update table='sales_fact_yearmonth_store'>
                    <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                    <column name='sales'>sales + p_store_sales_new</column>
                    <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                    <column name='cost'>cost + p_store_cost_new</column>
                    <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                    <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                    <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                    <column name='sales_count'>sales_count + 1</column>
                    <where>
                        store_id = p_store_id_new     AND
                        the_year = m_the_year AND
                        month_of_year = m_month_of_year
                    </where>
                </update>
            </then>
            <else>
                <insert table='sales_fact_yearmonth_store'>
                    <column name='store_id'>p_store_id_new</column>
                    <column name='the_year'>m_the_year</column>
                    <column name='the_month'>m_the_month</column>
                    <column name='month_of_year'>m_month_of_year</column>
                    <column name='unit_sales'>p_unit_sales_new</column>
                    <column name='unit_sales_prev'>m_unit_sales_prev</column>
                    <column name='sales'>p_store_sales_new</column>
                    <column name='sales_prev'>m_sales_prev</column>
                    <column name='sales_avg'>p_store_cost_new/12.0</column>
                    <column name='cost'>p_store_cost_new</column>
                    <column name='cost_prev'>m_cost_prev</column>
                    <column name='cost_avg'>p_store_cost_new/12.0</column>
                    <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                    <column name='promotion_sales'>m_promotion_cost</column>
                    <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                    <column name='sales_count'>1</column>
                    <column name='sales_count_prev'>m_sales_count_prev</column>
                </insert>
            </else>
        </if>

        <if>
            <expr>
                m_sales_prev != 0 AND m_cost_prev != 0
            </expr>
            <then>
                <update table='sales_fact_yearmonth_store'>
                    <column name='sales_pct'>ROUND(((((sales)/(sales_prev))-1)*100),2)</column>
                    <column name='cost_pct'>ROUND(((((cost)/(cost_prev))-1)*100),2)</column>
                    <where>
                        store_id = p_store_id_new     AND
                        the_year = m_the_year AND
                        month_of_year = m_month_of_year
                    </where>
                </update>
            </then>
            <else-if>
                <expr>
                    m_sales_prev == 0 AND m_cost_prev != 0
                </expr>
                <then>
                    <update table='sales_fact_yearmonth_store'>
                        <column name='sales_pct'>100</column>
                        <column name='cost_pct'>ROUND(((((cost)/(cost_prev))-1)*100),2)</column>
                        <where>
                            store_id = p_store_id_new     AND
                            the_year = m_the_year AND
                            month_of_year = m_month_of_year
                        </where>
                    </update>
                </then>
            </else-if>
            <else-if>
                <expr>
                    m_sales_prev != 0 AND m_cost_prev == 0
                </expr>
                <then>
                    <update table='sales_fact_yearmonth_store'>
                        <column name='sales_pct'>ROUND(((((sales)/(sales_prev))-1)*100),2)</column>
                        <column name='cost_pct'>100</column>
                        <where>
                            store_id = p_store_id_new     AND
                            the_year = m_the_year AND
                            month_of_year = m_month_of_year
                        </where>
                    </update>
                </then>
            </else-if>
            <else-if>
                <expr>
                    m_sales_prev == 0 AND m_cost_prev == 0
                </expr>
                <then>
                    <update table='sales_fact_yearmonth_store'>
                        <column name='sales_pct'>100</column>
                        <column name='cost_pct'>100</column>
                        <where>
                            store_id = p_store_id_new     AND
                            the_year = m_the_year AND
                            month_of_year = m_month_of_year
                        </where>
                    </update>
                </then>
            </else-if>
        </if>

        <!-- Month + 1 [Year + 1] -->
        <if>
            <expr>
                m_month_of_year == 12
            </expr>
            <then>
                <set name='m_month_of_year_prev'>1</set>
                <set name='m_the_year_prev'>m_the_year + 1</set>
            </then>
            <else>
                <set name='m_month_of_year_prev'>m_month_of_year + 1</set>
                <set name='m_the_year_prev'>m_the_year</set>
            </else>
        </if>

        <!-- Counter for number of entries in sales_fact_yearmonth_* for previous year, if greater than 0 will set previous year data into variables, if 0 will insert 0 -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_yearmonth_store
            WHERE
                store_id = p_store_id_new     AND
                the_year = m_the_year_prev AND
                month_of_year = m_month_of_year_prev
        </select>
        
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>

                <!-- Select sales_prev and cost_prev from month +1 [and year +1] -->
                <select>
                    <columns>
                        sales, cost
                    </columns>
                    <into>m_sales_prev, m_cost_prev</into>
                    FROM sales_fact_yearmonth_store
                    WHERE 
                        store_id = p_store_id_new       AND
                        the_year = m_the_year AND
                        month_of_year = m_month_of_year
                </select>
                
                <!-- Updates sales_fact_year_month_* for month + 1 [and year + 1] with percentages -->
                <if>
                    <expr>
                        m_sales_prev != 0 AND m_cost_prev != 0
                    </expr>
                    <then>
                        <update table='sales_fact_yearmonth_store'>
                            <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales_new</column>
                            <column name='sales_prev'>sales_prev + p_store_sales_new</column>
                            <column name='cost_prev'>cost_prev + p_store_cost_new</column>
                            <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                            <column name='sales_count_prev'>sales_count_prev + 1</column>
                            <column name='sales_pct'>ROUND(((((sales)/(sales_prev + p_store_sales_new))-1)*100),2)</column>
                            <column name='cost_pct'>ROUND(((((cost)/(cost_prev + p_store_cost_new))-1)*100),2)</column>
                            <where>
                                store_id = p_store_id_new    AND
                                the_year = m_the_year_prev AND
                                month_of_year = m_month_of_year_prev
                            </where>
                        </update>
                    </then>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev != 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_store'>
                                <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales_new</column>
                                <column name='sales_prev'>sales_prev + p_store_sales_new</column>
                                <column name='cost_prev'>cost_prev + p_store_cost_new</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev + 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>ROUND(((((cost)/(cost_prev + p_store_cost_new))-1)*100),2)</column>
                                <where>
                                    store_id = p_store_id_new    AND
                                    the_year = m_the_year_prev AND
                                    month_of_year = m_month_of_year_prev
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev != 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_store'>
                                <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales_new</column>
                                <column name='sales_prev'>sales_prev + p_store_sales_new</column>
                                <column name='cost_prev'>cost_prev + p_store_cost_new</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev + 1</column>
                                <column name='sales_pct'>ROUND(((((sales)/(sales_prev + p_store_sales_new))-1)*100),2)</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    store_id = p_store_id_new    AND
                                    the_year = m_the_year_prev AND
                                    month_of_year = m_month_of_year_prev
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_store'>
                                <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales_new</column>
                                <column name='sales_prev'>sales_prev + p_store_sales_new</column>
                                <column name='cost_prev'>cost_prev + p_store_cost_new</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev + 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    store_id = p_store_id_new    AND
                                    the_year = m_the_year_prev AND
                                    month_of_year = m_month_of_year_prev
                                </where>
                            </update>
                        </then>
                    </else-if>
                </if>
            </then>
        </if>
        
        <!--    product     -->
        
        <!-- Counter for number of entries in sales_fact_month_* for previous year, if greater than 0 will set previous year data into variables, if 0 will insert 0 -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_month_product
            WHERE
                product_id = p_product_id_new     AND
                the_year = m_the_year - 1
        </select>
        
        <!-- Selects unit_sales-prev, sales_prev, cost_prev, promotion_sales_prev and sales_count_prev for actual year -->
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                <select>
                    <columns>
                        unit_sales, sales, cost, promotion_sales, sales_count
                    </columns>
                    <into>m_unit_sales_prev, m_sales_prev, m_cost_prev, m_promotion_sales_prev, m_sales_count_prev</into>
                    FROM sales_fact_month_product
                    WHERE 
                        product_id = p_product_id_new     AND
                        the_year = m_the_year - 1
                </select>
            </then>
            <else>
                <set name='m_unit_sales_prev'>0</set>
                <set name='m_sales_prev'>0.0</set>
                <set name='m_cost_prev'>0.0</set>
                <set name='m_promotion_sales_prev'>0.0</set>
                <set name='m_sales_count_prev'>0</set>
            </else>
        </if>

        <!-- Counter for number of entries in sales_fact_month_* for actual year, if 0 will insert, if greater will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_month_product
            WHERE
                product_id = p_product_id_new     AND
                the_year = m_the_year
        </select>

        <!-- Insert or Update year on sales_fact_month_product -->
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                
                <if>
                    <expr>
                        m_month_of_year == 1
                    </expr>
                    <then>
                        
                        <update table='sales_fact_month_product'>
                            <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                            <column name='sales'>sales + p_store_sales_new</column>
                            <column name='sales_jan'>sales_jan + p_store_sales_new</column>
                            <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                            <column name='cost'>cost + p_store_cost_new</column>
                            <column name='cost_jan'>cost_jan + p_store_cost_new</column>
                            <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                            <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                            <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                            <column name='sales_count'>sales_count + 1</column>
                            <where>
                                product_id = p_product_id_new     AND
                                the_year = m_the_year
                            </where>
                        </update>
    
                    </then>
                    <else-if>
                        <expr>
                            m_month_of_year == 2
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                                <column name='sales'>sales + p_store_sales_new</column>
                                <column name='sales_feb'>sales_feb + p_store_sales_new</column>
                                <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                                <column name='cost'>cost + p_store_cost_new</column>
                                <column name='cost_feb'>cost_feb + p_store_cost_new</column>
                                <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                                <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    product_id = p_product_id_new     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 3
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                                <column name='sales'>sales + p_store_sales_new</column>
                                <column name='sales_mar'>sales_mar + p_store_sales_new</column>
                                <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                                <column name='cost'>cost + p_store_cost_new</column>
                                <column name='cost_mar'>cost_mar + p_store_cost_new</column>
                                <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                                <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    product_id = p_product_id_new     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 4
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                                <column name='sales'>sales + p_store_sales_new</column>
                                <column name='sales_apr'>sales_apr + p_store_sales_new</column>
                                <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                                <column name='cost'>cost + p_store_cost_new</column>
                                <column name='cost_apr'>cost_apr + p_store_cost_new</column>
                                <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                                <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    product_id = p_product_id_new     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 5
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                                <column name='sales'>sales + p_store_sales_new</column>
                                <column name='sales_may'>sales_may + p_store_sales_new</column>
                                <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                                <column name='cost'>cost + p_store_cost_new</column>
                                <column name='cost_may'>cost_may + p_store_cost_new</column>
                                <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                                <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    product_id = p_product_id_new     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 6
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                                <column name='sales'>sales + p_store_sales_new</column>
                                <column name='sales_jun'>sales_jun + p_store_sales_new</column>
                                <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                                <column name='cost'>cost + p_store_cost_new</column>
                                <column name='cost_jun'>cost_jun + p_store_cost_new</column>
                                <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                                <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    product_id = p_product_id_new     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 7
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                                <column name='sales'>sales + p_store_sales_new</column>
                                <column name='sales_jul'>sales_jul + p_store_sales_new</column>
                                <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                                <column name='cost'>cost + p_store_cost_new</column>
                                <column name='cost_jul'>cost_jul + p_store_cost_new</column>
                                <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                                <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    product_id = p_product_id_new     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 8
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                                <column name='sales'>sales + p_store_sales_new</column>
                                <column name='sales_ago'>sales_ago + p_store_sales_new</column>
                                <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                                <column name='cost'>cost + p_store_cost_new</column>
                                <column name='cost_ago'>cost_ago + p_store_cost_new</column>
                                <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                                <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    product_id = p_product_id_new     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 9
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                                <column name='sales'>sales + p_store_sales_new</column>
                                <column name='sales_sep'>sales_sep + p_store_sales_new</column>
                                <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                                <column name='cost'>cost + p_store_cost_new</column>
                                <column name='cost_sep'>cost_sep + p_store_cost_new</column>
                                <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                                <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    product_id = p_product_id_new     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 10
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                                <column name='sales'>sales + p_store_sales_new</column>
                                <column name='sales_oct'>sales_oct + p_store_sales_new</column>
                                <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                                <column name='cost'>cost + p_store_cost_new</column>
                                <column name='cost_oct'>cost_oct + p_store_cost_new</column>
                                <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                                <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    product_id = p_product_id_new     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 11
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                                <column name='sales'>sales + p_store_sales_new</column>
                                <column name='sales_nov'>sales_nov + p_store_sales_new</column>
                                <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                                <column name='cost'>cost + p_store_cost_new</column>
                                <column name='cost_nov'>cost_nov + p_store_cost_new</column>
                                <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                                <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    product_id = p_product_id_new     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 12
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                                <column name='sales'>sales + p_store_sales_new</column>
                                <column name='sales_dec'>sales_dec + p_store_sales_new</column>
                                <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                                <column name='cost'>cost + p_store_cost_new</column>
                                <column name='cost_dec'>cost_dec + p_store_cost_new</column>
                                <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                                <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                                <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                                <column name='sales_count'>sales_count + 1</column>
                                <where>
                                    product_id = p_product_id_new     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                </if>
                
            </then>
            <else>
                <if>
                    <expr>
                        m_month_of_year == 1
                    </expr>
                    <then>
                    
                        <insert table='sales_fact_month_product'>
                            <column name='product_id'>p_product_id_new</column>
                            <column name='the_year'>m_the_year</column>
                            <column name='unit_sales'>p_unit_sales_new</column>
                            <column name='unit_sales_prev'>m_unit_sales_prev</column>
                            <column name='sales'>p_store_sales_new</column>
                            <column name='sales_prev'>m_sales_prev</column>
                            <column name='sales_jan'>p_store_sales_new</column>
                            <column name='sales_avg'>p_store_cost_new/12.0</column>
                            <column name='cost'>p_store_cost_new</column>
                            <column name='cost_jan'>p_store_cost_new</column>
                            <column name='cost_prev'>m_cost_prev</column>
                            <column name='cost_avg'>p_store_cost_new/12.0</column>
                            <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                            <column name='promotion_sales'>m_promotion_cost</column>
                            <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                            <column name='sales_count'>1</column>
                            <column name='sales_count_prev'>m_sales_count_prev</column>
                        </insert>

                    </then>
                    <else-if>
                        <expr>
                            m_month_of_year == 2
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_product'>
                                <column name='product_id'>p_product_id_new</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales_new</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales_new</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_feb'>p_store_sales_new</column>
                                <column name='sales_avg'>p_store_cost_new/12.0</column>
                                <column name='cost'>p_store_cost_new</column>
                                <column name='cost_feb'>p_store_cost_new</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost_new/12.0</column>
                                <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 3
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_product'>
                                <column name='product_id'>p_product_id_new</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales_new</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales_new</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_mar'>p_store_sales_new</column>
                                <column name='sales_avg'>p_store_cost_new/12.0</column>
                                <column name='cost'>p_store_cost_new</column>
                                <column name='cost_mar'>p_store_cost_new</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost_new/12.0</column>
                                <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 4
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_product'>
                                <column name='product_id'>p_product_id_new</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales_new</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales_new</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_apr'>p_store_sales_new</column>
                                <column name='sales_avg'>p_store_cost_new/12.0</column>
                                <column name='cost'>p_store_cost_new</column>
                                <column name='cost_apr'>p_store_cost_new</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost_new/12.0</column>
                                <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 5
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_product'>
                                <column name='product_id'>p_product_id_new</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales_new</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales_new</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_may'>p_store_sales_new</column>
                                <column name='sales_avg'>p_store_cost_new/12.0</column>
                                <column name='cost'>p_store_cost_new</column>
                                <column name='cost_may'>p_store_cost_new</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost_new/12.0</column>
                                <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 6
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_product'>
                                <column name='product_id'>p_product_id_new</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales_new</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales_new</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_jun'>p_store_sales_new</column>
                                <column name='sales_avg'>p_store_cost_new/12.0</column>
                                <column name='cost'>p_store_cost_new</column>
                                <column name='cost_jun'>p_store_cost_new</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost_new/12.0</column>
                                <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 7
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_product'>
                                <column name='product_id'>p_product_id_new</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales_new</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales_new</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_jul'>p_store_sales_new</column>
                                <column name='sales_avg'>p_store_cost_new/12.0</column>
                                <column name='cost'>p_store_cost_new</column>
                                <column name='cost_jul'>p_store_cost_new</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost_new/12.0</column>
                                <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 8
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_product'>
                                <column name='product_id'>p_product_id_new</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales_new</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales_new</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_ago'>p_store_sales_new</column>
                                <column name='sales_avg'>p_store_cost_new/12.0</column>
                                <column name='cost'>p_store_cost_new</column>
                                <column name='cost_ago'>p_store_cost_new</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost_new/12.0</column>
                                <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 9
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_product'>
                                <column name='product_id'>p_product_id_new</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales_new</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales_new</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_sep'>p_store_sales_new</column>
                                <column name='sales_avg'>p_store_cost_new/12.0</column>
                                <column name='cost'>p_store_cost_new</column>
                                <column name='cost_sep'>p_store_cost_new</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost_new/12.0</column>
                                <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 10
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_product'>
                                <column name='product_id'>p_product_id_new</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales_new</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales_new</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_oct'>p_store_sales_new</column>
                                <column name='sales_avg'>p_store_cost_new/12.0</column>
                                <column name='cost'>p_store_cost_new</column>
                                <column name='cost_oct'>p_store_cost_new</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost_new/12.0</column>
                                <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 11
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_product'>
                                <column name='product_id'>p_product_id_new</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales_new</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales_new</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_nov'>p_store_sales_new</column>
                                <column name='sales_avg'>p_store_cost_new/12.0</column>
                                <column name='cost'>p_store_cost_new</column>
                                <column name='cost_nov'>p_store_cost_new</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost_new/12.0</column>
                                <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 12
                        </expr>
                        <then>
                        
                            <insert table='sales_fact_month_product'>
                                <column name='product_id'>p_product_id_new</column>
                                <column name='the_year'>m_the_year</column>
                                <column name='unit_sales'>p_unit_sales_new</column>
                                <column name='unit_sales_prev'>m_unit_sales_prev</column>
                                <column name='sales'>p_store_sales_new</column>
                                <column name='sales_prev'>m_sales_prev</column>
                                <column name='sales_dec'>p_store_sales_new</column>
                                <column name='sales_avg'>p_store_cost_new/12.0</column>
                                <column name='cost'>p_store_cost_new</column>
                                <column name='cost_dec'>p_store_cost_new</column>
                                <column name='cost_prev'>m_cost_prev</column>
                                <column name='cost_avg'>p_store_cost_new/12.0</column>
                                <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                                <column name='promotion_sales'>m_promotion_cost</column>
                                <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                                <column name='sales_count'>1</column>
                                <column name='sales_count_prev'>m_sales_count_prev</column>
                            </insert>
                        
                        </then>
                    </else-if>
                </if>

            </else>
        </if>

        <!-- Updates percentage in sales_fact_month_* for actual year-->
        <if>
            <expr>
                m_sales_prev != 0 AND m_cost_prev != 0
            </expr>
            <then>
                <update table='sales_fact_month_product'>
                    <column name='sales_pct'>ROUND(((((sales)/(sales_prev))-1)*100),2)</column>
                    <column name='cost_pct'>ROUND(((((cost)/(cost_prev))-1)*100),2)</column>
                    <where>
                        product_id = p_product_id_new    AND
                        the_year = m_the_year
                    </where>
                </update>
            </then>
            <else-if>
                <expr>
                    m_sales_prev == 0 AND m_cost_prev != 0
                </expr>
                <then>
                    <update table='sales_fact_month_product'>
                        <column name='sales_pct'>100</column>
                        <column name='cost_pct'>ROUND(((((cost)/(cost_prev))-1)*100),2)</column>
                        <where>
                            product_id = p_product_id_new    AND
                            the_year = m_the_year
                        </where>
                    </update>
                </then>
            </else-if>
            <else-if>
                <expr>
                    m_sales_prev != 0 AND m_cost_prev == 0
                </expr>
                <then>
                    <update table='sales_fact_month_product'>
                        <column name='sales_pct'>ROUND(((((sales)/(sales_prev))-1)*100),2)</column>
                        <column name='cost_pct'>100</column>
                        <where>
                            product_id = p_product_id_new    AND
                            the_year = m_the_year
                        </where>
                    </update>
                </then>
            </else-if>
            <else-if>
                <expr>
                    m_sales_prev == 0 AND m_cost_prev == 0
                </expr>
                <then>
                    <update table='sales_fact_month_product'>
                        <column name='sales_pct'>100</column>
                        <column name='cost_pct'>100</column>
                        <where>
                            product_id = p_product_id_new    AND
                            the_year = m_the_year
                        </where>
                    </update>
                </then>
            </else-if>
        </if>

        <!-- Counter for number of entries in sales_fact_month_* for year + 1, if 0 will insert, if greater will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_month_product
            WHERE
                product_id = p_product_id_new     AND
                the_year = m_the_year + 1
        </select>

        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                <!-- Selects sales_prev and cost_prev to choose the percentage -->
                <select>
                    <columns>
                        sales, cost
                    </columns>
                    <into>m_sales_prev, m_cost_prev</into>
                    FROM sales_fact_month_product
                    WHERE 
                        product_id = p_product_id_new       AND
                        the_year = m_the_year
                </select>
                
                <!-- Updates percentage in actual year -->
                <if>
                    <expr>
                        m_sales_prev != 0 AND m_cost_prev != 0
                    </expr>
                    <then>
                        <update table='sales_fact_month_product'>
                            <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales_new</column>
                            <column name='sales_prev'>sales_prev + p_store_sales_new</column>
                            <column name='cost_prev'>cost_prev + p_store_cost_new</column>
                            <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                            <column name='sales_count_prev'>sales_count_prev + 1</column>
                            <column name='sales_pct'>ROUND(((((sales)/(sales_prev + p_store_sales_new))+1)*100),2)</column>
                            <column name='cost_pct'>ROUND(((((cost)/(cost_prev + p_store_cost_new))-1)*100),2)</column>
                            <where>
                                product_id = p_product_id_new    AND
                                the_year = m_the_year + 1
                            </where>
                        </update>
                    </then>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev != 0
                        </expr>
                        <then>
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales_new</column>
                                <column name='sales_prev'>sales_prev + p_store_sales_new</column>
                                <column name='cost_prev'>cost_prev + p_store_cost_new</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev + 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>ROUND(((((cost)/(cost_prev + p_store_cost_new))-1)*100),2)</column>
                                <where>
                                    product_id = p_product_id_new    AND
                                    the_year = m_the_year + 1
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev != 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales_new</column>
                                <column name='sales_prev'>sales_prev + p_store_sales_new</column>
                                <column name='cost_prev'>cost_prev + p_store_cost_new</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev + 1</column>
                                <column name='sales_pct'>ROUND(((((sales)/(sales_prev + p_store_sales_new))-1)*100),2)</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    product_id = p_product_id_new    AND
                                    the_year = m_the_year + 1
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales_new</column>
                                <column name='sales_prev'>sales_prev + p_store_sales_new</column>
                                <column name='cost_prev'>cost_prev + p_store_cost_new</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev + 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    product_id = p_product_id_new    AND
                                    the_year = m_the_year + 1
                                </where>
                            </update>
                        </then>
                    </else-if>
                </if>
            </then>
        </if>

        <!--         product SALES_FACT_YEARMONTH        -->

        <!-- Month - 1 [Year - 1] -->
        <if>
            <expr>
                m_month_of_year == 1
            </expr>
            <then>
                <set name='m_month_of_year_prev'>12</set>
                <set name='m_the_year_prev'>m_the_year - 1</set>
            </then>
            <else>
                <set name='m_month_of_year_prev'>m_month_of_year - 1</set>
                <set name='m_the_year_prev'>m_the_year</set>
            </else>
        </if>

        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_yearmonth_product
            WHERE
                product_id = p_product_id_new     AND
                the_year = m_the_year_prev AND
                month_of_year = m_month_of_year_prev
        </select>
        
        <!-- Selects unit_sales-prev, sales_prev, cost_prev, promotion_sales_prev and sales_count_prev for actual year -->
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                <select>
                    <columns>
                        unit_sales, sales, cost, promotion_sales, sales_count
                    </columns>
                    <into>m_unit_sales_prev, m_sales_prev, m_cost_prev, m_promotion_sales_prev, m_sales_count_prev</into>
                    FROM sales_fact_yearmonth_product
                    WHERE 
                        product_id = p_product_id_new     AND
                        the_year = m_the_year_prev AND
                        month_of_year = m_month_of_year_prev
                </select>
            </then>
            <else>
                <set name='m_unit_sales_prev'>0</set>
                <set name='m_sales_prev'>0.0</set>
                <set name='m_cost_prev'>0.0</set>
                <set name='m_promotion_sales_prev'>0.0</set>
                <set name='m_sales_count_prev'>0</set>
            </else>
        </if>

        <!-- Counter for number of entries in sales_fact_yearmonth_* for actual year, if 0 will insert, if greater will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_yearmonth_product
            WHERE
                product_id = p_product_id_new     AND
                the_year = m_the_year AND
                month_of_year = m_month_of_year
        </select>
        
        <!-- Insert or Update on sales_fact_yearmonth_product -->
        <if>
            <expr>
               m_count &gt; 0
            </expr>
            <then>
                <update table='sales_fact_yearmonth_product'>
                    <column name='unit_sales'>unit_sales + p_unit_sales_new</column>
                    <column name='sales'>sales + p_store_sales_new</column>
                    <column name='sales_avg'>sales_avg + p_store_cost_new/12.0</column>
                    <column name='cost'>cost + p_store_cost_new</column>
                    <column name='cost_avg'>cost_avg + p_store_cost_new/12.0</column>
                    <column name='profit'>profit + (p_store_sales_new - p_store_cost_new)</column>
                    <column name='promotion_sales'>promotion_sales + m_promotion_cost</column>
                    <column name='sales_count'>sales_count + 1</column>
                    <where>
                        product_id = p_product_id_new     AND
                        the_year = m_the_year AND
                        month_of_year = m_month_of_year
                    </where>
                </update>
            </then>
            <else>
                <insert table='sales_fact_yearmonth_product'>
                    <column name='product_id'>p_product_id_new</column>
                    <column name='the_year'>m_the_year</column>
                    <column name='the_month'>m_the_month</column>
                    <column name='month_of_year'>m_month_of_year</column>
                    <column name='unit_sales'>p_unit_sales_new</column>
                    <column name='unit_sales_prev'>m_unit_sales_prev</column>
                    <column name='sales'>p_store_sales_new</column>
                    <column name='sales_prev'>m_sales_prev</column>
                    <column name='sales_avg'>p_store_cost_new/12.0</column>
                    <column name='cost'>p_store_cost_new</column>
                    <column name='cost_prev'>m_cost_prev</column>
                    <column name='cost_avg'>p_store_cost_new/12.0</column>
                    <column name='profit'>p_store_sales_new - p_store_cost_new</column>
                    <column name='promotion_sales'>m_promotion_cost</column>
                    <column name='promotion_sales_prev'>m_promotion_sales_prev</column>
                    <column name='sales_count'>1</column>
                    <column name='sales_count_prev'>m_sales_count_prev</column>
                </insert>
            </else>
        </if>

        <if>
            <expr>
                m_sales_prev != 0 AND m_cost_prev != 0
            </expr>
            <then>
                <update table='sales_fact_yearmonth_product'>
                    <column name='sales_pct'>ROUND(((((sales)/(sales_prev))-1)*100),2)</column>
                    <column name='cost_pct'>ROUND(((((cost)/(cost_prev))-1)*100),2)</column>
                    <where>
                        product_id = p_product_id_new     AND
                        the_year = m_the_year AND
                        month_of_year = m_month_of_year
                    </where>
                </update>
            </then>
            <else-if>
                <expr>
                    m_sales_prev == 0 AND m_cost_prev != 0
                </expr>
                <then>
                    <update table='sales_fact_yearmonth_product'>
                        <column name='sales_pct'>100</column>
                        <column name='cost_pct'>ROUND(((((cost)/(cost_prev))-1)*100),2)</column>
                        <where>
                            product_id = p_product_id_new     AND
                            the_year = m_the_year AND
                            month_of_year = m_month_of_year
                        </where>
                    </update>
                </then>
            </else-if>
            <else-if>
                <expr>
                    m_sales_prev != 0 AND m_cost_prev == 0
                </expr>
                <then>
                    <update table='sales_fact_yearmonth_product'>
                        <column name='sales_pct'>ROUND(((((sales)/(sales_prev))-1)*100),2)</column>
                        <column name='cost_pct'>100</column>
                        <where>
                            product_id = p_product_id_new     AND
                            the_year = m_the_year AND
                            month_of_year = m_month_of_year
                        </where>
                    </update>
                </then>
            </else-if>
            <else-if>
                <expr>
                    m_sales_prev == 0 AND m_cost_prev == 0
                </expr>
                <then>
                    <update table='sales_fact_yearmonth_product'>
                        <column name='sales_pct'>100</column>
                        <column name='cost_pct'>100</column>
                        <where>
                            product_id = p_product_id_new     AND
                            the_year = m_the_year AND
                            month_of_year = m_month_of_year
                        </where>
                    </update>
                </then>
            </else-if>
        </if>

        <!-- Month + 1 [Year + 1] -->
        <if>
            <expr>
                m_month_of_year == 12
            </expr>
            <then>
                <set name='m_month_of_year_prev'>1</set>
                <set name='m_the_year_prev'>m_the_year + 1</set>
            </then>
            <else>
                <set name='m_month_of_year_prev'>m_month_of_year + 1</set>
                <set name='m_the_year_prev'>m_the_year</set>
            </else>
        </if>

        <!-- Counter for number of entries in sales_fact_yearmonth_* for previous year, if greater than 0 will set previous year data into variables, if 0 will insert 0 -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_yearmonth_product
            WHERE
                product_id = p_product_id_new     AND
                the_year = m_the_year_prev AND
                month_of_year = m_month_of_year_prev
        </select>
        
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>

                <!-- Select sales_prev and cost_prev from month +1 [and year +1] -->
                <select>
                    <columns>
                        sales, cost
                    </columns>
                    <into>m_sales_prev, m_cost_prev</into>
                    FROM sales_fact_yearmonth_product
                    WHERE 
                        product_id = p_product_id_new       AND
                        the_year = m_the_year AND
                        month_of_year = m_month_of_year
                </select>
                
                <!-- Updates sales_fact_year_month_* for month + 1 [and year + 1] with percentages -->
                <if>
                    <expr>
                        m_sales_prev != 0 AND m_cost_prev != 0
                    </expr>
                    <then>
                        <update table='sales_fact_yearmonth_product'>
                            <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales_new</column>
                            <column name='sales_prev'>sales_prev + p_store_sales_new</column>
                            <column name='cost_prev'>cost_prev + p_store_cost_new</column>
                            <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                            <column name='sales_count_prev'>sales_count_prev + 1</column>
                            <column name='sales_pct'>ROUND(((((sales)/(sales_prev + p_store_sales_new))-1)*100),2)</column>
                            <column name='cost_pct'>ROUND(((((cost)/(cost_prev + p_store_cost_new))-1)*100),2)</column>
                            <where>
                                product_id = p_product_id_new    AND
                                the_year = m_the_year_prev AND
                                month_of_year = m_month_of_year_prev
                            </where>
                        </update>
                    </then>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev != 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_product'>
                                <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales_new</column>
                                <column name='sales_prev'>sales_prev + p_store_sales_new</column>
                                <column name='cost_prev'>cost_prev + p_store_cost_new</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev + 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>ROUND(((((cost)/(cost_prev + p_store_cost_new))-1)*100),2)</column>
                                <where>
                                    product_id = p_product_id_new    AND
                                    the_year = m_the_year_prev AND
                                    month_of_year = m_month_of_year_prev
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev != 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_product'>
                                <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales_new</column>
                                <column name='sales_prev'>sales_prev + p_store_sales_new</column>
                                <column name='cost_prev'>cost_prev + p_store_cost_new</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev + 1</column>
                                <column name='sales_pct'>ROUND(((((sales)/(sales_prev + p_store_sales_new))-1)*100),2)</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    product_id = p_product_id_new    AND
                                    the_year = m_the_year_prev AND
                                    month_of_year = m_month_of_year_prev
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_product'>
                                <column name='unit_sales_prev'>unit_sales_prev + p_unit_sales_new</column>
                                <column name='sales_prev'>sales_prev + p_store_sales_new</column>
                                <column name='cost_prev'>cost_prev + p_store_cost_new</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev + m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev + 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    product_id = p_product_id_new    AND
                                    the_year = m_the_year_prev AND
                                    month_of_year = m_month_of_year_prev
                                </where>
                            </update>
                        </then>
                    </else-if>
                </if>
            </then>
        </if>
	</body>
</procedure>
-- **************************************************************************
-- sales_fact_act_upd
-- DEISTER WebStudio XSQL-UDFUNC  -  Engine: ids
-- **************************************************************************

3.3 Delete

3.3.1 Trigger

When an entry is deleted in sales_fact; sales_fact_month_customer, sales_fact_month_product and sales_fact_month_store update their corresponding entries and do the same in sales_fact_yearmonth_customer, sales_fact_yearmonth_product and sales_fact_yearmonth_store.

<xsql-trigger
    name='sales_fact_del'
    event='delete'
    table='sales_fact'
>
    <foreach-row>
        <execute-procedure name='sales_fact_act_del'>
            <in>
                <param><prv>product_id</prv></param>
                <param><prv>time_id</prv></param>
                <param><prv>customer_id</prv></param>
                <param><prv>promotion_id</prv></param>
                <param><prv>store_id</prv></param>
                <param><prv>store_sales</prv></param>
                <param><prv>store_cost</prv></param>
                <param><prv>unit_sales</prv></param>
            </in>	
        </execute-procedure>	
    </foreach-row>
</xsql-trigger>
-- **************************************************************************
-- sales_fact_del
-- DEISTER WebStudio XSQL-TRIGGER Wed Nov 25 22:41:32 CET 2020 Engine: ids
-- **************************************************************************

3.3.2 Procedure

When deleting, the afected rows in sales_fact_month_* and sales_fact_yearmonth_* will be updated to the corresponding values, as well as the *_prev columns in the following year or month will be updated to the newer values.

<procedure name='sales_fact_act_del'>
   	
    <args>
        <arg name='p_product_id'                like='sales_fact.product_id'                        />
        <arg name='p_time_id'                   like='sales_fact.time_id'                           />
        <arg name='p_customer_id'               like='sales_fact.customer_id'                       />
        <arg name='p_promotion_id'              like='sales_fact.promotion_id'                      />
        <arg name='p_store_id'                  like='sales_fact.store_id'                          />
        <arg name='p_store_sales'               like='sales_fact.store_sales'                       />
        <arg name='p_store_cost'                like='sales_fact.store_cost'                        />
        <arg name='p_unit_sales'                like='sales_fact.unit_sales'                        />
    </args>
   	
    <define>
        <variable name='m_month_of_year'        like='time_by_day.month_of_year'                    />
        <variable name='m_month_of_year_prev'   like='time_by_day.month_of_year'                    />
        <variable name='m_the_month'            like='time_by_day.the_month'                        />
        <variable name='m_the_month_prev'       like='time_by_day.the_month'                        />
        <variable name='m_the_year'             like='time_by_day.the_year'                         />
        <variable name='m_the_year_prev'        like='time_by_day.the_year'                         />
        <variable name='m_sales_prev'           like='sales_fact_month_customer.sales_prev'         />
        <variable name='m_cost_prev'            like='sales_fact_month_customer.cost_prev'          />
        
        <variable name='m_count'                type='integer'                                      />
        
        <variable name='m_promotion_cost'       like='sales_fact_month_customer.promotion_sales'    />
    </define>

    <body>

        <!-- Gets month, month number and year -->
        <select>
            <columns>
                month_of_year, the_month, the_year
            </columns>
            <into>m_month_of_year, m_the_month, m_the_year</into>
            FROM time_by_day
            WHERE time_id = p_time_id
        </select>

        <!-- If promotion is applied, sales are substracted -->
        <if>
            <expr>
                p_promotion_id &gt; 0
            </expr>
            <then>
                <set name='m_promotion_cost'>p_store_sales</set>
            </then>
            <else>
                <set name='m_promotion_cost'>0.0</set>
            </else>
        </if>
        
        
        
        <!--        CUSTOMER        -->

        <!--         STORE SALES_FACT_MONTH        -->

        <!-- Counter for number of entries in sales_fact_month_* for actual year, if 0 will insert, if greater will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_month_customer
            WHERE
                customer_id = p_customer_id     AND
                the_year = m_the_year
        </select>

        <!-- Update on sales_fact_month_customer for actual year-->
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                
                <if>
                    <expr>
                        m_month_of_year == 1
                    </expr>
                    <then>
                        
                        <update table='sales_fact_month_customer'>
                            <column name='unit_sales'>unit_sales - p_unit_sales</column>
                            <column name='sales'>sales - p_store_sales</column>
                            <column name='sales_jan'>sales_jan - p_store_sales</column>
                            <column name='sales_avg'>sales_avg - p_store_cost/12</column>
                            <column name='cost'>cost - p_store_cost</column>
                            <column name='cost_jan'>cost_jan - p_store_cost</column>
                            <column name='cost_avg'>cost_avg - p_store_cost/12</column>
                            <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                            <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                            <column name='sales_count'>sales_count - 1</column>

                            <where>
                                customer_id = p_customer_id     AND
                                the_year = m_the_year
                            </where>
                        </update>
    
                    </then>
                    <else-if>
                        <expr>
                            m_month_of_year == 2
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_feb'>sales_feb - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_feb'>cost_feb - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    customer_id = p_customer_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 3
                        </expr>
                        <then>
                            
                            
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_mar'>sales_mar - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_mar'>cost_mar - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    customer_id = p_customer_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 4
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_apr'>sales_apr - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_apr'>cost_apr - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    customer_id = p_customer_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 5
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_may'>sales_may - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_may'>cost_may - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    customer_id = p_customer_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 6
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_jun'>sales_jun - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_jun'>cost_jun - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    customer_id = p_customer_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 7
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_jul'>sales_jul - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_jul'>cost_jul - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    customer_id = p_customer_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 8
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_ago'>sales_ago - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_ago'>cost_ago - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    customer_id = p_customer_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 9
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_sep'>sales_sep - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_sep'>cost_sep - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    customer_id = p_customer_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 10
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_oct'>sales_oct - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_oct'>cost_oct - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    customer_id = p_customer_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 11
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_nov'>sales_nov - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_nov'>cost_nov - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    customer_id = p_customer_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 12
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_feb'>sales_feb - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_dec'>cost_dec - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    customer_id = p_customer_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                </if>
                
            </then>
        </if>

        <!-- Counter for number of entries in sales_fact_month_* for year - 1, if greater than 0 will set previous year data into variables, if 0 will insert 0 -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_month_customer
            WHERE
                customer_id = p_customer_id     AND
                the_year = m_the_year - 1
        </select>
        
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                <select>
                    <columns>
                        sales, cost
                    </columns>
                    <into>m_sales_prev, m_cost_prev</into>
                    FROM sales_fact_month_customer
                    WHERE 
                        customer_id = p_customer_id     AND
                        the_year = m_the_year - 1
                </select>
            </then>
            <else>
                <set name='m_sales_prev'>0.0</set>
                <set name='m_cost_prev'>0.0</set>
            </else>
        </if>

        <!-- Updates percentage in sales_fact_month_* actual year -->
        <if>
            <expr>
                m_sales_prev != 0 AND m_cost_prev != 0
            </expr>
            <then>
                <update table='sales_fact_month_customer'>
                    <column name='sales_pct'>ROUND(((((sales)/(sales_prev))-1)*100),2)</column>
                    <column name='cost_pct'>ROUND(((((cost)/(cost_prev))-1)*100),2)</column>
                    <where>
                        customer_id = p_customer_id    AND
                        the_year = m_the_year
                    </where>
                </update>
            </then>
            <else-if>
                <expr>
                    m_sales_prev == 0 AND m_cost_prev != 0
                </expr>
                <then>
                    <update table='sales_fact_month_customer'>
                        <column name='sales_pct'>100</column>
                        <column name='cost_pct'>ROUND(((((cost)/(cost_prev))-1)*100),2)</column>
                        <where>
                            customer_id = p_customer_id    AND
                            the_year = m_the_year
                        </where>
                    </update>
                </then>
            </else-if>
            <else-if>
                <expr>
                    m_sales_prev != 0 AND m_cost_prev == 0
                </expr>
                <then>
                    <update table='sales_fact_month_customer'>
                        <column name='sales_pct'>ROUND(((((sales)/(sales_prev))-1)*100),2)</column>
                        <column name='cost_pct'>100</column>
                        <where>
                            customer_id = p_customer_id    AND
                            the_year = m_the_year
                        </where>
                    </update>
                </then>
            </else-if>
            <else-if>
                <expr>
                    m_sales_prev == 0 AND m_cost_prev == 0
                </expr>
                <then>
                    <update table='sales_fact_month_customer'>
                        <column name='sales_pct'>100</column>
                        <column name='cost_pct'>100</column>
                        <where>
                            customer_id = p_customer_id    AND
                            the_year = m_the_year
                        </where>
                    </update>
                </then>
            </else-if>
        </if>
        
        <!-- Counter for number of entries in sales_fact_month_* for year + 1, if 0 will insert, if greater will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_month_customer
            WHERE
                customer_id = p_customer_id     AND
                the_year = m_the_year + 1
        </select>

        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>

                <!-- Selects sales_prev and cost_prev to choose the percentage -->
                <select>
                    <columns>
                        sales, cost
                    </columns>
                    <into>m_sales_prev, m_cost_prev</into>
                    FROM sales_fact_month_customer
                    WHERE 
                        customer_id = p_customer_id       AND
                        the_year = m_the_year
                </select>
                
                <!-- Updates percentage in actual year -->
                <if>
                    <expr>
                        m_sales_prev != 0 AND m_cost_prev != 0
                    </expr>
                    <then>
                        <update table='sales_fact_month_customer'>
                            <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales</column>
                            <column name='sales_prev'>sales_prev - p_store_sales</column>
                            <column name='cost_prev'>cost_prev - p_store_cost</column>
                            <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                            <column name='sales_count_prev'>sales_count_prev - 1</column>
                            <column name='sales_pct'>ROUND(((((sales)/(sales_prev - p_store_sales))-1)*100),2)</column>
                            <column name='cost_pct'>ROUND(((((cost)/(cost_prev - p_store_cost))-1)*100),2)</column>
                            <where>
                                customer_id = p_customer_id    AND
                                the_year = m_the_year + 1
                            </where>
                        </update>
                    </then>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev != 0
                        </expr>
                        <then>
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales</column>
                                <column name='sales_prev'>sales_prev - p_store_sales</column>
                                <column name='cost_prev'>cost_prev - p_store_cost</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev - 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>ROUND(((((cost)/(cost_prev - p_store_cost))-1)*100),2)</column>
                                <where>
                                    customer_id = p_customer_id    AND
                                    the_year = m_the_year + 1
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev != 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales</column>
                                <column name='sales_prev'>sales_prev - p_store_sales</column>
                                <column name='cost_prev'>cost_prev - p_store_cost</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev - 1</column>
                                <column name='sales_pct'>ROUND(((((sales)/(sales_prev - p_store_sales))-1)*100),2)</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    customer_id = p_customer_id    AND
                                    the_year = m_the_year + 1
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_month_customer'>
                                <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales</column>
                                <column name='sales_prev'>sales_prev - p_store_sales</column>
                                <column name='cost_prev'>cost_prev - p_store_cost</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev - 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    customer_id = p_customer_id    AND
                                    the_year = m_the_year + 1
                                </where>
                            </update>
                        </then>
                    </else-if>
                </if>
            </then>
        </if>

        <!--         CUSTOMER SALES_FACT_YEARMONTH        -->

        <!-- Month - 1 [Year - 1] -->
        <if>
            <expr>
                m_month_of_year == 1
            </expr>
            <then>
                <set name='m_month_of_year_prev'>12</set>
                <set name='m_the_year_prev'>m_the_year - 1</set>
            </then>
            <else>
                <set name='m_month_of_year_prev'>m_month_of_year - 1</set>
                <set name='m_the_year_prev'>m_the_year</set>
            </else>
        </if>
        
        <!-- Counter for number of entries in sales_fact_yearmonth_* for previous year, if greater than 0 will set previous year data into variables, if 0 will insert 0 -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_yearmonth_customer
            WHERE
                customer_id = p_customer_id     AND
                the_year = m_the_year_prev AND
                month_of_year = m_month_of_year_prev
        </select>
        
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                <select>
                    <columns>
                        sales, cost
                    </columns>
                    <into>m_sales_prev, m_cost_prev</into>
                    FROM sales_fact_yearmonth_customer
                    WHERE 
                            customer_id = p_customer_id     AND
                            month_of_year = m_month_of_year_prev AND
                            the_year = m_the_year_prev
                </select>
            </then>
            <else>
                <set name='m_sales_prev'>0.0</set>
                <set name='m_cost_prev'>0.0</set>
            </else>
        </if>

        <!-- Counter for number of entries in sales_fact_yearmonth_* for actual month and year, if greater than 0 will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_yearmonth_customer
            WHERE
                customer_id = p_customer_id     AND
                the_year = m_the_year AND
                month_of_year = m_month_of_year
        </select>

        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                <!-- Updates percentage in sales_fact_month_* -->
                <if>
                    <expr>
                        m_sales_prev != 0 AND m_cost_prev != 0
                    </expr>
                    <then>
                        <update table='sales_fact_yearmonth_customer'>
                            <column name='unit_sales'>unit_sales - p_unit_sales</column>
                            <column name='sales'>sales - p_store_sales</column>
                            <column name='sales_avg'>sales_avg - p_store_cost/12.0</column>
                            <column name='cost'>cost - p_store_cost</column>
                            <column name='cost_avg'>cost_avg - p_store_cost/12.0</column>
                            <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                            <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                            <column name='sales_count'>sales_count - 1</column>
                            <column name='sales_pct'>ROUND(((((sales - p_store_sales)/(sales_prev))-1)*100),2)</column>
                            <column name='cost_pct'>ROUND(((((cost - p_store_cost)/(cost_prev))-1)*100),2)</column>
                            <where>
                                customer_id = p_customer_id     AND
                                the_year = m_the_year AND
                                month_of_year = m_month_of_year
                            </where>
                        </update>
                    </then>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev != 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_customer'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12.0</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12.0</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>ROUND(((((cost - p_store_cost)/(cost_prev))-1)*100),2)</column>
                                <where>
                                    customer_id = p_customer_id     AND
                                    the_year = m_the_year AND
                                    month_of_year = m_month_of_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev != 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_customer'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12.0</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12.0</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>
                                    <column name='sales_pct'>ROUND(((((sales - p_store_sales)/(sales_prev))-1)*100),2)</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    customer_id = p_customer_id     AND
                                    the_year = m_the_year AND
                                    month_of_year = m_month_of_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_customer'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12.0</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12.0</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    customer_id = p_customer_id     AND
                                    the_year = m_the_year AND
                                    month_of_year = m_month_of_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                </if>
            </then>
        </if>

        <!-- Month + 1 [Year + 1] -->
        <if>
            <expr>
                m_month_of_year == 12
            </expr>
            <then>
                <set name='m_month_of_year_prev'>1</set>
                <set name='m_the_year_prev'>m_the_year + 1</set>
            </then>
            <else>
                <set name='m_month_of_year_prev'>m_month_of_year + 1</set>
                <set name='m_the_year_prev'>m_the_year</set>
            </else>
        </if>
        
        <!-- Counter for number of entries in sales_fact_yearmonth_* for month + 1 [and year + 1], if 0 will insert, if greater will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_yearmonth_customer
            WHERE
                customer_id = p_customer_id     AND
                the_year = m_the_year_prev AND
                month_of_year = m_month_of_year_prev
        </select>
        
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>

                <!-- Select sales_prev and cost_prev from month +1 [and year +1] -->
                <select>
                    <columns>
                        sales, cost
                    </columns>
                    <into>m_sales_prev, m_cost_prev</into>
                    FROM sales_fact_yearmonth_customer
                    WHERE 
                        customer_id = p_customer_id       AND
                        the_year = m_the_year AND
                        month_of_year = m_month_of_year
                </select>
                
                <!-- Updates sales_fact_year_month_* for month + 1 [and year + 1] with percentages -->
                <if>
                    <expr>
                        m_sales_prev != 0 AND m_cost_prev != 0
                    </expr>
                    <then>
                        <update table='sales_fact_yearmonth_customer'>
                            <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales</column>
                            <column name='sales_prev'>sales_prev - p_store_sales</column>
                            <column name='cost_prev'>cost_prev - p_store_cost</column>
                            <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                            <column name='sales_count_prev'>sales_count_prev - 1</column>
                            <column name='sales_pct'>ROUND(((((sales)/(sales_prev - p_store_sales))-1)*100),2)</column>
                            <column name='cost_pct'>ROUND(((((cost)/(cost_prev - p_store_cost))-1)*100),2)</column>
                            <where>
                                customer_id = p_customer_id    AND
                                the_year = m_the_year_prev AND
                                month_of_year = m_month_of_year_prev
                            </where>
                        </update>
                    </then>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev != 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_customer'>
                                <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales</column>
                                <column name='sales_prev'>sales_prev - p_store_sales</column>
                                <column name='cost_prev'>cost_prev - p_store_cost</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev - 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>ROUND(((((cost)/(cost_prev - p_store_cost))-1)*100),2)</column>
                                <where>
                                    customer_id = p_customer_id    AND
                                    the_year = m_the_year_prev AND
                                    month_of_year = m_month_of_year_prev
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev != 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_customer'>
                                <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales</column>
                                <column name='sales_prev'>sales_prev - p_store_sales</column>
                                <column name='cost_prev'>cost_prev - p_store_cost</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev - 1</column>
                                <column name='sales_pct'>ROUND(((((sales)/(sales_prev - p_store_sales))-1)*100),2)</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    customer_id = p_customer_id    AND
                                    the_year = m_the_year_prev AND
                                    month_of_year = m_month_of_year_prev
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_customer'>
                                <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales</column>
                                <column name='sales_prev'>sales_prev - p_store_sales</column>
                                <column name='cost_prev'>cost_prev - p_store_cost</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev - 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    customer_id = p_customer_id    AND
                                    the_year = m_the_year_prev AND
                                    month_of_year = m_month_of_year_prev
                                </where>
                            </update>
                        </then>
                    </else-if>
                </if>
            </then>
        </if>
        
        <!--        store        -->

        <!--         STORE SALES_FACT_MONTH        -->

        <!-- Counter for number of entries in sales_fact_month_* for actual year, if 0 will insert, if greater will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_month_store
            WHERE
                store_id = p_store_id     AND
                the_year = m_the_year
        </select>

        <!-- Update on sales_fact_month_store for actual year-->
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                
                <if>
                    <expr>
                        m_month_of_year == 1
                    </expr>
                    <then>
                        
                        <update table='sales_fact_month_store'>
                            <column name='unit_sales'>unit_sales - p_unit_sales</column>
                            <column name='sales'>sales - p_store_sales</column>
                            <column name='sales_jan'>sales_jan - p_store_sales</column>
                            <column name='sales_avg'>sales_avg - p_store_cost/12</column>
                            <column name='cost'>cost - p_store_cost</column>
                            <column name='cost_jan'>cost_jan - p_store_cost</column>
                            <column name='cost_avg'>cost_avg - p_store_cost/12</column>
                            <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                            <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                            <column name='sales_count'>sales_count - 1</column>

                            <where>
                                store_id = p_store_id     AND
                                the_year = m_the_year
                            </where>
                        </update>
    
                    </then>
                    <else-if>
                        <expr>
                            m_month_of_year == 2
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_feb'>sales_feb - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_feb'>cost_feb - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    store_id = p_store_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 3
                        </expr>
                        <then>
                            
                            
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_mar'>sales_mar - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_mar'>cost_mar - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    store_id = p_store_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 4
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_apr'>sales_apr - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_apr'>cost_apr - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    store_id = p_store_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 5
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_may'>sales_may - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_may'>cost_may - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    store_id = p_store_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 6
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_jun'>sales_jun - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_jun'>cost_jun - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    store_id = p_store_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 7
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_jul'>sales_jul - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_jul'>cost_jul - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    store_id = p_store_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 8
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_ago'>sales_ago - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_ago'>cost_ago - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    store_id = p_store_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 9
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_sep'>sales_sep - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_sep'>cost_sep - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    store_id = p_store_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 10
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_oct'>sales_oct - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_oct'>cost_oct - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    store_id = p_store_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 11
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_nov'>sales_nov - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_nov'>cost_nov - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    store_id = p_store_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 12
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_feb'>sales_feb - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_dec'>cost_dec - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    store_id = p_store_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                </if>
                
            </then>
        </if>

        <!-- Counter for number of entries in sales_fact_month_* for year - 1, if greater than 0 will set previous year data into variables, if 0 will insert 0 -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_month_store
            WHERE
                store_id = p_store_id     AND
                the_year = m_the_year - 1
        </select>
        
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                <select>
                    <columns>
                        sales, cost
                    </columns>
                    <into>m_sales_prev, m_cost_prev</into>
                    FROM sales_fact_month_store
                    WHERE 
                        store_id = p_store_id     AND
                        the_year = m_the_year - 1
                </select>
            </then>
            <else>
                <set name='m_sales_prev'>0.0</set>
                <set name='m_cost_prev'>0.0</set>
            </else>
        </if>

        <!-- Updates percentage in sales_fact_month_* actual year -->
        <if>
            <expr>
                m_sales_prev != 0 AND m_cost_prev != 0
            </expr>
            <then>
                <update table='sales_fact_month_store'>
                    <column name='sales_pct'>ROUND(((((sales)/(sales_prev))-1)*100),2)</column>
                    <column name='cost_pct'>ROUND(((((cost)/(cost_prev))-1)*100),2)</column>
                    <where>
                        store_id = p_store_id    AND
                        the_year = m_the_year
                    </where>
                </update>
            </then>
            <else-if>
                <expr>
                    m_sales_prev == 0 AND m_cost_prev != 0
                </expr>
                <then>
                    <update table='sales_fact_month_store'>
                        <column name='sales_pct'>100</column>
                        <column name='cost_pct'>ROUND(((((cost)/(cost_prev))-1)*100),2)</column>
                        <where>
                            store_id = p_store_id    AND
                            the_year = m_the_year
                        </where>
                    </update>
                </then>
            </else-if>
            <else-if>
                <expr>
                    m_sales_prev != 0 AND m_cost_prev == 0
                </expr>
                <then>
                    <update table='sales_fact_month_store'>
                        <column name='sales_pct'>ROUND(((((sales)/(sales_prev))-1)*100),2)</column>
                        <column name='cost_pct'>100</column>
                        <where>
                            store_id = p_store_id    AND
                            the_year = m_the_year
                        </where>
                    </update>
                </then>
            </else-if>
            <else-if>
                <expr>
                    m_sales_prev == 0 AND m_cost_prev == 0
                </expr>
                <then>
                    <update table='sales_fact_month_store'>
                        <column name='sales_pct'>100</column>
                        <column name='cost_pct'>100</column>
                        <where>
                            store_id = p_store_id    AND
                            the_year = m_the_year
                        </where>
                    </update>
                </then>
            </else-if>
        </if>
        
        <!-- Counter for number of entries in sales_fact_month_* for year + 1, if 0 will insert, if greater will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_month_store
            WHERE
                store_id = p_store_id     AND
                the_year = m_the_year + 1
        </select>

        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>

                <!-- Selects sales_prev and cost_prev to choose the percentage -->
                <select>
                    <columns>
                        sales, cost
                    </columns>
                    <into>m_sales_prev, m_cost_prev</into>
                    FROM sales_fact_month_store
                    WHERE 
                        store_id = p_store_id       AND
                        the_year = m_the_year
                </select>
                
                <!-- Updates percentage in actual year -->
                <if>
                    <expr>
                        m_sales_prev != 0 AND m_cost_prev != 0
                    </expr>
                    <then>
                        <update table='sales_fact_month_store'>
                            <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales</column>
                            <column name='sales_prev'>sales_prev - p_store_sales</column>
                            <column name='cost_prev'>cost_prev - p_store_cost</column>
                            <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                            <column name='sales_count_prev'>sales_count_prev - 1</column>
                            <column name='sales_pct'>ROUND(((((sales)/(sales_prev - p_store_sales))-1)*100),2)</column>
                            <column name='cost_pct'>ROUND(((((cost)/(cost_prev - p_store_cost))-1)*100),2)</column>
                            <where>
                                store_id = p_store_id    AND
                                the_year = m_the_year + 1
                            </where>
                        </update>
                    </then>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev != 0
                        </expr>
                        <then>
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales</column>
                                <column name='sales_prev'>sales_prev - p_store_sales</column>
                                <column name='cost_prev'>cost_prev - p_store_cost</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev - 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>ROUND(((((cost)/(cost_prev - p_store_cost))-1)*100),2)</column>
                                <where>
                                    store_id = p_store_id    AND
                                    the_year = m_the_year + 1
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev != 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales</column>
                                <column name='sales_prev'>sales_prev - p_store_sales</column>
                                <column name='cost_prev'>cost_prev - p_store_cost</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev - 1</column>
                                <column name='sales_pct'>ROUND(((((sales)/(sales_prev - p_store_sales))-1)*100),2)</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    store_id = p_store_id    AND
                                    the_year = m_the_year + 1
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_month_store'>
                                <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales</column>
                                <column name='sales_prev'>sales_prev - p_store_sales</column>
                                <column name='cost_prev'>cost_prev - p_store_cost</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev - 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    store_id = p_store_id    AND
                                    the_year = m_the_year + 1
                                </where>
                            </update>
                        </then>
                    </else-if>
                </if>
            </then>
        </if>

        <!--         store SALES_FACT_YEARMONTH        -->

        <!-- Month - 1 [Year - 1] -->
        <if>
            <expr>
                m_month_of_year == 1
            </expr>
            <then>
                <set name='m_month_of_year_prev'>12</set>
                <set name='m_the_year_prev'>m_the_year - 1</set>
            </then>
            <else>
                <set name='m_month_of_year_prev'>m_month_of_year - 1</set>
                <set name='m_the_year_prev'>m_the_year</set>
            </else>
        </if>
        
        <!-- Counter for number of entries in sales_fact_yearmonth_* for previous year, if greater than 0 will set previous year data into variables, if 0 will insert 0 -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_yearmonth_store
            WHERE
                store_id = p_store_id     AND
                the_year = m_the_year_prev AND
                month_of_year = m_month_of_year_prev
        </select>
        
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                <select>
                    <columns>
                        sales, cost
                    </columns>
                    <into>m_sales_prev, m_cost_prev</into>
                    FROM sales_fact_yearmonth_store
                    WHERE 
                            store_id = p_store_id     AND
                            month_of_year = m_month_of_year_prev AND
                            the_year = m_the_year_prev
                </select>
            </then>
            <else>
                <set name='m_sales_prev'>0.0</set>
                <set name='m_cost_prev'>0.0</set>
            </else>
        </if>

        <!-- Counter for number of entries in sales_fact_yearmonth_* for actual month and year, if greater than 0 will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_yearmonth_store
            WHERE
                store_id = p_store_id     AND
                the_year = m_the_year AND
                month_of_year = m_month_of_year
        </select>

        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                <!-- Updates percentage in sales_fact_month_* -->
                <if>
                    <expr>
                        m_sales_prev != 0 AND m_cost_prev != 0
                    </expr>
                    <then>
                        <update table='sales_fact_yearmonth_store'>
                            <column name='unit_sales'>unit_sales - p_unit_sales</column>
                            <column name='sales'>sales - p_store_sales</column>
                            <column name='sales_avg'>sales_avg - p_store_cost/12.0</column>
                            <column name='cost'>cost - p_store_cost</column>
                            <column name='cost_avg'>cost_avg - p_store_cost/12.0</column>
                            <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                            <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                            <column name='sales_count'>sales_count - 1</column>
                            <column name='sales_pct'>ROUND(((((sales - p_store_sales)/(sales_prev))-1)*100),2)</column>
                            <column name='cost_pct'>ROUND(((((cost - p_store_cost)/(cost_prev))-1)*100),2)</column>
                            <where>
                                store_id = p_store_id     AND
                                the_year = m_the_year AND
                                month_of_year = m_month_of_year
                            </where>
                        </update>
                    </then>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev != 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_store'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12.0</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12.0</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>ROUND(((((cost - p_store_cost)/(cost_prev))-1)*100),2)</column>
                                <where>
                                    store_id = p_store_id     AND
                                    the_year = m_the_year AND
                                    month_of_year = m_month_of_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev != 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_store'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12.0</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12.0</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>
                                    <column name='sales_pct'>ROUND(((((sales - p_store_sales)/(sales_prev))-1)*100),2)</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    store_id = p_store_id     AND
                                    the_year = m_the_year AND
                                    month_of_year = m_month_of_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_store'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12.0</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12.0</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    store_id = p_store_id     AND
                                    the_year = m_the_year AND
                                    month_of_year = m_month_of_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                </if>
            </then>
        </if>

        <!-- Month + 1 [Year + 1] -->
        <if>
            <expr>
                m_month_of_year == 12
            </expr>
            <then>
                <set name='m_month_of_year_prev'>1</set>
                <set name='m_the_year_prev'>m_the_year + 1</set>
            </then>
            <else>
                <set name='m_month_of_year_prev'>m_month_of_year + 1</set>
                <set name='m_the_year_prev'>m_the_year</set>
            </else>
        </if>
        
        <!-- Counter for number of entries in sales_fact_yearmonth_* for month + 1 [and year + 1], if 0 will insert, if greater will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_yearmonth_store
            WHERE
                store_id = p_store_id     AND
                the_year = m_the_year_prev AND
                month_of_year = m_month_of_year_prev
        </select>
        
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>

                <!-- Select sales_prev and cost_prev from month +1 [and year +1] -->
                <select>
                    <columns>
                        sales, cost
                    </columns>
                    <into>m_sales_prev, m_cost_prev</into>
                    FROM sales_fact_yearmonth_store
                    WHERE 
                        store_id = p_store_id       AND
                        the_year = m_the_year AND
                        month_of_year = m_month_of_year
                </select>
                
                <!-- Updates sales_fact_year_month_* for month + 1 [and year + 1] with percentages -->
                <if>
                    <expr>
                        m_sales_prev != 0 AND m_cost_prev != 0
                    </expr>
                    <then>
                        <update table='sales_fact_yearmonth_store'>
                            <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales</column>
                            <column name='sales_prev'>sales_prev - p_store_sales</column>
                            <column name='cost_prev'>cost_prev - p_store_cost</column>
                            <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                            <column name='sales_count_prev'>sales_count_prev - 1</column>
                            <column name='sales_pct'>ROUND(((((sales)/(sales_prev - p_store_sales))-1)*100),2)</column>
                            <column name='cost_pct'>ROUND(((((cost)/(cost_prev - p_store_cost))-1)*100),2)</column>
                            <where>
                                store_id = p_store_id    AND
                                the_year = m_the_year_prev AND
                                month_of_year = m_month_of_year_prev
                            </where>
                        </update>
                    </then>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev != 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_store'>
                                <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales</column>
                                <column name='sales_prev'>sales_prev - p_store_sales</column>
                                <column name='cost_prev'>cost_prev - p_store_cost</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev - 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>ROUND(((((cost)/(cost_prev - p_store_cost))-1)*100),2)</column>
                                <where>
                                    store_id = p_store_id    AND
                                    the_year = m_the_year_prev AND
                                    month_of_year = m_month_of_year_prev
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev != 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_store'>
                                <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales</column>
                                <column name='sales_prev'>sales_prev - p_store_sales</column>
                                <column name='cost_prev'>cost_prev - p_store_cost</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev - 1</column>
                                <column name='sales_pct'>ROUND(((((sales)/(sales_prev - p_store_sales))-1)*100),2)</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    store_id = p_store_id    AND
                                    the_year = m_the_year_prev AND
                                    month_of_year = m_month_of_year_prev
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_store'>
                                <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales</column>
                                <column name='sales_prev'>sales_prev - p_store_sales</column>
                                <column name='cost_prev'>cost_prev - p_store_cost</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev - 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    store_id = p_store_id    AND
                                    the_year = m_the_year_prev AND
                                    month_of_year = m_month_of_year_prev
                                </where>
                            </update>
                        </then>
                    </else-if>
                </if>
            </then>
        </if>
        
        <!--        product        -->

        <!--         STORE SALES_FACT_MONTH        -->

        <!-- Counter for number of entries in sales_fact_month_* for actual year, if 0 will insert, if greater will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_month_product
            WHERE
                product_id = p_product_id     AND
                the_year = m_the_year
        </select>

        <!-- Update on sales_fact_month_product for actual year-->
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                
                <if>
                    <expr>
                        m_month_of_year == 1
                    </expr>
                    <then>
                        
                        <update table='sales_fact_month_product'>
                            <column name='unit_sales'>unit_sales - p_unit_sales</column>
                            <column name='sales'>sales - p_store_sales</column>
                            <column name='sales_jan'>sales_jan - p_store_sales</column>
                            <column name='sales_avg'>sales_avg - p_store_cost/12</column>
                            <column name='cost'>cost - p_store_cost</column>
                            <column name='cost_jan'>cost_jan - p_store_cost</column>
                            <column name='cost_avg'>cost_avg - p_store_cost/12</column>
                            <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                            <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                            <column name='sales_count'>sales_count - 1</column>

                            <where>
                                product_id = p_product_id     AND
                                the_year = m_the_year
                            </where>
                        </update>
    
                    </then>
                    <else-if>
                        <expr>
                            m_month_of_year == 2
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_feb'>sales_feb - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_feb'>cost_feb - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    product_id = p_product_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 3
                        </expr>
                        <then>
                            
                            
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_mar'>sales_mar - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_mar'>cost_mar - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    product_id = p_product_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 4
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_apr'>sales_apr - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_apr'>cost_apr - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    product_id = p_product_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 5
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_may'>sales_may - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_may'>cost_may - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    product_id = p_product_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                            
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 6
                        </expr>
                        <then>
                            
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_jun'>sales_jun - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_jun'>cost_jun - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    product_id = p_product_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 7
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_jul'>sales_jul - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_jul'>cost_jul - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    product_id = p_product_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 8
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_ago'>sales_ago - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_ago'>cost_ago - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    product_id = p_product_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 9
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_sep'>sales_sep - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_sep'>cost_sep - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    product_id = p_product_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 10
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_oct'>sales_oct - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_oct'>cost_oct - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    product_id = p_product_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 11
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_nov'>sales_nov - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_nov'>cost_nov - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    product_id = p_product_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_month_of_year == 12
                        </expr>
                        <then>
                        
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_feb'>sales_feb - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_dec'>cost_dec - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>

                                <where>
                                    product_id = p_product_id     AND
                                    the_year = m_the_year
                                </where>
                            </update>
                        
                        </then>
                    </else-if>
                </if>
                
            </then>
        </if>

        <!-- Counter for number of entries in sales_fact_month_* for year - 1, if greater than 0 will set previous year data into variables, if 0 will insert 0 -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_month_product
            WHERE
                product_id = p_product_id     AND
                the_year = m_the_year - 1
        </select>
        
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                <select>
                    <columns>
                        sales, cost
                    </columns>
                    <into>m_sales_prev, m_cost_prev</into>
                    FROM sales_fact_month_product
                    WHERE 
                        product_id = p_product_id     AND
                        the_year = m_the_year - 1
                </select>
            </then>
            <else>
                <set name='m_sales_prev'>0.0</set>
                <set name='m_cost_prev'>0.0</set>
            </else>
        </if>

        <!-- Updates percentage in sales_fact_month_* actual year -->
        <if>
            <expr>
                m_sales_prev != 0 AND m_cost_prev != 0
            </expr>
            <then>
                <update table='sales_fact_month_product'>
                    <column name='sales_pct'>ROUND(((((sales)/(sales_prev))-1)*100),2)</column>
                    <column name='cost_pct'>ROUND(((((cost)/(cost_prev))-1)*100),2)</column>
                    <where>
                        product_id = p_product_id    AND
                        the_year = m_the_year
                    </where>
                </update>
            </then>
            <else-if>
                <expr>
                    m_sales_prev == 0 AND m_cost_prev != 0
                </expr>
                <then>
                    <update table='sales_fact_month_product'>
                        <column name='sales_pct'>100</column>
                        <column name='cost_pct'>ROUND(((((cost)/(cost_prev))-1)*100),2)</column>
                        <where>
                            product_id = p_product_id    AND
                            the_year = m_the_year
                        </where>
                    </update>
                </then>
            </else-if>
            <else-if>
                <expr>
                    m_sales_prev != 0 AND m_cost_prev == 0
                </expr>
                <then>
                    <update table='sales_fact_month_product'>
                        <column name='sales_pct'>ROUND(((((sales)/(sales_prev))-1)*100),2)</column>
                        <column name='cost_pct'>100</column>
                        <where>
                            product_id = p_product_id    AND
                            the_year = m_the_year
                        </where>
                    </update>
                </then>
            </else-if>
            <else-if>
                <expr>
                    m_sales_prev == 0 AND m_cost_prev == 0
                </expr>
                <then>
                    <update table='sales_fact_month_product'>
                        <column name='sales_pct'>100</column>
                        <column name='cost_pct'>100</column>
                        <where>
                            product_id = p_product_id    AND
                            the_year = m_the_year
                        </where>
                    </update>
                </then>
            </else-if>
        </if>
        
        <!-- Counter for number of entries in sales_fact_month_* for year + 1, if 0 will insert, if greater will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_month_product
            WHERE
                product_id = p_product_id     AND
                the_year = m_the_year + 1
        </select>

        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>

                <!-- Selects sales_prev and cost_prev to choose the percentage -->
                <select>
                    <columns>
                        sales, cost
                    </columns>
                    <into>m_sales_prev, m_cost_prev</into>
                    FROM sales_fact_month_product
                    WHERE 
                        product_id = p_product_id       AND
                        the_year = m_the_year
                </select>
                
                <!-- Updates percentage in actual year -->
                <if>
                    <expr>
                        m_sales_prev != 0 AND m_cost_prev != 0
                    </expr>
                    <then>
                        <update table='sales_fact_month_product'>
                            <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales</column>
                            <column name='sales_prev'>sales_prev - p_store_sales</column>
                            <column name='cost_prev'>cost_prev - p_store_cost</column>
                            <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                            <column name='sales_count_prev'>sales_count_prev - 1</column>
                            <column name='sales_pct'>ROUND(((((sales)/(sales_prev - p_store_sales))-1)*100),2)</column>
                            <column name='cost_pct'>ROUND(((((cost)/(cost_prev - p_store_cost))-1)*100),2)</column>
                            <where>
                                product_id = p_product_id    AND
                                the_year = m_the_year + 1
                            </where>
                        </update>
                    </then>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev != 0
                        </expr>
                        <then>
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales</column>
                                <column name='sales_prev'>sales_prev - p_store_sales</column>
                                <column name='cost_prev'>cost_prev - p_store_cost</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev - 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>ROUND(((((cost)/(cost_prev - p_store_cost))-1)*100),2)</column>
                                <where>
                                    product_id = p_product_id    AND
                                    the_year = m_the_year + 1
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev != 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales</column>
                                <column name='sales_prev'>sales_prev - p_store_sales</column>
                                <column name='cost_prev'>cost_prev - p_store_cost</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev - 1</column>
                                <column name='sales_pct'>ROUND(((((sales)/(sales_prev - p_store_sales))-1)*100),2)</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    product_id = p_product_id    AND
                                    the_year = m_the_year + 1
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_month_product'>
                                <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales</column>
                                <column name='sales_prev'>sales_prev - p_store_sales</column>
                                <column name='cost_prev'>cost_prev - p_store_cost</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev - 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    product_id = p_product_id    AND
                                    the_year = m_the_year + 1
                                </where>
                            </update>
                        </then>
                    </else-if>
                </if>
            </then>
        </if>

        <!--         product SALES_FACT_YEARMONTH        -->

        <!-- Month - 1 [Year - 1] -->
        <if>
            <expr>
                m_month_of_year == 1
            </expr>
            <then>
                <set name='m_month_of_year_prev'>12</set>
                <set name='m_the_year_prev'>m_the_year - 1</set>
            </then>
            <else>
                <set name='m_month_of_year_prev'>m_month_of_year - 1</set>
                <set name='m_the_year_prev'>m_the_year</set>
            </else>
        </if>
        
        <!-- Counter for number of entries in sales_fact_yearmonth_* for previous year, if greater than 0 will set previous year data into variables, if 0 will insert 0 -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_yearmonth_product
            WHERE
                product_id = p_product_id     AND
                the_year = m_the_year_prev AND
                month_of_year = m_month_of_year_prev
        </select>
        
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                <select>
                    <columns>
                        sales, cost
                    </columns>
                    <into>m_sales_prev, m_cost_prev</into>
                    FROM sales_fact_yearmonth_product
                    WHERE 
                            product_id = p_product_id     AND
                            month_of_year = m_month_of_year_prev AND
                            the_year = m_the_year_prev
                </select>
            </then>
            <else>
                <set name='m_sales_prev'>0.0</set>
                <set name='m_cost_prev'>0.0</set>
            </else>
        </if>

        <!-- Counter for number of entries in sales_fact_yearmonth_* for actual month and year, if greater than 0 will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_yearmonth_product
            WHERE
                product_id = p_product_id     AND
                the_year = m_the_year AND
                month_of_year = m_month_of_year
        </select>

        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>
                <!-- Updates percentage in sales_fact_month_* -->
                <if>
                    <expr>
                        m_sales_prev != 0 AND m_cost_prev != 0
                    </expr>
                    <then>
                        <update table='sales_fact_yearmonth_product'>
                            <column name='unit_sales'>unit_sales - p_unit_sales</column>
                            <column name='sales'>sales - p_store_sales</column>
                            <column name='sales_avg'>sales_avg - p_store_cost/12.0</column>
                            <column name='cost'>cost - p_store_cost</column>
                            <column name='cost_avg'>cost_avg - p_store_cost/12.0</column>
                            <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                            <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                            <column name='sales_count'>sales_count - 1</column>
                            <column name='sales_pct'>ROUND(((((sales - p_store_sales)/(sales_prev))-1)*100),2)</column>
                            <column name='cost_pct'>ROUND(((((cost - p_store_cost)/(cost_prev))-1)*100),2)</column>
                            <where>
                                product_id = p_product_id     AND
                                the_year = m_the_year AND
                                month_of_year = m_month_of_year
                            </where>
                        </update>
                    </then>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev != 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_product'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12.0</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12.0</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>ROUND(((((cost - p_store_cost)/(cost_prev))-1)*100),2)</column>
                                <where>
                                    product_id = p_product_id     AND
                                    the_year = m_the_year AND
                                    month_of_year = m_month_of_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev != 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_product'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12.0</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12.0</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>
                                    <column name='sales_pct'>ROUND(((((sales - p_store_sales)/(sales_prev))-1)*100),2)</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    product_id = p_product_id     AND
                                    the_year = m_the_year AND
                                    month_of_year = m_month_of_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_product'>
                                <column name='unit_sales'>unit_sales - p_unit_sales</column>
                                <column name='sales'>sales - p_store_sales</column>
                                <column name='sales_avg'>sales_avg - p_store_cost/12.0</column>
                                <column name='cost'>cost - p_store_cost</column>
                                <column name='cost_avg'>cost_avg - p_store_cost/12.0</column>
                                <column name='profit'>profit - (p_store_sales - p_store_cost)</column>
                                <column name='promotion_sales'>promotion_sales - m_promotion_cost</column>
                                <column name='sales_count'>sales_count - 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    product_id = p_product_id     AND
                                    the_year = m_the_year AND
                                    month_of_year = m_month_of_year
                                </where>
                            </update>
                        </then>
                    </else-if>
                </if>
            </then>
        </if>

        <!-- Month + 1 [Year + 1] -->
        <if>
            <expr>
                m_month_of_year == 12
            </expr>
            <then>
                <set name='m_month_of_year_prev'>1</set>
                <set name='m_the_year_prev'>m_the_year + 1</set>
            </then>
            <else>
                <set name='m_month_of_year_prev'>m_month_of_year + 1</set>
                <set name='m_the_year_prev'>m_the_year</set>
            </else>
        </if>
        
        <!-- Counter for number of entries in sales_fact_yearmonth_* for month + 1 [and year + 1], if 0 will insert, if greater will update -->
        <select>COUNT(*)
            <into>m_count</into>
            FROM sales_fact_yearmonth_product
            WHERE
                product_id = p_product_id     AND
                the_year = m_the_year_prev AND
                month_of_year = m_month_of_year_prev
        </select>
        
        <if>
            <expr>
                m_count &gt; 0
            </expr>
            <then>

                <!-- Select sales_prev and cost_prev from month +1 [and year +1] -->
                <select>
                    <columns>
                        sales, cost
                    </columns>
                    <into>m_sales_prev, m_cost_prev</into>
                    FROM sales_fact_yearmonth_product
                    WHERE 
                        product_id = p_product_id       AND
                        the_year = m_the_year AND
                        month_of_year = m_month_of_year
                </select>
                
                <!-- Updates sales_fact_year_month_* for month + 1 [and year + 1] with percentages -->
                <if>
                    <expr>
                        m_sales_prev != 0 AND m_cost_prev != 0
                    </expr>
                    <then>
                        <update table='sales_fact_yearmonth_product'>
                            <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales</column>
                            <column name='sales_prev'>sales_prev - p_store_sales</column>
                            <column name='cost_prev'>cost_prev - p_store_cost</column>
                            <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                            <column name='sales_count_prev'>sales_count_prev - 1</column>
                            <column name='sales_pct'>ROUND(((((sales)/(sales_prev - p_store_sales))-1)*100),2)</column>
                            <column name='cost_pct'>ROUND(((((cost)/(cost_prev - p_store_cost))-1)*100),2)</column>
                            <where>
                                product_id = p_product_id    AND
                                the_year = m_the_year_prev AND
                                month_of_year = m_month_of_year_prev
                            </where>
                        </update>
                    </then>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev != 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_product'>
                                <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales</column>
                                <column name='sales_prev'>sales_prev - p_store_sales</column>
                                <column name='cost_prev'>cost_prev - p_store_cost</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev - 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>ROUND(((((cost)/(cost_prev - p_store_cost))-1)*100),2)</column>
                                <where>
                                    product_id = p_product_id    AND
                                    the_year = m_the_year_prev AND
                                    month_of_year = m_month_of_year_prev
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev != 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_product'>
                                <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales</column>
                                <column name='sales_prev'>sales_prev - p_store_sales</column>
                                <column name='cost_prev'>cost_prev - p_store_cost</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev - 1</column>
                                <column name='sales_pct'>ROUND(((((sales)/(sales_prev - p_store_sales))-1)*100),2)</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    product_id = p_product_id    AND
                                    the_year = m_the_year_prev AND
                                    month_of_year = m_month_of_year_prev
                                </where>
                            </update>
                        </then>
                    </else-if>
                    <else-if>
                        <expr>
                            m_sales_prev == 0 AND m_cost_prev == 0
                        </expr>
                        <then>
                            <update table='sales_fact_yearmonth_product'>
                                <column name='unit_sales_prev'>unit_sales_prev - p_unit_sales</column>
                                <column name='sales_prev'>sales_prev - p_store_sales</column>
                                <column name='cost_prev'>cost_prev - p_store_cost</column>
                                <column name='promotion_sales_prev'>promotion_sales_prev - m_promotion_cost</column>
                                <column name='sales_count_prev'>sales_count_prev - 1</column>
                                <column name='sales_pct'>100</column>
                                <column name='cost_pct'>100</column>
                                <where>
                                    product_id = p_product_id    AND
                                    the_year = m_the_year_prev AND
                                    month_of_year = m_month_of_year_prev
                                </where>
                            </update>
                        </then>
                    </else-if>
                </if>
            </then>
        </if>
    </body>
</procedure>
-- **************************************************************************
-- sales_fact_act_del
-- DEISTER WebStudio XSQL-UDFUNC  -  Engine: ids
-- **************************************************************************