SQLize Online / PHPize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
CREATE DEFINER=`datawarehouse`@`%` PROCEDURE `INFORME_COMERCIAL_AUDITORIA_ESPECIAL_UNIFICADO`(in FECHA_INICIAL date,in FECHA_FINAL date) BEGIN -- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- /* Procedimiento almacenado que calcula los datos de ventas, márgen, stock, footfall y objetivos necesarios para genear los Informes de Ventas Semanales del año JD. Los Informes Semanales se generan todas las semanas. La fecha final y la fecha inical del año actual son los parámetros de entrada del procedimiento y a partir de éstas se calculan las fechas de inicio y fin del año anterior. */ /* LDP 2016-06-15: Se incorpora la creación de la tabla PRODUCTO_WEB. LDP 2016-06-20: Se incorpora la creación de la tablas para las ventas a través de los dispositivos móviles APP. LDP 2016-07-27: Se incorporan los campos DiaSemana, GPVTarget_TY, Target_LY y GPVTarget_LY a las tablas de "TIENDAS". LDP 2016-11-03: Se modifica la forma de calcular las fechas comparables del año anterior. Ahora no se tiene en cuenta si el año anterior es bisiesto, se usa la tabla de calendario_detalle_jd_v2 que ya contiene las fechas comparables en cada caso". LDP 2016-11-21: Se incorpora la creación de la tabla EVOL_SEMANAL_PROD. LDP 2017-04-04: Se añaden los campos Net_Sales_TY_LFL, Net_Sales_LY_LFL, GPV_TY_LFL y GPV_LY_LFL a las tablas de TIENDA y TIENDA_APP. LDP 2017-04-21: Se añaden los campos branch_type y seccion en las tablas de PRODUCTO. LDP 2017-06-22: Se detecta que los cálculos de los campos Net_Sales_TY_LFL, Net_Sales_LY_LFL, GPV_TY_LFL y GPV_LY_LFL en las tablas de TIENDA y TIENDA_APP no son correctos y se modifican. LDP 2017-10-26: Se incorpora al final del código, la recarga incremental de la tabla PRODUCTO_SEMANAS. LDP 2017-10-26: Se incorpora al final del código, una nueva tabla para un informe de gestión de ventas de la 911 y ventas multicanal. LDP 2017-11-20: Se incorporan las columnas FF_TY_LFL, FF_LY_LFL, Transactions_TY_LFL y Transactions_LY_LFL en las tablas de Tickets. LDP 2018-01-29: Incluimos filtros en las consultas para no sacar datos de las tiendas Sport Zone (Store_type='SPZ'). LDP 2018-03-24: Incluimos filtros en las consultas para no sacar datos de stock de producto SPZ que son producto de temporada 9XX del almacén A99. LDP 2018-10-11: Incluimos filtros en las consultas para no sacar datos de venta y stock de producto SPZ filtrando por almacen/tienda TVMF. LDP 2018-10-22: Incluimos la semana en la tabla de TIENDAS cuando se ejecute el informe Acumulado del año. LDP 2019-03-13: Incluimos la venta bruta LFL en las tablas de Tickets. LDP 2019-07-08: Se modifica el filtro para las tiendas y se coge sólo las tiendas SPR cuando el mercado es distinto de España. LDP 2019-08-01: Se modifica el filtro para coger las tiendas de todos los mercados. Se añane el mercado en la hoja de tiendas. Se tienen en cuenta las ventas y stock de todas las tiendas abiertas y cerradas, de Portugal y de Sprinter, por lo que necesitamos leer datos de las tablas de stock_Semanas_pt y de sales_detail_pt. ALM 2020-01-09: Se añade un ifnull para el campo product_code_1 al crear la tabla ventas_pt ya que entran articulos de la 1722 que no tenemos en el maestro LDP 2020-01-21: - stock de Atalayas asignado al mercado de España - stock de Azambuya asignado al mercado de Portugal (si existe stock) - El resto de mercados aparecerán con stock en almacén = 0 LDP 2020-01-28: - Se incluye la venta bruta aact y aant en la tabla de PRODUCTO - Se excluye la tienda TVZ de PORT, equivalenete a la TVM de ESP - Se modifica el cálculo del Tarjet , antes Tarjet=objetivo/1.21, ahora Tarjet = objetivo/(1+ %iva_general_mercado) - Se incluyen los datos de stock en uds y stock euros en la tabla TICKET_SEMANAS - Guardar en BBDD la tabla TIENDA_AREA2 con la información de la ejecución de cada una de las semanas. - El campo pvp_actual se rescata de la nueva tabla md_dbg.pvps_fares que recoge los cambios de precio por tarifa. - Se incluye el campo tarifa (fare_code) en las tablas de PRODUCTOS y multi_web ALM 2020-03-20: - Se añade el IVA por tienda con la nueva tabla de excepciones vats_branch_exceptions VSS 2020-05-11: - Se generan registros con ventas a 0 para todos los días de la semana para las tablas DWH (NEW_NEW y YTD) para que tengamos target_TY para los días en que no tenemos ventas_TY ni ventas_LY ALM 2020-06-10: - Corregir cálculo de Transactions_TY_LFL y LY para que miren transacciones y no FF VSS 2020-06-12: - Se añade una columna "Channel_Type" en las tablas PRODUCTO_SEMANAS_NEW_NEW y PRODUCTO_SEMANAS_YTD para que consigamos hacer el top productos solo de la WEB, en conjunto y separado por mercado ALM 2020-06-10: - Crear tablas de TIENDA y TICKET sin servicios: TIENDA_NO_SERV y TICKET_NO_SERV - Se añaden las tablas TICKET_SEMANAS_NO_SERV_NEW_NEW y TICKET_SEMANAS_NO_SERV_YTD ALM 2021-03-15: - Nueva versión con tablas precalculadas para compara las ventas de 2021 contra 2019 para als tiendas físicas - QUITAR EN 2022 Y VOLVER A LA VERSIÓN INFORME_COMERCIAL_AUDITORIA FRGG 2021-10-07: - Creación de la tabla multi_web2 - Seleccionar las variables de la nueva tabla "multi_web2", y renombrarlas con los mismos nombres que utilizamos ahora en el informe pero incluyendo el nuevo campo de Size_pos MJFP 2021-10-28: - Se modifica este SP para crear un nuevo archivo PRODUCT contra año 2020 (Jira LBI-137) MJFP 2022-02-02: - Se revisa este SP para comprobar que la copia que se hizo del archivo PRODUCT contra año 2020 ahora lo hace contra 2021 (Jira LBI-202) MJFP 2022-03-08: - Se modifica este SP para añadir los campos "Store_Target_TY" y "GPV_Store_Target_TY" (Jira LBI-190) FRGG 2022-06-07: Se unifican los SP INFORME_COMERCIAL_AUDITORIA_ESPECIAL_19_20 (acumulado) y INFORME_COMERCIAL_AUDITORIA_ESPECIAL_19_20_21 (semanal) para generar uno solo y poder lanzar únicamente 5 semanas MJFP 2022-11-14: Se modifica el SP para sustituir la utilizacion de la tabla "md_dbg.sales_detail" por la tabla con las promociones incluidas "consultas.rep_pr_sales_detail" (Jira CH2240-130) FRGG 2022-11-23: Se añade el parametro FECHA_INICIAL_SEMANA_FINAL_JD para simular el informe semanal para generar las tablas multi_web y multi_web2 MJFP 2023-01-19: En enero de 2023, se modifica este sp para volver a comparar el año anterior con el año anterior natural (2023vs2022,Jira LBI-577) */ /*modificacion compara 19-22*/ declare ANIO_ACTUAL VARCHAR(4); declare SEMANA_ACTUAL VARCHAR(2); declare MES_ACTUAL VARCHAR(2); -- declare SEMANAS_A_REPOCESAR int unsigned default 0; declare SEMANA_FINAL_STR VARCHAR(2); declare SEMANA_FINAL int unsigned default 1; declare SEMANA_INICIAL int unsigned default 1; declare MES_FINAL_JD VARCHAR(2); declare FIN_MES_JD bool default 0; declare SEMANA_INICIAL_MES_JD int unsigned default 0; declare FECHA_INICIAL_SEMANA_FINAL_JD date; declare FECHA_INICIAL_ANT date; declare FECHA_FINAL_ANT date; /*declare FECHA_INICIAL_ANT_ANT date; declare FECHA_FINAL_ANT_ANT date; declare FECHA_INICIAL_ANT_ANT_ANT date; declare FECHA_FINAL_ANT_ANT_ANT date;*/ SET ANIO_ACTUAL=(select anio_jd from md_dbg.calendario_Detalle_jd_v2 where fecha=FECHA_INICIAL); SET SEMANA_ACTUAL=(select semana_jd from md_dbg.calendario_Detalle_jd_v2 where fecha=FECHA_INICIAL); SET MES_ACTUAL=(select mes_jd from md_dbg.calendario_Detalle_jd_v2 where fecha=FECHA_INICIAL); SET SEMANA_FINAL_STR=(select semana_jd from md_dbg.calendario_Detalle_jd_v2 where fecha=FECHA_FINAL); SET SEMANA_FINAL=(cast(SEMANA_FINAL_STR as unsigned)); SET SEMANA_INICIAL=(cast(SEMANA_ACTUAL as unsigned)); SET MES_FINAL_JD=(select mes_jd from md_dbg.calendario_Detalle_jd_v2 where fecha=FECHA_FINAL); SET FIN_MES_JD=(select case when mes_jd > MES_FINAL_JD then true else false end as fin_mes_jd_flag from md_dbg.calendario_Detalle_jd_v2 where fecha=date_add(FECHA_FINAL, interval 1 DAY)); SET SEMANA_INICIAL_MES_JD=(select min(semana_jd) as semana_jd from md_dbg.calendario_Detalle_jd_v2 where mes_jd=MES_FINAL_JD and anio_jd=ANIO_ACTUAL); SET FECHA_INICIAL_SEMANA_FINAL_JD=(select min(fecha) as fecha_inicial from md_dbg.calendario_Detalle_jd_v2 where semana_jd=SEMANA_FINAL_STR and anio_jd=ANIO_ACTUAL); SET FECHA_INICIAL_ANT=(select fecha_comp from md_dbg.calendario_Detalle_jd_v2 where fecha=FECHA_INICIAL); SET FECHA_FINAL_ANT=(select fecha_comp from md_dbg.calendario_Detalle_jd_v2 where fecha=FECHA_FINAL); /*SET FECHA_INICIAL_ANT_ANT=(select fecha_comp from md_dbg.calendario_Detalle_jd_v2 where fecha=FECHA_INICIAL_ANT); SET FECHA_FINAL_ANT_ANT=(select fecha_comp from md_dbg.calendario_Detalle_jd_v2 where fecha=FECHA_FINAL_ANT); SET FECHA_INICIAL_ANT_ANT_ANT=(select fecha_comp from md_dbg.calendario_Detalle_jd_v2 where fecha=FECHA_INICIAL_ANT_ANT); SET FECHA_FINAL_ANT_ANT_ANT=(select fecha_comp from md_dbg.calendario_Detalle_jd_v2 where fecha=FECHA_FINAL_ANT_ANT);*/ /*actualiza tabla artículos Portugal */ call consultas.articulos_pt (); /* maestro de artículos sin servicios para nuevas tablas _NO_SERV */ drop table if exists consultas.articulos_total_NO_SERV_ica; create table consultas.articulos_total_NO_SERV_ica as select distinct product_code from consultas.articulos where area <> 'Servicios' UNION select distinct product_code from consultas.articulos_pt where area <> 'Servicios'; alter table consultas.articulos_total_NO_SERV_ica add index idx(product_code); drop table if exists consultas.articulos_NO_SERV_ica; create table consultas.articulos_NO_SERV_ica as select distinct product_code from consultas.articulos where area <> 'Servicios'; alter table consultas.articulos_NO_SERV_ica add index idx(product_code); drop table if exists consultas.articulos_pt_NO_SERV_ica; create table consultas.articulos_pt_NO_SERV_ica as select distinct product_code from consultas.articulos_pt where area <> 'Servicios'; alter table consultas.articulos_pt_NO_SERV_ica add index idx(product_code); /* Construimos la tabla de tiendas teniendo en cuenta tanto las que se han dado de alta en el sistema de Sprinter como las que existían de antes en SPZ Portugal */ /* De esta manera podemos ver la información comparable del año anterior en totales de ventas */ drop table if exists tiendas_ica; create table tiendas_ica as select a.*,b.branch_no as cod_spz from md_dbg.branch a left join md_Dbg.branch_pt b on a.branch_no=b.codspr where a.branch_no not in ('101','TVM','TVZ') and substring(a.branch_no,1,2)<>'9Z' /* excluimos estas tiendas y almacenes */; insert into tiendas_ica (branch_name,id_subzone,cp,branch_type,store_type,open_date,close_date,is_store,id_mercado,mercado,cod_spz) select branch_name, id_subzone, cp, (case when branch_type is null then store_type else branch_type end), store_type, open_date, close_date, (case when store_type='WH' then 0 else 1 end) /* es tienda = 0 cuando es almacén de Portugal */, (case when id_mercado is null then 2 else id_mercado end) /* mercado Portugal cuando es null */, (case when desc_mercado is null then 'Portugal (+ islas)' else desc_mercado end) /* mercado Portugal cuando es null */, branch_no /* se inserta como código de tienda spz */ from md_dbg.branch_pt where codspr is null; /* update fare_code */ update tiendas_ica set fare_code=(case when mercado in ('España','Canarias') then '01' when mercado='Portugal (+ islas)' then '02' when mercado='Islas Azores' then '04' end) where fare_code is null; alter table tiendas_ica add index (cod_spz); alter table tiendas_ica add index (branch_no); alter table tiendas_ica add index (fare_code); /*-------------------------/* stock en stock_Semanas_pt /*------------------------------------------*/ -- BORRADO AL ESTAR TODO COMENTADO, MIRAR EN LOS SP INFORME_COMERCIAL_AUDITORIA_ESPECIAL_19_20 (acumulado) O INFORME_COMERCIAL_AUDITORIA_ESPECIAL_19_20_21 (semanal) SI SE QUIERE RECUPERAR /*--------------------------------------------------------------------------------------------------------*/ /* LFL */ /*modificacion compara 19-22*/ drop table if exists tiendas_actual_pre_1; create table tiendas_actual_pre_1 as select product_code, branch_no, sales_date, sum(case when qty is null or qty='' then 0 else qty end) as vta_uds_aact, sum(case when net_sales is null or net_sales='' then 0 else net_sales end) as vta_neta_aact, sum(case when gross_sales is null or gross_sales='' then 0 else gross_sales end) as vta_bruta_aact, sum((case when net_sales is null or net_sales='' then 0 else net_sales end)-(case when cost_price_atos is null or cost_price_atos='' then 0 else cost_price_atos end)) as mrg_aact, 0 as vta_uds_aant, 0 as vta_neta_aant, 0 as vta_bruta_aant, 0 as mrg_aant from consultas.rep_pr_sales_detail where sales_date<=FECHA_FINAL and sales_date>=FECHA_INICIAL and branch_no in (select branch_no from tiendas_ica) group by sales_date,branch_no,product_code; drop table if exists tiendas_actual_pre; create table tiendas_actual_pre as select product_code, branch_no, sales_date, sum(vta_uds_aact) as vta_uds_aact, sum(vta_neta_aact) as vta_neta_aact, sum(vta_bruta_aact) as vta_bruta_aact, sum(mrg_aact) as mrg_aact, 0 as vta_uds_aant, 0 as vta_neta_aant, 0 as vta_bruta_aant, 0 as mrg_aant from tiendas_actual_pre_1 group by product_code,branch_no,sales_date; alter table tiendas_actual_pre add index indice (sales_date); drop table if exists tiendas_actual; create table tiendas_actual as select a.*, weekday(a.sales_date) as dia, b.semana_jd as semana from tiendas_actual_pre a left join md_dbg.calendario_detalle_jd_v2 b ON a.sales_date=b.Fecha; /*modificacion compara año actual vs año anterior*/ drop table if exists tiendas_anterior_pre_1_especial; create table tiendas_anterior_pre_1_especial as select product_code, branch_no, sales_date, 0 as vta_uds_aact, 0 as vta_neta_aact, 0 as vta_bruta_aact, 0 as mrg_aact, sum(case when qty is null or qty='' then 0 else qty end) as vta_uds_aant, sum(case when net_sales is null or net_sales='' then 0 else net_sales end) as vta_neta_aant, sum(case when gross_sales is null or gross_sales='' then 0 else gross_sales end) as vta_bruta_aant, sum((case when net_sales is null or net_sales='' then 0 else net_sales end)-(case when cost_price_atos is null or cost_price_atos='' then 0 else cost_price_atos end)) as mrg_aant from consultas.rep_pr_sales_detail where sales_date<=FECHA_FINAL_ANT and sales_date>=FECHA_INICIAL_ANT and branch_no in (select branch_no from tiendas_ica) group by sales_date,branch_no,product_code; drop table if exists tiendas_anterior_pre_especial; create table tiendas_anterior_pre_especial as select product_code, branch_no, sales_date, 0 as vta_uds_aact, 0 as vta_neta_aact, 0 as vta_bruta_aact, 0 as mrg_aact, sum(vta_uds_aant)as vta_uds_aant, sum(vta_neta_aant) as vta_neta_aant, sum(vta_bruta_aant) as vta_bruta_aant, sum(mrg_aant) as mrg_aant from tiendas_anterior_pre_1_especial group by product_code,branch_no,sales_date; alter table tiendas_anterior_pre_especial add index indice (sales_date); drop table if exists tiendas_anterior; create table tiendas_anterior as select a.*, weekday(a.sales_date) as dia, b.semana_jd as semana from tiendas_anterior_pre_especial a left join md_dbg.calendario_detalle_jd_v2 b ON a.sales_date=b.Fecha_comp; alter table tiendas_anterior add index indice (branch_no,semana,dia); alter table tiendas_actual add index indice (branch_no,semana,dia); drop table if exists LFL_pre_1; create table LFL_pre_1 as select distinct a.branch_no, a.semana, a.dia from tiendas_actual a inner join (select distinct branch_no, semana, dia from tiendas_anterior) b on a.branch_no=b.branch_no and a.dia=b.dia and a.semana=b.semana; alter table LFL_pre_1 add index indice (branch_no,semana,dia); drop table if exists LFL_pre_2; create table LFL_pre_2 as select product_code,branch_no,sales_date,vta_uds_aact,vta_neta_aact,vta_bruta_aact,mrg_aact,vta_uds_aant,vta_neta_aant,vta_bruta_aant,mrg_aant,dia,semana from tiendas_actual where (branch_no,semana,dia) in (select branch_no,semana,dia from LFL_pre_1) union all select product_code,branch_no,sales_date,vta_uds_aact,vta_neta_aact,vta_bruta_aact,mrg_aact,vta_uds_aant,vta_neta_aant,vta_bruta_aant,mrg_aant,dia,semana from tiendas_anterior where (branch_no,semana,dia) in (select branch_no,semana,dia from LFL_pre_1); alter table LFL_pre_2 add index indice (branch_no,product_code,semana,dia); alter table LFL_pre_2 add index idx_semana (semana); drop table if exists LFL; create table LFL as select semana, dia, branch_no, product_code, (case when sum(vta_uds_aact) is null then 0 else sum(vta_uds_aact) end) as vta_uds_aact, (case when sum(vta_neta_aact) is null then 0 else sum(vta_neta_aact) end) as vta_neta_aact, (case when sum(vta_bruta_aact) is null then 0 else sum(vta_bruta_aact) end) as vta_bruta_aact, (case when sum(mrg_aact) is null then 0 else sum(mrg_aact) end) as mrg_aact, (case when sum(vta_uds_aant) is null then 0 else sum(vta_uds_aant) end) as vta_uds_aant, (case when sum(vta_neta_aant) is null then 0 else sum(vta_neta_aant) end) as vta_neta_aant, (case when sum(vta_bruta_aant) is null then 0 else sum(vta_bruta_aant) end) as vta_bruta_aant, (case when sum(mrg_aant) is null then 0 else sum(mrg_aant) end) as mrg_aant from LFL_pre_2 group by branch_no,product_code,semana,dia; /* eliminamos tablas temporales */ drop table if exists tiendas_actual_pre; drop table if exists tiendas_actual; drop table if exists LFL_pre_1; drop table if exists LFL_pre_2; /* TIENDAS_ZONAS_TERRITORIALES_TIPO_TIENDA*/ drop table if exists tienda_zona_territorial_tipo_tienda; create table tienda_zona_territorial_tipo_tienda as select (case when a.branch_no='' or a.branch_no is null then a.cod_spz else a.branch_no end) as branch_no, a.branch_name as nombre_tienda, a.branch_type as tipo_tienda, CONCAT(b.ID_SUBZONE,'_',b.SUBZONE) AS subzona, c.delegation_manager as delegado, CONCAT(b.ID_ZONE,'_',b.ZONE) AS zona, c.territory_manager as territorial, a.mercado from tiendas_ica a left join md_dbg.geocomercial_hierarchy b on a.ID_SUBZONE=b.ID_SUBZONE left join md_dbg.branch_hierarchy_emails c on a.branch_no=c.branch_no where a.is_store=1; alter table tienda_zona_territorial_tipo_tienda add index idx_branch_no(branch_no); /*----------------------------------------------- DATOS AÑO ACTUAL --------------------------------------------------------------*/ /* ventas año actual */ drop table if exists ventas_anio_actual_pre_1_spr; create table ventas_anio_actual_pre_1_spr as SELECT * FROM consultas.rep_pr_sales_detail a where a.sales_date<=FECHA_FINAL and a.sales_date>=FECHA_INICIAL; alter table ventas_anio_actual_pre_1_spr add index idx(branch_no); drop table if exists ventas_anio_actual_pre_1; create table ventas_anio_actual_pre_1 as select (case when a.branch_no is null or a.branch_no='' then 'na' else a.branch_no end) as branch_no, (case when b.nombre_tienda is null or b.nombre_tienda='' then 'na' else b.nombre_tienda end) as nombre_tienda, (case when b.tipo_tienda is null or b.tipo_tienda='' then 'na' else b.tipo_tienda end) as tipo_tienda, (case when b.subzona is null or b.subzona='' then 'na' else b.subzona end) as subzona, (case when b.delegado is null or b.delegado='' then 'na' else b.delegado end) as delegado, (case when b.zona is null or b.zona='' then 'na' else b.zona end) as zona, (case when b.territorial is null or b.territorial='' then 'na' else b.territorial end) as territorial, (case when a.product_code is null or a.product_code='' then 'na' else a.product_code end) as product_code, (case when a.qty is null or a.qty='' then 0 else a.qty end ) as qty, (case when a.net_sales is null or a.net_sales='' then 0 else a.net_sales end) as net_sales, (case when a.gross_sales is null or a.gross_sales='' then 0 else a.gross_sales end) as gross_sales, (case when a.cost_price_atos is null or a.cost_price_atos='' then 0 else a.cost_price_atos end) as cost_price_atos, a.sales_date, a.channel_code from ventas_anio_actual_pre_1_spr a inner join tienda_zona_territorial_tipo_tienda b on a.branch_no=b.branch_no; /*modificacion compara 19-22*/ drop table if exists ventas_anio_actual; create table ventas_anio_actual as select b.semana_jd as semana, weekday(a.sales_date) as dia, a.product_code, a.branch_no, a.nombre_tienda, a.tipo_tienda, a.subzona, a.delegado, a.zona, a.territorial, sum(a.qty) as venta_uds_anio_actual, sum(a.net_sales) as venta_neta_anio_actual, sum(a.gross_sales) as venta_bruta_anio_actual, sum(a.cost_price_atos) as coste_anio_actual, sum(a.net_sales-a.cost_price_atos) as margen_anio_actual, 0 as stock_anio_actual, 0 as stock_unidades_actual, 0 as venta_uds_ultima_semana_anio_actual, 0 as venta_neta_ultima_semana_anio_actual, 0 as margen_ultima_semana_anio_actual from ventas_anio_actual_pre_1 a left join md_dbg.calendario_detalle_jd_v2 b on a.sales_date=b.fecha GROUP BY a.sales_date,b.semana_jd,a.product_code,a.branch_no,a.nombre_tienda,a.tipo_tienda,a.subzona,a.delegado,a.zona,a.territorial; drop table if exists ventas_anio_actual_app; create table ventas_anio_actual_app as select b.semana_jd as semana, weekday(a.sales_date) as dia, a.product_code, a.branch_no, a.nombre_tienda, a.tipo_tienda, a.subzona, a.delegado, a.zona, a.territorial, sum(a.qty) as venta_uds_anio_actual, sum(a.net_sales) as venta_neta_anio_actual, sum(a.gross_sales) as venta_bruta_anio_actual, sum(a.cost_price_atos) as coste_anio_actual, sum(a.net_sales-a.cost_price_atos) as margen_anio_actual, 0 as stock_anio_actual, 0 as stock_unidades_actual, 0 as venta_uds_ultima_semana_anio_actual, 0 as venta_neta_ultima_semana_anio_actual, 0 as margen_ultima_semana_anio_actual from ventas_anio_actual_pre_1 a left join md_dbg.calendario_detalle_jd_v2 b on a.sales_date=b.fecha where a.channel_code='3' and a.channel_code<>'' GROUP BY a.sales_date,b.semana_jd,a.product_code,a.branch_no,a.nombre_tienda,a.tipo_tienda,a.subzona,a.delegado,a.zona,a.territorial; drop table if exists ventas_ult_semana_anio_actual; create table ventas_ult_semana_anio_actual as select b.semana_jd as semana, weekday(a.sales_date) as dia, a.product_code, a.branch_no, a.nombre_tienda, a.tipo_tienda, a.subzona, a.delegado, a.zona, a.territorial, sum(a.qty) as venta_uds_ultima_semana_anio_actual, sum(a.net_sales) as venta_neta_ultima_semana_anio_actual, sum(a.net_sales-a.cost_price_atos) as margen_ultima_semana_anio_actual, 0 as venta_uds_anio_actual, 0 as venta_neta_anio_actual, 0 as venta_bruta_anio_actual, 0 as coste_anio_actual, 0 as margen_anio_actual, 0 as stock_anio_actual, 0 as stock_unidades_actual from ventas_anio_actual_pre_1 a left join md_dbg.calendario_detalle_jd_v2 b on a.sales_date=b.fecha where sales_date<=FECHA_FINAL and sales_date>=(select min(Fecha) from md_dbg.calendario_detalle_jd_v2 where Semana_jd=(select Semana_jd from md_dbg.calendario_detalle_jd_v2 where Fecha=FECHA_FINAL) and Anio_jd=(select Anio_jd from md_dbg.calendario_detalle_jd_v2 where Fecha=FECHA_FINAL)) group by b.semana_jd,a.sales_date,a.product_code,a.branch_no,a.nombre_tienda,a.tipo_tienda,a.subzona,a.delegado,a.zona,a.territorial; drop table if exists ventas_ult_semana_anio_actual_app; create table ventas_ult_semana_anio_actual_app as select b.semana_jd as semana, weekday(a.sales_date) as dia, a.product_code, a.branch_no, a.nombre_tienda, a.tipo_tienda, a.subzona, a.delegado, a.zona, a.territorial, sum(a.qty) as venta_uds_ultima_semana_anio_actual, sum(a.net_sales) as venta_neta_ultima_semana_anio_actual, sum(a.net_sales-a.cost_price_atos) as margen_ultima_semana_anio_actual, 0 as venta_uds_anio_actual, 0 as venta_neta_anio_actual, 0 as venta_bruta_anio_actual, 0 as coste_anio_actual, 0 as margen_anio_actual, 0 as stock_anio_actual, 0 as stock_unidades_actual from ventas_anio_actual_pre_1 a left join md_dbg.calendario_detalle_jd_v2 b on a.sales_date=b.fecha where sales_date<=FECHA_FINAL and sales_date>=(select min(Fecha) from md_dbg.calendario_detalle_jd_v2 where Semana_jd=(select Semana_jd from md_dbg.calendario_detalle_jd_v2 where Fecha=FECHA_FINAL) and Anio_jd=(select Anio_jd from md_dbg.calendario_detalle_jd_v2 where Fecha=FECHA_FINAL)) and a.channel_code='3' and a.channel_code<>'' group by b.semana_jd,a.sales_date,a.product_code,a.branch_no,a.nombre_tienda,a.tipo_tienda,a.subzona,a.delegado,a.zona,a.territorial; /* stock año actual */ drop table if exists stock_anio_actual_pre; create table stock_anio_actual_pre as select fecha_fin,product_code,branch_no,uds,valoracion from md_dbg.stock_Semanas where Fecha_fin=FECHA_FINAL and branch_no in (select branch_no from tiendas_ica); drop table if exists stock_anio_actual_pre_2; create table stock_anio_actual_pre_2 as select b.semana_jd as semana, weekday(a.Fecha_fin) as dia, a.product_code, a.branch_no, sum(a.valoracion) as stock_anio_actual, sum(a.uds) as stock_unidades_actual from stock_anio_actual_pre a left join md_dbg.calendario_Detalle_jd_v2 b on a.fecha_fin=b.fecha group by a.Fecha_fin,b.semana_jd,a.product_code,a.branch_no; drop table if exists stock_anio_actual; create table stock_anio_actual as select a.semana, a.dia, a.product_code, a.branch_no, (case when b.nombre_tienda is null or b.nombre_tienda='' then 'na' else b.nombre_tienda end) as nombre_tienda, (case when b.tipo_tienda is null or b.tipo_tienda='' then 'na' else b.tipo_tienda end) as tipo_tienda, (case when b.subzona is null or b.subzona='' then 'na' else b.subzona end) as subzona, (case when b.delegado is null or b.delegado='' then 'na' else b.delegado end) as delegado, (case when b.zona is null or b.zona='' then 'na' else b.zona end) as zona, (case when b.territorial is null or b.territorial='' then 'na' else b.territorial end) as territorial, sum(a.stock_anio_actual) as stock_anio_actual, sum(a.stock_unidades_actual) as stock_unidades_actual, 0 as venta_uds_ultima_semana_anio_actual, 0 as venta_neta_ultima_semana_anio_actual, 0 as margen_ultima_semana_anio_actual, 0 as venta_uds_anio_actual, 0 as venta_neta_anio_actual, 0 as venta_bruta_anio_actual, 0 as coste_anio_actual, 0 as margen_anio_actual from stock_anio_actual_pre_2 a left join tienda_zona_territorial_tipo_tienda b on a.branch_no=b.branch_no group by semana,dia,product_code,branch_no,nombre_tienda,tipo_tienda,subzona,delegado,zona,territorial; /* unimos ventas y stock del año actual */ drop table if exists ventas_stock_actual; create table ventas_stock_actual as select semana,dia,product_code,branch_no,nombre_tienda,tipo_tienda,subzona,delegado,zona,territorial,stock_anio_actual,stock_unidades_actual,venta_uds_anio_actual, venta_neta_anio_actual,venta_bruta_anio_actual,coste_anio_actual,margen_anio_actual,venta_uds_ultima_semana_anio_actual,venta_neta_ultima_semana_anio_actual,margen_ultima_semana_anio_actual from stock_anio_actual union all select semana,dia,product_code,branch_no,nombre_tienda,tipo_tienda,subzona,delegado,zona,territorial,stock_anio_actual,stock_unidades_actual,venta_uds_anio_actual, venta_neta_anio_actual,venta_bruta_anio_actual,coste_anio_actual,margen_anio_actual,venta_uds_ultima_semana_anio_actual,venta_neta_ultima_semana_anio_actual,margen_ultima_semana_anio_actual from ventas_anio_actual union all select semana,dia,product_code,branch_no,nombre_tienda,tipo_tienda,subzona,delegado,zona,territorial,stock_anio_actual,stock_unidades_actual,venta_uds_anio_actual, venta_neta_anio_actual,venta_bruta_anio_actual,coste_anio_actual,margen_anio_actual,venta_uds_ultima_semana_anio_actual,venta_neta_ultima_semana_anio_actual,margen_ultima_semana_anio_actual from ventas_ult_semana_anio_actual; drop table if exists ventas_stock_actual_app; create table ventas_stock_actual_app as select semana,dia,product_code,branch_no,nombre_tienda,tipo_tienda,subzona,delegado,zona,territorial,stock_anio_actual,stock_unidades_actual,venta_uds_anio_actual, venta_neta_anio_actual,venta_bruta_anio_actual,coste_anio_actual,margen_anio_actual,venta_uds_ultima_semana_anio_actual,venta_neta_ultima_semana_anio_actual,margen_ultima_semana_anio_actual from stock_anio_actual union all select semana,dia,product_code,branch_no,nombre_tienda,tipo_tienda,subzona,delegado,zona,territorial,stock_anio_actual,stock_unidades_actual,venta_uds_anio_actual, venta_neta_anio_actual,venta_bruta_anio_actual,coste_anio_actual,margen_anio_actual,venta_uds_ultima_semana_anio_actual,venta_neta_ultima_semana_anio_actual,margen_ultima_semana_anio_actual from ventas_anio_actual_app union all select semana,dia,product_code,branch_no,nombre_tienda,tipo_tienda,subzona,delegado,zona,territorial,stock_anio_actual,stock_unidades_actual,venta_uds_anio_actual, venta_neta_anio_actual,venta_bruta_anio_actual,coste_anio_actual,margen_anio_actual,venta_uds_ultima_semana_anio_actual,venta_neta_ultima_semana_anio_actual,margen_ultima_semana_anio_actual from ventas_ult_semana_anio_actual_app; drop table if exists ventas_stock_actual_agru; create table ventas_stock_actual_agru as select semana, dia, product_code, branch_no, nombre_tienda, tipo_tienda, subzona, delegado, zona, territorial, sum(stock_anio_actual) as stock_anio_actual, sum(stock_unidades_actual) as stock_unidades_actual, sum(venta_uds_anio_actual) as venta_uds_anio_actual, sum(venta_neta_anio_actual) as venta_neta_anio_actual, sum(venta_bruta_anio_actual) as venta_bruta_anio_actual, sum(coste_anio_actual) as coste_anio_actual, sum(margen_anio_actual) as margen_anio_actual, sum(venta_uds_ultima_semana_anio_actual) as venta_uds_ultima_semana_anio_actual, sum(venta_neta_ultima_semana_anio_actual) as venta_neta_ultima_semana_anio_actual, sum(margen_ultima_semana_anio_actual) as margen_ultima_semana_anio_actual, 0 as stock_anio_anterior, 0 as stock_unidades_anterior, 0 as venta_uds_anio_anterior, 0 as venta_neta_anio_anterior, 0 as venta_bruta_anio_anterior, 0 as coste_anio_anterior, 0 as margen_anio_anterior, 0 as venta_uds_ultima_semana_anio_anterior, 0 as venta_neta_ultima_semana_anio_anterior, 0 as margen_ultima_semana_anio_anterior from ventas_stock_actual where stock_unidades_actual+stock_anio_actual+venta_neta_anio_actual+venta_bruta_anio_actual+venta_uds_anio_actual+venta_uds_ultima_semana_anio_actual+margen_anio_actual<>0 group by semana,dia,product_code,branch_no,nombre_tienda,tipo_tienda,subzona,delegado,zona,territorial; drop table if exists ventas_stock_actual_agru_app; create table ventas_stock_actual_agru_app as select semana, dia, product_code, branch_no, nombre_tienda, tipo_tienda, subzona, delegado, zona, territorial, sum(stock_anio_actual) as stock_anio_actual, sum(stock_unidades_actual) as stock_unidades_actual, sum(venta_uds_anio_actual) as venta_uds_anio_actual, sum(venta_neta_anio_actual) as venta_neta_anio_actual, sum(venta_bruta_anio_actual) as venta_bruta_anio_actual, sum(coste_anio_actual) as coste_anio_actual, sum(margen_anio_actual) as margen_anio_actual, sum(venta_uds_ultima_semana_anio_actual) as venta_uds_ultima_semana_anio_actual, sum(venta_neta_ultima_semana_anio_actual) as venta_neta_ultima_semana_anio_actual, sum(margen_ultima_semana_anio_actual) as margen_ultima_semana_anio_actual, 0 as stock_anio_anterior, 0 as stock_unidades_anterior, 0 as venta_uds_anio_anterior, 0 as venta_neta_anio_anterior, 0 as venta_bruta_anio_anterior, 0 as coste_anio_anterior, 0 as margen_anio_anterior, 0 as venta_uds_ultima_semana_anio_anterior, 0 as venta_neta_ultima_semana_anio_anterior, 0 as margen_ultima_semana_anio_anterior from ventas_stock_actual_app where stock_unidades_actual+stock_anio_actual+venta_neta_anio_actual+venta_bruta_anio_actual+venta_uds_anio_actual+venta_uds_ultima_semana_anio_actual+margen_anio_actual<>0 group by semana,dia,product_code,branch_no,nombre_tienda,tipo_tienda,subzona,delegado,zona,territorial; /* -----------------------------------------------------------------------------------------------------------------------------------*/ /*eliminamos tablas temporales */ drop table if exists ventas_anio_actual_pre_1; drop table if exists stock_anio_actual_pre; drop table if exists stock_anio_actual_pre_1; drop table if exists stock_anio_actual_pre_2; drop table if exists stock_anio_actual; drop table if exists ventas_stock_actual; drop table if exists ventas_stock_actual_app; drop table if exists ventas_ult_semana_anio_actual; drop table if exists ventas_ult_semana_anio_actual_app; /*--------------------------------------------- DATOS AÑO ANTERIOR ------------------------------------------------------------*/ /* ventas año anterior */ drop table if exists ventas_anio_anterior_pre_1; create table ventas_anio_anterior_pre_1 as select (case when a.branch_no is null or a.branch_no='' then 'na' else a.branch_no end) as branch_no, (case when b.nombre_tienda is null or b.nombre_tienda='' then 'na' else b.nombre_tienda end) as nombre_tienda, (case when b.tipo_tienda is null or b.tipo_tienda='' then 'na' else b.tipo_tienda end) as tipo_tienda, (case when b.subzona is null or b.subzona='' then 'na' else b.subzona end) as subzona, (case when b.delegado is null or b.delegado='' then 'na' else b.delegado end) as delegado, (case when b.zona is null or b.zona='' then 'na' else b.zona end) as zona, (case when b.territorial is null or b.territorial='' then 'na' else b.territorial end) as territorial, (case when a.product_code is null or a.product_code='' then 'na' else a.product_code end) as product_code, (case when a.qty is null or a.qty='' then 0 else a.qty end ) as qty, (case when a.net_sales is null or a.net_sales='' then 0 else a.net_sales end) as net_sales, (case when a.gross_sales is null or a.gross_sales='' then 0 else a.gross_sales end) as gross_sales, (case when a.cost_price_atos is null or a.cost_price_atos='' then 0 else a.cost_price_atos end) as cost_price_atos, a.sales_date, a.channel_code from consultas.rep_pr_sales_detail a inner join tienda_zona_territorial_tipo_tienda b on a.branch_no=b.branch_no where a.sales_date<=FECHA_FINAL_ANT and a.sales_date>=FECHA_INICIAL_ANT; drop table if exists ventas_anio_anterior; create table ventas_anio_anterior as select b.semana_jd as semana, weekday(a.sales_date) as dia, a.product_code, a.branch_no, a.nombre_tienda, a.tipo_tienda, a.subzona, a.delegado, a.zona, a.territorial, sum(a.qty) as venta_uds_anio_anterior, sum(a.net_sales) as venta_neta_anio_anterior, sum(a.gross_sales) as venta_bruta_anio_anterior, sum(a.cost_price_atos) as coste_anio_anterior, sum(a.net_sales-a.cost_price_atos) as margen_anio_anterior, 0 as stock_anio_anterior, 0 as stock_unidades_anterior, 0 as venta_uds_ultima_semana_anio_anterior, 0 as venta_neta_ultima_semana_anio_anterior, 0 as margen_ultima_semana_anio_anterior from ventas_anio_anterior_pre_1 a left join md_dbg.calendario_detalle_jd_v2 b on a.sales_date=b.fecha_comp GROUP BY b.semana_jd,a.sales_date,a.product_code,a.branch_no,a.tipo_tienda,a.subzona,a.delegado,a.zona,a.territorial; drop table if exists ventas_anio_anterior_app; create table ventas_anio_anterior_app as select b.semana_jd as semana, weekday(a.sales_date) as dia, a.product_code, a.branch_no, a.nombre_tienda, a.tipo_tienda, a.subzona, a.delegado, a.zona, a.territorial, sum(a.qty) as venta_uds_anio_anterior, sum(a.net_sales) as venta_neta_anio_anterior, sum(a.gross_sales) as venta_bruta_anio_anterior, sum(a.cost_price_atos) as coste_anio_anterior, sum(a.net_sales-a.cost_price_atos) as margen_anio_anterior, 0 as stock_anio_anterior, 0 as stock_unidades_anterior, 0 as venta_uds_ultima_semana_anio_anterior, 0 as venta_neta_ultima_semana_anio_anterior, 0 as margen_ultima_semana_anio_anterior from ventas_anio_anterior_pre_1 a left join md_dbg.calendario_detalle_jd_v2 b on a.sales_date=b.fecha_comp where a.channel_code='3' and a.channel_code<>'' GROUP by b.semana_jd,a.sales_date,a.product_code,a.branch_no,a.tipo_tienda,a.subzona,a.delegado,a.zona,a.territorial; drop table if exists ventas_ult_semana_anio_anterior; create table ventas_ult_semana_anio_anterior as select b.semana_jd as semana, weekday(a.sales_date) as dia, a.product_code, a.branch_no, a.nombre_tienda, a.tipo_tienda, a.subzona, a.delegado, a.zona, a.territorial, sum(a.qty) as venta_uds_ultima_semana_anio_anterior, sum(a.net_sales) as venta_neta_ultima_semana_anio_anterior, sum(a.net_sales-a.cost_price_atos) as margen_ultima_semana_anio_anterior, 0 as venta_uds_anio_anterior, 0 as venta_neta_anio_anterior, 0 as venta_bruta_anio_anterior, 0 as coste_anio_anterior, 0 as margen_anio_anterior, 0 as stock_anio_anterior, 0 as stock_unidades_anterior from ventas_anio_anterior_pre_1 a left join md_dbg.calendario_detalle_jd_v2 b on a.sales_date=b.fecha_comp where a.sales_date<=FECHA_FINAL_ANT and a.sales_date>=(select min(Fecha) from md_dbg.calendario_detalle_jd_v2 where Semana_jd=(select Semana_jd from md_dbg.calendario_detalle_jd_v2 where Fecha=FECHA_FINAL_ANT) and Anio_jd=(select Anio_jd from md_dbg.calendario_detalle_jd_v2 where Fecha=FECHA_FINAL_ANT)) group by b.semana_jd,a.sales_date,a.product_code,a.branch_no,a.nombre_tienda,a.tipo_tienda,a.subzona,a.delegado,a.zona,a.territorial; drop table if exists ventas_ult_semana_anio_anterior_app; create table ventas_ult_semana_anio_anterior_app as select b.semana_jd as semana, weekday(a.sales_date) as dia, a.product_code, a.branch_no, a.nombre_tienda, a.tipo_tienda, a.subzona, a.delegado, a.zona, a.territorial, sum(a.qty) as venta_uds_ultima_semana_anio_anterior, sum(a.net_sales) as venta_neta_ultima_semana_anio_anterior, sum(a.net_sales-a.cost_price_atos) as margen_ultima_semana_anio_anterior, 0 as venta_uds_anio_anterior, 0 as venta_neta_anio_anterior, 0 as venta_bruta_anio_anterior, 0 as coste_anio_anterior, 0 as margen_anio_anterior, 0 as stock_anio_anterior, 0 as stock_unidades_anterior from ventas_anio_anterior_pre_1 a left join md_dbg.calendario_detalle_jd_v2 b on a.sales_date=b.fecha_comp where a.sales_date<=FECHA_FINAL_ANT and a.sales_date>=(select min(Fecha) from md_dbg.calendario_detalle_jd_v2 where Semana_jd=(select Semana_jd from md_dbg.calendario_detalle_jd_v2 where Fecha=FECHA_FINAL_ANT) and Anio_jd=(select Anio_jd from md_dbg.calendario_detalle_jd_v2 where Fecha=FECHA_FINAL_ANT)) and a.channel_code='3' and a.channel_code<>'' group by b.semana_jd,a.sales_date,a.product_code,a.branch_no,a.nombre_tienda,a.tipo_tienda,a.subzona,a.delegado,a.zona,a.territorial; /*-----------stock anio anterior---------------*/ /*modificacion compara 19-22*/ drop table if exists stock_anio_anterior_pre; create table stock_anio_anterior_pre as select fecha_fin,product_code,branch_no,uds,valoracion from md_dbg.stock_Semanas where Fecha_fin=FECHA_FINAL_ANT and branch_no in (select branch_no from tiendas_ica); /*-----------stock anio anterior---------------*/ /*modificacion compara año actual vs año anterior*/ drop table if exists stock_anio_anterior_pre_2; create table stock_anio_anterior_pre_2 as select b.semana_jd as semana, weekday(a.Fecha_fin) as dia, a.product_code, a.branch_no, sum(a.valoracion) as stock_anio_anterior, sum(a.uds) as stock_unidades_anterior from stock_anio_anterior_pre a left join md_dbg.calendario_detalle_jd_v2 b on a.fecha_fin=b.fecha_comp group by b.semana_jd,a.Fecha_fin,a.product_code,a.branch_no; drop table if exists stock_anio_anterior; create table stock_anio_anterior as select a.semana, a.dia, a.product_code, a.branch_no, (case when b.nombre_tienda is null or b.nombre_tienda='' then 'na' else b.nombre_tienda end) as nombre_tienda, (case when b.tipo_tienda is null or b.tipo_tienda='' then 'na' else b.tipo_tienda end) as tipo_tienda, (case when b.subzona is null or b.subzona='' then 'na' else b.subzona end) as subzona, (case when b.delegado is null or b.delegado='' then 'na' else b.delegado end) as delegado, (case when b.zona is null or b.zona='' then 'na' else b.zona end) as zona, (case when b.territorial is null or b.territorial='' then 'na' else b.territorial end) as territorial, sum(a.stock_anio_anterior) as stock_anio_anterior, sum(a.stock_unidades_anterior) as stock_unidades_anterior, 0 as venta_uds_anio_anterior, 0 as venta_neta_anio_anterior, 0 as venta_bruta_anio_anterior, 0 as coste_anio_anterior, 0 as margen_anio_anterior, 0 as venta_uds_ultima_semana_anio_anterior, 0 as venta_neta_ultima_semana_anio_anterior, 0 as margen_ultima_semana_anio_anterior from stock_anio_anterior_pre_2 a left join tienda_zona_territorial_tipo_tienda b on a.branch_no=b.branch_no group by semana,dia,product_code,branch_no,tipo_tienda,subzona,delegado,zona,territorial; drop table if exists ventas_stock_anterior; create table ventas_stock_anterior as select semana,dia,product_code,branch_no,nombre_tienda,tipo_tienda,subzona,delegado,zona,territorial,stock_anio_anterior,stock_unidades_anterior,venta_uds_anio_anterior, venta_neta_anio_anterior,venta_bruta_anio_anterior,coste_anio_anterior,margen_anio_anterior,venta_uds_ultima_semana_anio_anterior, venta_neta_ultima_semana_anio_anterior,margen_ultima_semana_anio_anterior from stock_anio_anterior union all select semana,dia,product_code,branch_no,nombre_tienda,tipo_tienda,subzona,delegado,zona,territorial,stock_anio_anterior,stock_unidades_anterior,venta_uds_anio_anterior, venta_neta_anio_anterior,venta_bruta_anio_anterior,coste_anio_anterior,margen_anio_anterior,venta_uds_ultima_semana_anio_anterior, venta_neta_ultima_semana_anio_anterior,margen_ultima_semana_anio_anterior from ventas_anio_anterior union all select semana,dia,product_code,branch_no,nombre_tienda,tipo_tienda,subzona,delegado,zona,territorial,stock_anio_anterior,stock_unidades_anterior,venta_uds_anio_anterior, venta_neta_anio_anterior,venta_bruta_anio_anterior,coste_anio_anterior,margen_anio_anterior,venta_uds_ultima_semana_anio_anterior, venta_neta_ultima_semana_anio_anterior,margen_ultima_semana_anio_anterior from ventas_ult_semana_anio_anterior; drop table if exists ventas_stock_anterior_app; create table ventas_stock_anterior_app as select semana,dia,product_code,branch_no,nombre_tienda,tipo_tienda,subzona,delegado,zona,territorial,stock_anio_anterior,stock_unidades_anterior,venta_uds_anio_anterior, venta_neta_anio_anterior,venta_bruta_anio_anterior,coste_anio_anterior,margen_anio_anterior,venta_uds_ultima_semana_anio_anterior, venta_neta_ultima_semana_anio_anterior,margen_ultima_semana_anio_anterior from stock_anio_anterior union all select semana,dia,product_code,branch_no,nombre_tienda,tipo_tienda,subzona,delegado,zona,territorial,stock_anio_anterior,stock_unidades_anterior,venta_uds_anio_anterior, venta_neta_anio_anterior,venta_bruta_anio_anterior,coste_anio_anterior,margen_anio_anterior,venta_uds_ultima_semana_anio_anterior, venta_neta_ultima_semana_anio_anterior,margen_ultima_semana_anio_anterior from ventas_anio_anterior_app union all select semana,dia,product_code,branch_no,nombre_tienda,tipo_tienda,subzona,delegado,zona,territorial,stock_anio_anterior,stock_unidades_anterior,venta_uds_anio_anterior, venta_neta_anio_anterior,venta_bruta_anio_anterior,coste_anio_anterior,margen_anio_anterior,venta_uds_ultima_semana_anio_anterior, venta_neta_ultima_semana_anio_anterior,margen_ultima_semana_anio_anterior from ventas_ult_semana_anio_anterior_app; drop table if exists ventas_stock_anterior_agru; create table ventas_stock_anterior_agru as select semana, dia, product_code, branch_no, nombre_tienda, tipo_tienda, subzona, delegado, zona, territorial, sum(stock_anio_anterior) as stock_anio_anterior, sum(stock_unidades_anterior) as stock_unidades_anterior, sum(venta_uds_anio_anterior) as venta_uds_anio_anterior, sum(venta_neta_anio_anterior) as venta_neta_anio_anterior, sum(venta_bruta_anio_anterior) as venta_bruta_anio_anterior, sum(coste_anio_anterior) as coste_anio_anterior, sum(margen_anio_anterior) as margen_anio_anterior, sum(venta_uds_ultima_semana_anio_anterior) as venta_uds_ultima_semana_anio_anterior, sum(venta_neta_ultima_semana_anio_anterior) as venta_neta_ultima_semana_anio_anterior, sum(margen_ultima_semana_anio_anterior) as margen_ultima_semana_anio_anterior, 0 as stock_anio_actual, 0 as stock_unidades_actual, 0 as venta_uds_anio_actual, 0 as venta_neta_anio_actual, 0 as venta_bruta_anio_actual, 0 as coste_anio_actual, 0 as margen_anio_actual, 0 as venta_uds_ultima_semana_anio_actual, 0 as venta_neta_ultima_semana_anio_actual, 0 as margen_ultima_semana_anio_actual from ventas_stock_anterior where stock_unidades_anterior+stock_anio_anterior+venta_uds_anio_anterior+venta_neta_anio_anterior+venta_bruta_anio_anterior+venta_uds_ultima_semana_anio_anterior+margen_anio_anterior<>0 group by semana,dia,product_code,branch_no,tipo_tienda,subzona,delegado,zona,territorial; drop table if exists ventas_stock_anterior_agru_app; create table ventas_stock_anterior_agru_app as select semana, dia, product_code, branch_no, nombre_tienda, tipo_tienda, subzona, delegado, zona, territorial, sum(stock_anio_anterior) as stock_anio_anterior, sum(stock_unidades_anterior) as stock_unidades_anterior, sum(venta_uds_anio_anterior) as venta_uds_anio_anterior, sum(venta_neta_anio_anterior) as venta_neta_anio_anterior, sum(venta_bruta_anio_anterior) as venta_bruta_anio_anterior, sum(coste_anio_anterior) as coste_anio_anterior, sum(margen_anio_anterior) as margen_anio_anterior, sum(venta_uds_ultima_semana_anio_anterior) as venta_uds_ultima_semana_anio_anterior, sum(venta_neta_ultima_semana_anio_anterior) as venta_neta_ultima_semana_anio_anterior, sum(margen_ultima_semana_anio_anterior) as margen_ultima_semana_anio_anterior, 0 as stock_anio_actual, 0 as stock_unidades_actual, 0 as venta_uds_anio_actual, 0 as venta_neta_anio_actual, 0 as venta_bruta_anio_actual, 0 as coste_anio_actual, 0 as margen_anio_actual, 0 as venta_uds_ultima_semana_anio_actual, 0 as venta_neta_ultima_semana_anio_actual, 0 as margen_ultima_semana_anio_actual from ventas_stock_anterior_app where stock_unidades_anterior+stock_anio_anterior+venta_uds_anio_anterior+venta_neta_anio_anterior+venta_bruta_anio_anterior+venta_uds_ultima_semana_anio_anterior+margen_anio_anterior<>0 group by semana,dia,product_code,branch_no,tipo_tienda,subzona,delegado,zona,territorial; /*eliminamos tablas temporales */ drop table if exists ventas_anio_anterior_pre_1; drop table if exists ventas_anio_anterior_pre_2; drop table if exists stock_anio_anterior_pre; drop table if exists stock_anio_anterior_pre_1; drop table if exists stock_anio_anterior_pre_2; drop table if exists stock_anio_anterior; drop table if exists ventas_stock_anterior; drop table if exists ventas_stock_anterior_app; drop table if exists ventas_ult_semana_anio_anterior; drop table if exists ventas_ult_semana_anio_anterior_app; /*--------------------------------*/ /* año actual y año anterior */ drop table if exists ventas_stock_pre_11; create table ventas_stock_pre_11 as select semana,dia,product_code,branch_no,nombre_tienda,tipo_tienda,subzona,delegado,zona,territorial,venta_uds_anio_actual,venta_uds_anio_anterior, venta_neta_anio_actual,venta_bruta_anio_actual,venta_neta_anio_anterior,venta_bruta_anio_anterior,coste_anio_actual,coste_anio_anterior,margen_anio_actual, margen_anio_anterior,stock_anio_actual,stock_anio_anterior,stock_unidades_actual,stock_unidades_anterior,venta_uds_ultima_semana_anio_actual,venta_uds_ultima_semana_anio_anterior,venta_neta_ultima_semana_anio_actual,venta_neta_ultima_semana_anio_anterior, margen_ultima_semana_anio_actual,margen_ultima_semana_anio_anterior from ventas_stock_actual_agru union all select semana,dia,product_code,branch_no,nombre_tienda,tipo_tienda,subzona,delegado,zona,territorial,venta_uds_anio_actual,venta_uds_anio_anterior, venta_neta_anio_actual,venta_bruta_anio_actual,venta_neta_anio_anterior,venta_bruta_anio_anterior,coste_anio_actual,coste_anio_anterior,margen_anio_actual, margen_anio_anterior,stock_anio_actual,stock_anio_anterior,stock_unidades_actual,stock_unidades_anterior,venta_uds_ultima_semana_anio_actual,venta_uds_ultima_semana_anio_anterior,venta_neta_ultima_semana_anio_actual,venta_neta_ultima_semana_anio_anterior, margen_ultima_semana_anio_actual,margen_ultima_semana_anio_anterior from ventas_stock_anterior_agru; alter table ventas_stock_pre_11 add index (semana,dia,product_code,branch_no,tipo_tienda,subzona,delegado,zona,territorial); /*borrado tablas temporales*/ drop table if exists ventas_stock_actual_agru; drop table if exists ventas_stock_anterior_agru; /*-----------------------*/ /* año actual y año anterior APP */ drop table if exists ventas_stock_pre_1_app; create table ventas_stock_pre_1_app as select semana,dia,product_code,branch_no,nombre_tienda,tipo_tienda,subzona,delegado,zona,territorial,venta_uds_anio_actual,venta_uds_anio_anterior, venta_neta_anio_actual,venta_bruta_anio_actual,venta_neta_anio_anterior,venta_bruta_anio_anterior,coste_anio_actual,coste_anio_anterior,margen_anio_actual, margen_anio_anterior,stock_anio_actual,stock_anio_anterior,stock_unidades_actual,stock_unidades_anterior,venta_uds_ultima_semana_anio_actual,venta_uds_ultima_semana_anio_anterior,venta_neta_ultima_semana_anio_actual,venta_neta_ultima_semana_anio_anterior, margen_ultima_semana_anio_actual,margen_ultima_semana_anio_anterior from ventas_stock_actual_agru_app union all select semana,dia,product_code,branch_no,nombre_tienda,tipo_tienda,subzona,delegado,zona,territorial,venta_uds_anio_actual,venta_uds_anio_anterior, venta_neta_anio_actual,venta_bruta_anio_actual,venta_neta_anio_anterior,venta_bruta_anio_anterior,coste_anio_actual,coste_anio_anterior,margen_anio_actual, margen_anio_anterior,stock_anio_actual,stock_anio_anterior,stock_unidades_actual,stock_unidades_anterior,venta_uds_ultima_semana_anio_actual,venta_uds_ultima_semana_anio_anterior,venta_neta_ultima_semana_anio_actual,venta_neta_ultima_semana_anio_anterior, margen_ultima_semana_anio_actual,margen_ultima_semana_anio_anterior from ventas_stock_anterior_agru_app; alter table ventas_stock_pre_1_app add index (semana,dia,product_code,branch_no,tipo_tienda,subzona,delegado,zona,territorial); /* borrado tablas temporales APP */ drop table if exists ventas_stock_actual_agru_app; drop table if exists ventas_stock_anterior_agru_app; /*-----------------------*/ drop table if exists ventas_stock_pre_21; create table ventas_stock_pre_21 as select semana, dia, product_code, branch_no, nombre_tienda, tipo_tienda, subzona, delegado, zona, territorial, sum(venta_uds_anio_actual) as venta_uds_anio_actual, sum(venta_uds_anio_anterior) as venta_uds_anio_anterior, sum(venta_neta_anio_actual) as venta_neta_anio_actual, sum(venta_neta_anio_anterior) as venta_neta_anio_anterior, sum(venta_bruta_anio_actual) as venta_bruta_anio_actual, sum(venta_bruta_anio_anterior) as venta_bruta_anio_anterior, sum(coste_anio_actual) as coste_anio_actual, sum(coste_anio_anterior) as coste_anio_anterior, sum(margen_anio_actual) as margen_anio_actual, sum(margen_anio_anterior) as margen_anio_anterior, sum(stock_anio_actual) as stock_anio_actual, sum(stock_anio_anterior) as stock_anio_anterior, sum(stock_unidades_actual) as stock_unidades_actual, sum(stock_unidades_anterior) as stock_unidades_anterior, sum(venta_uds_ultima_semana_anio_actual) as venta_uds_ultima_semana_anio_actual, sum(venta_uds_ultima_semana_anio_anterior) as venta_uds_ultima_semana_anio_anterior, sum(venta_neta_ultima_semana_anio_actual) as venta_neta_ultima_semana_anio_actual, sum(venta_neta_ultima_semana_anio_anterior) as venta_neta_ultima_semana_anio_anterior, sum(margen_ultima_semana_anio_actual) as margen_ultima_semana_anio_actual, sum(margen_ultima_semana_anio_anterior) as margen_ultima_semana_anio_anterior from ventas_stock_pre_11 group by semana,dia,product_code,branch_no,tipo_tienda,subzona,delegado,zona,territorial; alter table ventas_stock_pre_21 add index indice (semana,dia,product_code,branch_no); alter table LFL add index indice (semana,dia,product_code,branch_no); drop table if exists ventas_stock_pre_3; create table ventas_stock_pre_3 as select a.semana, a.dia, a.product_code, a.branch_no, a.nombre_tienda, a.tipo_tienda, a.subzona, a.delegado, a.zona, a.territorial, b.vta_uds_aact as venta_uds_anio_actual_lfl, b.vta_uds_aant as venta_uds_anio_anterior_lfl, b.vta_neta_aact as venta_neta_anio_actual_lfl, b.vta_bruta_aact as venta_bruta_anio_actual_lfl, b.vta_neta_aant as venta_neta_anio_anterior_lfl, b.vta_bruta_aant as venta_bruta_anio_anterior_lfl, b.mrg_aact as margen_anio_actual_lfl, b.mrg_aant as margen_anio_anterior_lfl from ventas_stock_pre_21 a INNER join LFL b on a.product_code=b.product_code and a.branch_no=b.branch_no and a.semana=b.semana and a.dia=b.dia; alter table ventas_stock_pre_3 add index indice (semana,dia,product_code,branch_no); /*--------------------------------*/ drop table if exists ventas_stock_pre_41; create table ventas_stock_pre_41 as select a.*, coalesce(b.venta_uds_anio_actual_lfl,0) as venta_uds_anio_actual_lfl, coalesce(b.venta_uds_anio_anterior_lfl,0) as venta_uds_anio_anterior_lfl, coalesce(b.venta_neta_anio_actual_lfl,0) as venta_neta_anio_actual_lfl, coalesce(b.venta_bruta_anio_actual_lfl,0) as venta_bruta_anio_actual_lfl, coalesce(b.venta_neta_anio_anterior_lfl,0) as venta_neta_anio_anterior_lfl, coalesce(b.venta_bruta_anio_anterior_lfl,0) as venta_bruta_anio_anterior_lfl, coalesce(b.margen_anio_actual_lfl,0) as margen_anio_actual_lfl, coalesce(b.margen_anio_anterior_lfl,0) as margen_anio_anterior_lfl from ventas_stock_pre_21 a left join ventas_stock_pre_3 b on a.product_code=b.product_code and a.branch_no=b.branch_no and a.semana=b.semana and a.dia=b.dia; alter table ventas_stock_pre_41 add index (branch_no); /*--------------------------------*/ drop table if exists ventas_stock_pre_5; create table ventas_stock_pre_5 as select product_code, branch_no, semana, dia, stock_anio_actual as stock_anio_actual_alm, stock_anio_anterior as stock_anio_anterior_alm, stock_unidades_actual as stock_uds_anio_actual_alm, stock_unidades_anterior as stock_uds_anio_anterior_alm from ventas_stock_pre_41 where branch_no in (select branch_no from tiendas_ica where is_store='0') or branch_no in (select cod_spz from tiendas_ica where is_store='0'); alter table ventas_stock_pre_41 add index indice (product_code,branch_no,semana,dia); alter table ventas_stock_pre_5 add index indice (product_code,branch_no,semana,dia); drop table if exists ventas_stock_pre_61; create table ventas_stock_pre_61 as select a.*, (case when b.stock_anio_actual_alm='' or b.stock_anio_actual_alm is null then 0 else b.stock_anio_actual_alm end) AS stock_anio_actual_alm, (case when b.stock_anio_anterior_alm='' or b.stock_anio_anterior_alm is null then 0 else b.stock_anio_anterior_alm end) AS stock_anio_anterior_alm, (case when b.stock_uds_anio_actual_alm='' or b.stock_uds_anio_actual_alm is null then 0 else b.stock_uds_anio_actual_alm end) AS stock_uds_anio_actual_alm, (case when b.stock_uds_anio_anterior_alm='' or b.stock_uds_anio_anterior_alm is null then 0 else b.stock_uds_anio_anterior_alm end) AS stock_uds_anio_anterior_alm from ventas_stock_pre_41 a left join ventas_stock_pre_5 b on a.product_code=b.product_code and a.branch_no=b.branch_no and a.semana=b.semana and a.dia=b.dia; alter table ventas_stock_pre_61 add index indice (product_code,branch_no); drop table if exists ventas_stock_pre_71; create table ventas_stock_pre_71 as select a.*, coalesce(b.descripcion,coalesce(e.descripcion,'na')) as descripcion, coalesce(b.original_price,coalesce(e.original_price,0)) as precio_original, coalesce(b.area,coalesce(e.area,'na')) as area, coalesce(b.serie,coalesce(e.serie,'na')) as serie, coalesce(b.serie1,coalesce(e.serie1,'na')) as serie1, coalesce(b.seccion_a,coalesce(e.seccion_a,'na')) as seccion_a, coalesce(b.subseccion_a,coalesce(e.subseccion_a,'na')) as subseccion_a, coalesce(b.seccion,coalesce(e.seccion,'na')) as seccion, coalesce(b.familia,coalesce(e.familia,'na')) as familia, coalesce(b.disenyo,coalesce(e.disenyo,'na')) as disenyo, coalesce(b.marca,coalesce(e.marca,'na')) as marca, coalesce(b.udp_code,coalesce(e.udp_code,'na')) as udp_code, coalesce(b.udp_name,coalesce(e.udp_name,'na')) as udp_name, coalesce(b.brand_type,coalesce(e.brand_type,'na')) as brand_type, coalesce(d.brand_desc,coalesce(f.brand_desc,'na')) as brand_desc, coalesce(b.temporada,coalesce(e.temporada,'na')) as Temporada, coalesce(b.analytical_color,coalesce(e.analytical_color,'na')) as analytical_color, case when coalesce(b.temporal,e.temporal) = '0' then 'temporal' when coalesce(b.temporal,e.temporal) = '1' then 'intemporal' else 'na' end as TEMPORALIDAD, coalesce(b.DESCATALOGADO,coalesce(e.DESCATALOGADO,'na')) as DESCATALOGADO, coalesce(c.Is_Store,g.Is_Store) as EsTienda, coalesce(c.Mercado,g.Mercado) as Mercado, coalesce(c.fare_code,g.fare_code) as fare_code from ventas_stock_pre_61 a left join consultas.articulos b on a.product_code=b.product_code left join consultas.articulos_pt e on a.product_code=e.product_code left join tiendas_ica c on a.branch_no=c.branch_no left join tiendas_ica g on a.branch_no=g.cod_spz left join md_dbg.brand_type d on b.brand_type=d.brand_type left join md_dbg.brand_type f on e.brand_type=f.brand_type; alter table ventas_stock_pre_71 add index (semana); drop table if exists ventas_stock_pre_81; create table ventas_stock_pre_81 as select a.*, b.mes_jd as mes from ventas_stock_pre_71 a left join (select distinct mes_jd,semana_jd from md_dbg.calendario_detalle_jd_v2 where anio_jd=ANIO_ACTUAL) b on a.semana=b.semana_jd; /* Incorporamos el precio actual para artículo y día */ drop table if exists precios; create table precios as select a.fare_code, a.product_code, a.price_after as precio_actual from md_dbg.pvps_fares a inner join (select fare_code, product_code, max(pvp_Date) as fecha from md_dbg.pvps_fares where pvp_Date<=FECHA_FINAL group by fare_code,product_code) b on a.product_code=b.product_code and a.pvp_Date=b.fecha and a.fare_code=b.fare_code; alter table precios add index (product_code,fare_code); alter table ventas_stock_pre_81 add index (product_code,fare_code); drop table if exists ventas_stock1; create table ventas_stock1 as select a.*, coalesce(b.precio_actual,a.precio_original) as precio_actual from ventas_stock_pre_81 a left join precios b on a.product_code=b.product_code and a.fare_code=b.fare_code; alter table ventas_stock1 add index (mercado); /* borra tablas temporales */ drop table if exists ventas_stock_pre_1; drop table if exists ventas_stock_pre_21; drop table if exists ventas_stock_pre_22; drop table if exists ventas_stock_pre_3; drop table if exists ventas_stock_pre_41; drop table if exists ventas_stock_pre_5; drop table if exists ventas_stock_pre_61; drop table if exists ventas_stock_pre_62; drop table if exists ventas_stock_pre_71; drop table if exists ventas_stock_pre_72; drop table if exists ventas_stock_pre_81; drop table if exists ventas_stock_pre_82; drop table if exists precios; /*----------------------------------------------------------------------------*/ /* Lo mismo para la venta APP*/ drop table if exists ventas_stock_pre_2_app; create table ventas_stock_pre_2_app as select semana, dia, product_code, branch_no, nombre_tienda, tipo_tienda, subzona, delegado, zona, territorial, sum(venta_uds_anio_actual) as venta_uds_anio_actual, sum(venta_uds_anio_anterior) as venta_uds_anio_anterior, sum(venta_neta_anio_actual) as venta_neta_anio_actual, sum(venta_neta_anio_anterior) as venta_neta_anio_anterior, sum(venta_bruta_anio_actual) as venta_bruta_anio_actual, sum(venta_bruta_anio_anterior) as venta_bruta_anio_anterior, sum(coste_anio_actual) as coste_anio_actual, sum(coste_anio_anterior) as coste_anio_anterior, sum(margen_anio_actual) as margen_anio_actual, sum(margen_anio_anterior) as margen_anio_anterior, sum(stock_anio_actual) as stock_anio_actual, sum(stock_anio_anterior) as stock_anio_anterior, sum(stock_unidades_actual) as stock_unidades_actual, sum(stock_unidades_anterior) as stock_unidades_anterior, sum(venta_uds_ultima_semana_anio_actual) as venta_uds_ultima_semana_anio_actual, sum(venta_uds_ultima_semana_anio_anterior) as venta_uds_ultima_semana_anio_anterior, sum(venta_neta_ultima_semana_anio_actual) as venta_neta_ultima_semana_anio_actual, sum(venta_neta_ultima_semana_anio_anterior) as venta_neta_ultima_semana_anio_anterior, sum(margen_ultima_semana_anio_actual) as margen_ultima_semana_anio_actual, sum(margen_ultima_semana_anio_anterior) as margen_ultima_semana_anio_anterior from ventas_stock_pre_1_app group by semana,dia,product_code,branch_no,tipo_tienda,subzona,delegado,zona,territorial; alter table ventas_stock_pre_2_app add index indice (semana,dia,product_code,branch_no); drop table if exists ventas_stock_pre_3_app; create table ventas_stock_pre_3_app as select a.semana, a.dia, a.product_code, a.branch_no, a.nombre_tienda, a.tipo_tienda, a.subzona, a.delegado, a.zona, a.territorial, a.venta_uds_anio_actual as venta_uds_anio_actual_lfl, a.venta_uds_anio_anterior as venta_uds_anio_anterior_lfl, a.venta_neta_anio_actual as venta_neta_anio_actual_lfl, a.venta_bruta_anio_actual as venta_bruta_anio_actual_lfl, a.venta_neta_anio_anterior as venta_neta_anio_anterior_lfl, a.venta_bruta_anio_anterior as venta_bruta_anio_anterior_lfl, a.margen_anio_actual as margen_anio_actual_lfl, a.margen_anio_anterior as margen_anio_anterior_lfl from ventas_stock_pre_2_app a INNER join LFL b on a.product_code=b.product_code and a.branch_no=b.branch_no and a.semana=b.semana and a.dia=b.dia; alter table ventas_stock_pre_3_app add index indice (semana,dia,product_code,branch_no); drop table if exists ventas_stock_pre_4_app; create table ventas_stock_pre_4_app as select a.*, coalesce(b.venta_uds_anio_actual_lfl,0) as venta_uds_anio_actual_lfl, coalesce(b.venta_uds_anio_anterior_lfl,0) as venta_uds_anio_anterior_lfl, coalesce(b.venta_neta_anio_actual_lfl,0) as venta_neta_anio_actual_lfl, coalesce(b.venta_bruta_anio_actual_lfl,0) as venta_bruta_anio_actual_lfl, coalesce(b.venta_neta_anio_anterior_lfl,0) as venta_neta_anio_anterior_lfl, coalesce(b.venta_bruta_anio_anterior_lfl,0) as venta_bruta_anio_anterior_lfl, coalesce(b.margen_anio_actual_lfl,0) as margen_anio_actual_lfl, coalesce(b.margen_anio_anterior_lfl,0) as margen_anio_anterior_lfl from ventas_stock_pre_2_app a left join ventas_stock_pre_3_app b on a.product_code=b.product_code and a.branch_no=b.branch_no and a.semana=b.semana and a.dia=b.dia; alter table ventas_stock_pre_4_app add index (branch_no); drop table if exists ventas_stock_pre_5_app; create table ventas_stock_pre_5_app as select product_code, branch_no, semana, dia, stock_anio_actual as stock_anio_actual_alm, stock_anio_anterior as stock_anio_anterior_alm, stock_unidades_actual as stock_uds_anio_actual_alm, stock_unidades_anterior as stock_uds_anio_anterior_alm from ventas_stock_pre_4_app where branch_no in (select branch_no from tiendas_ica where is_store='0') or branch_no in (select cod_spz from tiendas_ica where is_store='0'); alter table ventas_stock_pre_4_app add index indice (product_code,branch_no,semana,dia); alter table ventas_stock_pre_5_app add index indice (product_code,branch_no,semana,dia); drop table if exists ventas_stock_pre_6_app; create table ventas_stock_pre_6_app as select a.*, (case when b.stock_anio_actual_alm='' or b.stock_anio_actual_alm is null then 0 else b.stock_anio_actual_alm end) AS stock_anio_actual_alm, (case when b.stock_anio_anterior_alm='' or b.stock_anio_anterior_alm is null then 0 else b.stock_anio_anterior_alm end) AS stock_anio_anterior_alm, (case when b.stock_uds_anio_actual_alm='' or b.stock_uds_anio_actual_alm is null then 0 else b.stock_uds_anio_actual_alm end) AS stock_uds_anio_actual_alm, (case when b.stock_uds_anio_anterior_alm='' or b.stock_uds_anio_anterior_alm is null then 0 else b.stock_uds_anio_anterior_alm end) AS stock_uds_anio_anterior_alm from ventas_stock_pre_4_app a left join ventas_stock_pre_5_app b on a.product_code=b.product_code and a.branch_no=b.branch_no and a.semana=b.semana and a.dia=b.dia; alter table ventas_stock_pre_6_app add index indice (product_code,branch_no); drop table if exists ventas_stock_pre_7_app; create table ventas_stock_pre_7_app as select a.*, coalesce(b.descripcion,coalesce(e.descripcion,'na')) as descripcion, coalesce(b.original_price,coalesce(e.original_price,0)) as precio_original, coalesce(b.area,coalesce(e.area,'na')) as area, coalesce(b.serie,coalesce(e.serie,'na')) as serie, coalesce(b.serie1,coalesce(e.serie1,'na')) as serie1, coalesce(b.seccion_a,coalesce(e.seccion_a,'na')) as seccion_a, coalesce(b.subseccion_a,coalesce(e.subseccion_a,'na')) as subseccion_a, coalesce(b.seccion,coalesce(e.seccion,'na')) as seccion, coalesce(b.familia,coalesce(e.familia,'na')) as familia, coalesce(b.disenyo,coalesce(e.disenyo,'na')) as disenyo, coalesce(b.marca,coalesce(e.marca,'na')) as marca, coalesce(b.udp_code,coalesce(e.udp_code,'na')) as udp_code, coalesce(b.udp_name,coalesce(e.udp_name,'na')) as udp_name, coalesce(b.brand_type,coalesce(e.brand_type,'na')) as brand_type, coalesce(d.brand_desc,coalesce(f.brand_desc,'na')) as brand_desc, coalesce(b.temporada,coalesce(e.temporada,'na')) as Temporada, coalesce(b.analytical_color,coalesce(e.analytical_color,'na')) as analytical_color, /*coalesce((case when b.temporal=0 then 'temporal' else 'intemporal' end),coalesce((case when e.temporal=0 then 'temporal' else 'intemporal' end),'na')) as TEMPORALIDAD,*/ case when coalesce(b.temporal,e.temporal) = '0' then 'temporal' when coalesce(b.temporal,e.temporal) = '1' then 'intemporal' else 'na' end as TEMPORALIDAD, coalesce(b.DESCATALOGADO,coalesce(e.DESCATALOGADO,'na')) as DESCATALOGADO, coalesce(c.Is_Store,g.Is_Store) as EsTienda, coalesce(c.Mercado,g.Mercado) as Mercado, coalesce(c.fare_code,g.fare_code) as fare_code from ventas_stock_pre_6_app a left join consultas.articulos b on a.product_code=b.product_code left join consultas.articulos_pt e on a.product_code=e.product_code left join tiendas_ica c on a.branch_no=c.branch_no left join tiendas_ica g on a.branch_no=g.cod_spz left join md_dbg.brand_type d on b.brand_type=d.brand_type left join md_dbg.brand_type f on e.brand_type=f.brand_type /*left join md_dbg.products_original_price_fare p on a.product_code=p.product_code and c.fare_code=p.fare_code*/; alter table ventas_stock_pre_7_app add index (semana); drop table if exists ventas_stock_pre_8_app; create table ventas_stock_pre_8_app as select a.*, b.mes_jd as mes from ventas_stock_pre_7_app a left join (select distinct mes_jd,semana_jd from md_dbg.calendario_detalle_jd_v2 where anio_jd=ANIO_ACTUAL) b on a.semana=b.semana_jd; /* Incorporamos el precio actual para artículo y día */ drop table if exists precios_app; create table precios_app as select a.fare_code, a.product_code, a.price_after as precio_actual from md_dbg.pvps_fares a inner join (select fare_code, product_code, max(pvp_Date) as fecha from md_dbg.pvps_fares where pvp_Date<=FECHA_FINAL group by product_code,fare_code) b on a.product_code=b.product_code and a.pvp_Date=b.fecha and a.fare_code=b.fare_code; alter table precios_app add index (product_code,fare_code); alter table ventas_stock_pre_8_app add index (product_code,fare_code); drop table if exists ventas_stock_app; create table ventas_stock_app as select a.*, coalesce(b.precio_actual,a.precio_original) as precio_actual from ventas_stock_pre_8_app a left join precios_app b on a.product_code=b.product_code and a.fare_code=b.fare_code; /* borra tablas temporales */ drop table if exists ventas_stock_pre_1_app; drop table if exists ventas_stock_pre_2_app; drop table if exists ventas_stock_pre_3_app; drop table if exists ventas_stock_pre_4_app; drop table if exists ventas_stock_pre_5_app; drop table if exists ventas_stock_pre_6_app; drop table if exists ventas_stock_pre_7_app; drop table if exists ventas_stock_pre_8_app; drop table if exists precios_app; /*---------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ /* MARCAS PARA DEPARTAMENTO DE REPORTING */ -- Borramos la tabla ventas_stock_stg si nos encontramos en la semana 1 del año actual jd sino borramos las semanas correspondientes a las fechas de la ejecución if SEMANA_INICIAL = 1 then truncate table ventas_stock_stg; else delete from ventas_stock_stg where cast(semana as unsigned) between SEMANA_INICIAL and SEMANA_FINAL; end if; -- Insertamos en la tabla ventas_stock_stg los nuevos datos de la tabla ventas_stock_stg insert into ventas_stock_stg (semana, dia, product_code, branch_no, nombre_tienda, tipo_tienda, subzona, delegado, zona, territorial, venta_uds_anio_actual, venta_uds_anio_anterior, venta_neta_anio_actual, venta_neta_anio_anterior, venta_bruta_anio_actual, venta_bruta_anio_anterior, coste_anio_actual, coste_anio_anterior, margen_anio_actual, margen_anio_anterior, stock_anio_actual, stock_anio_anterior, stock_unidades_actual, stock_unidades_anterior, venta_uds_ultima_semana_anio_actual, venta_uds_ultima_semana_anio_anterior, venta_neta_ultima_semana_anio_actual, venta_neta_ultima_semana_anio_anterior, margen_ultima_semana_anio_actual, margen_ultima_semana_anio_anterior, venta_uds_anio_actual_lfl, venta_uds_anio_anterior_lfl, venta_neta_anio_actual_lfl, venta_bruta_anio_actual_lfl, venta_neta_anio_anterior_lfl, venta_bruta_anio_anterior_lfl, margen_anio_actual_lfl, margen_anio_anterior_lfl, stock_anio_actual_alm, stock_anio_anterior_alm, stock_uds_anio_actual_alm, stock_uds_anio_anterior_alm, descripcion, precio_original, area, serie, serie1, seccion_a, subseccion_a, seccion, familia, disenyo, marca, udp_code, udp_name, brand_type, brand_desc, Temporada, analytical_color, TEMPORALIDAD, DESCATALOGADO, EsTienda, Mercado, fare_code, mes, precio_actual) select semana, dia, product_code, branch_no, nombre_tienda, tipo_tienda, subzona, delegado, zona, territorial, venta_uds_anio_actual, venta_uds_anio_anterior, venta_neta_anio_actual, venta_neta_anio_anterior, venta_bruta_anio_actual, venta_bruta_anio_anterior, coste_anio_actual, coste_anio_anterior, margen_anio_actual, margen_anio_anterior, stock_anio_actual, stock_anio_anterior, stock_unidades_actual, stock_unidades_anterior, venta_uds_ultima_semana_anio_actual, venta_uds_ultima_semana_anio_anterior, venta_neta_ultima_semana_anio_actual, venta_neta_ultima_semana_anio_anterior, margen_ultima_semana_anio_actual, margen_ultima_semana_anio_anterior, venta_uds_anio_actual_lfl, venta_uds_anio_anterior_lfl, venta_neta_anio_actual_lfl, venta_bruta_anio_actual_lfl, venta_neta_anio_anterior_lfl, venta_bruta_anio_anterior_lfl, margen_anio_actual_lfl, margen_anio_anterior_lfl, stock_anio_actual_alm, stock_anio_anterior_alm, stock_uds_anio_actual_alm, stock_uds_anio_anterior_alm, descripcion, precio_original, area, serie, serie1, seccion_a, subseccion_a, seccion, familia, disenyo, marca, udp_code, udp_name, brand_type, brand_desc, Temporada, analytical_color, TEMPORALIDAD, DESCATALOGADO, EsTienda, Mercado, fare_code, mes, precio_actual from ventas_stock1; drop table if exists marca; create table marca as select ' ' as totales, a.mercado, a.branch_no, a.brand_desc as marca_1, a.marca, cast(sum(a.venta_uds_anio_actual) as dec (10,0)) as Vta_uds_AAct, cast(sum(a.venta_uds_anio_anterior) as dec (10,0)) as Vta_uds_AAnt, cast((sum(a.venta_uds_anio_actual)-sum(a.venta_uds_anio_anterior))/sum(a.venta_uds_anio_anterior) as dec (10,4)) as Dif_uds_perc, cast(sum(a.venta_uds_anio_actual_lfl) as dec (10,0)) as Vta_uds_AAct_lfl, cast(sum(a.venta_uds_anio_anterior_lfl) as dec (10,0)) as Vta_uds_AAnt_lfl, cast((sum(a.venta_uds_anio_actual_lfl)-sum(a.venta_uds_anio_anterior_lfl))/sum(a.venta_uds_anio_anterior_lfl) as dec (10,4)) as Dif_uds_perc_lfl, cast(sum(a.venta_neta_anio_actual) as dec(10,0)) as Vta_AAct, cast(sum(a.venta_neta_anio_anterior) as dec(10,0)) as Vta_AAnt, cast(sum(a.venta_neta_anio_actual)-sum(a.venta_neta_anio_anterior) as dec(10,0)) as Dif_vta, cast((sum(a.venta_neta_anio_actual)-sum(a.venta_neta_anio_anterior))/sum(a.venta_neta_anio_anterior) as dec (10,4)) as Dif_vta_per, cast(sum(a.venta_neta_anio_actual_lfl) as dec(10,0)) as Vta_AAct_lfl, cast(sum(a.venta_neta_anio_anterior_lfl) as dec(10,0)) as Vta_AAnt_lfl, cast((sum(a.venta_neta_anio_actual_lfl)-sum(a.venta_neta_anio_anterior_lfl))/sum(a.venta_neta_anio_anterior_lfl) as dec (10,4)) as Dif_vta_per_lfl, cast(sum(a.margen_anio_actual) as dec(10,0)) as Mg_AAct, cast(sum(a.margen_anio_anterior) as dec(10,0)) as Mg_AAnt, cast(sum(a.margen_anio_actual)-sum(a.margen_anio_anterior) as dec(10,0)) as Dif_Mg, cast((sum(a.margen_anio_actual)-sum(a.margen_anio_anterior))/sum(a.margen_anio_anterior) as dec (10,4)) as Dif_Mg_per, cast((sum(a.margen_anio_actual_lfl)-sum(a.margen_anio_anterior_lfl))/sum(a.margen_anio_anterior_lfl) as dec (10,4)) as Dif_Mg_per_lfl, cast(sum(a.margen_anio_actual)/sum(a.venta_neta_anio_actual) as dec (10,4)) as GR_AAct, cast(sum(a.margen_anio_anterior)/sum(a.venta_neta_anio_anterior) as dec (10,4)) as GR_AAnt, cast(sum(a.stock_unidades_actual) as dec(10,0)) as St_uds_Aact, cast(sum(a.stock_unidades_actual)-sum(a.stock_unidades_anterior) as dec(10,0)) as Dif_St_uds, cast((sum(a.stock_unidades_actual)-sum(a.stock_unidades_anterior))/sum(a.stock_unidades_anterior) as dec (10,4)) as Dif_St_uds_per, cast(sum(a.stock_anio_actual)-sum(a.stock_anio_actual_alm) as dec(10,0)) as St_AAct_tda, cast(sum(a.stock_anio_actual_alm) as dec(10,0)) as St_AAct_alm, cast(sum(a.stock_anio_actual) as dec(10,0)) as St_AAct_tot, cast(sum(a.stock_anio_anterior)-sum(a.stock_anio_anterior_alm) as dec(10,0)) as St_AAnt_tda, cast(sum(a.stock_anio_anterior_alm) as dec (10,0)) as St_AAnt_alm, cast(sum(a.stock_anio_anterior) as dec (10,0)) as St_AAnt_tot, cast(sum(a.stock_anio_actual)-sum(a.stock_anio_anterior) as dec (10,0)) as Dif_St, cast((sum(a.stock_anio_actual)-sum(a.stock_anio_anterior))/sum(a.stock_anio_anterior) as dec (10,4)) as Dif_St_per, cast(sum(a.stock_unidades_actual)/sum(a.venta_uds_ultima_semana_anio_actual) as dec(10,2)) as cob_AAct, cast(sum(a.stock_unidades_anterior)/sum(a.venta_uds_ultima_semana_anio_anterior) as dec(10,2)) as cob_AAnt, cast(sum(a.venta_neta_ultima_semana_anio_actual)/sum(a.stock_anio_actual) as dec(10,2)) as vta_sobre_stock, cast(sum(a.margen_ultima_semana_anio_actual)/sum(a.stock_anio_actual) as dec(10,2)) as mg_sobre_stock from (select mercado, branch_no, brand_desc, marca, sum(venta_uds_anio_actual) as venta_uds_anio_actual, sum(venta_uds_anio_anterior) as venta_uds_anio_anterior, sum(venta_uds_anio_actual_lfl) as venta_uds_anio_actual_lfl, sum(venta_uds_anio_anterior_lfl) as venta_uds_anio_anterior_lfl, sum(venta_neta_anio_actual) as venta_neta_anio_actual, sum(venta_neta_anio_anterior) as venta_neta_anio_anterior, sum(venta_neta_anio_actual_lfl) as venta_neta_anio_actual_lfl, sum(venta_neta_anio_anterior_lfl) as venta_neta_anio_anterior_lfl, sum(margen_anio_actual) as margen_anio_actual, sum(margen_anio_anterior) as margen_anio_anterior, sum(margen_anio_actual_lfl) as margen_anio_actual_lfl, sum(margen_anio_anterior_lfl) as margen_anio_anterior_lfl, sum(stock_unidades_actual) as stock_unidades_actual, sum(stock_unidades_anterior) as stock_unidades_anterior, sum(stock_anio_actual) as stock_anio_actual, sum(stock_anio_actual_alm) as stock_anio_actual_alm, sum(stock_anio_anterior) as stock_anio_anterior, sum(stock_anio_anterior_alm) as stock_anio_anterior_alm, sum(venta_uds_ultima_semana_anio_actual) as venta_uds_ultima_semana_anio_actual, sum(venta_uds_ultima_semana_anio_anterior) as venta_uds_ultima_semana_anio_anterior, sum(venta_neta_ultima_semana_anio_actual) as venta_neta_ultima_semana_anio_actual, sum(margen_ultima_semana_anio_actual) as margen_ultima_semana_anio_actual from ventas_stock_stg group by marca,brand_desc,branch_no,mercado ) a group by marca,marca_1,branch_no,mercado order by marca,Vta_AAct desc; drop table if exists marca_subtotal; create table marca_subtotal as select 'SUBTOTAL' as totales, a.mercado, a.branch_no, a.brand_desc as marca_1, '' as marca, cast(sum(a.venta_uds_anio_actual) as dec (10,0)) as Vta_uds_AAct, cast(sum(a.venta_uds_anio_anterior) as dec (10,0)) as Vta_uds_AAnt, cast((sum(a.venta_uds_anio_actual)-sum(a.venta_uds_anio_anterior))/sum(a.venta_uds_anio_anterior) as dec (10,4)) as Dif_uds_perc, cast(sum(a.venta_uds_anio_actual_lfl) as dec (10,0)) as Vta_uds_AAct_lfl, cast(sum(a.venta_uds_anio_anterior_lfl) as dec (10,0)) as Vta_uds_AAnt_lfl, cast((sum(a.venta_uds_anio_actual_lfl)-sum(a.venta_uds_anio_anterior_lfl))/sum(a.venta_uds_anio_anterior_lfl) as dec (10,4)) as Dif_uds_perc_lfl, cast(sum(a.venta_neta_anio_actual) as dec(10,0)) as Vta_AAct, cast(sum(a.venta_neta_anio_anterior) as dec(10,0)) as Vta_AAnt, cast(sum(a.venta_neta_anio_actual)-sum(a.venta_neta_anio_anterior) as dec(10,0)) as Dif_vta, cast((sum(a.venta_neta_anio_actual)-sum(a.venta_neta_anio_anterior))/sum(a.venta_neta_anio_anterior) as dec (10,4)) as Dif_vta_per, cast(sum(a.venta_neta_anio_actual_lfl) as dec(10,0)) as Vta_AAct_lfl, cast(sum(a.venta_neta_anio_anterior_lfl) as dec(10,0)) as Vta_AAnt_lfl, cast((sum(a.venta_neta_anio_actual_lfl)-sum(a.venta_neta_anio_anterior_lfl))/sum(a.venta_neta_anio_anterior_lfl) as dec (10,4)) as Dif_vta_per_lfl, cast(sum(a.margen_anio_actual) as dec(10,0)) as Mg_AAct, cast(sum(a.margen_anio_anterior) as dec(10,0)) as Mg_AAnt, cast(sum(a.margen_anio_actual)-sum(a.margen_anio_anterior) as dec(10,0)) as Dif_Mg, cast((sum(a.margen_anio_actual)-sum(a.margen_anio_anterior))/sum(a.margen_anio_anterior) as dec (10,4)) as Dif_Mg_per, cast((sum(a.margen_anio_actual_lfl)-sum(a.margen_anio_anterior_lfl))/sum(a.margen_anio_anterior_lfl) as dec (10,4)) as Dif_Mg_per_lfl, cast(sum(a.margen_anio_actual)/sum(a.venta_neta_anio_actual) as dec (10,4)) as GR_AAct, cast(sum(a.margen_anio_anterior)/sum(a.venta_neta_anio_anterior) as dec (10,4)) as GR_AAnt, cast(sum(a.stock_unidades_actual) as dec(10,0)) as St_uds_Aact, cast(sum(a.stock_unidades_actual)-sum(a.stock_unidades_anterior) as dec(10,0)) as Dif_St_uds, cast((sum(a.stock_unidades_actual)-sum(a.stock_unidades_anterior))/sum(a.stock_unidades_anterior) as dec (10,4)) as Dif_St_uds_per, cast(sum(a.stock_anio_actual)-sum(a.stock_anio_actual_alm) as dec(10,0)) as St_AAct_tda, cast(sum(a.stock_anio_actual_alm) as dec(10,0)) as St_AAct_alm, cast(sum(a.stock_anio_actual) as dec(10,0)) as St_AAct_tot, cast(sum(a.stock_anio_anterior)-sum(a.stock_anio_anterior_alm) as dec(10,0)) as St_AAnt_tda, cast(sum(a.stock_anio_anterior_alm) as dec (10,0)) as St_AAnt_alm, cast(sum(a.stock_anio_anterior) as dec (10,0)) as St_AAnt_tot, cast(sum(a.stock_anio_actual)-sum(a.stock_anio_anterior) as dec (10,0)) as Dif_St, cast((sum(a.stock_anio_actual)-sum(a.stock_anio_anterior))/sum(a.stock_anio_anterior) as dec (10,4)) as Dif_St_per, cast(sum(a.stock_unidades_actual)/sum(a.venta_uds_ultima_semana_anio_actual) as dec(10,2)) as cob_AAct, cast(sum(a.stock_unidades_anterior)/sum(a.venta_uds_ultima_semana_anio_anterior) as dec(10,2)) as cob_AAnt, cast(sum(a.venta_neta_ultima_semana_anio_actual)/sum(a.stock_anio_actual) as dec(10,2)) as vta_sobre_stock, cast(sum(a.margen_ultima_semana_anio_actual)/sum(a.stock_anio_actual) as dec(10,2)) as mg_sobre_stock from ( select mercado, branch_no, brand_desc, sum(venta_uds_anio_actual) as venta_uds_anio_actual, sum(venta_uds_anio_anterior) as venta_uds_anio_anterior, sum(venta_uds_anio_actual_lfl) as venta_uds_anio_actual_lfl, sum(venta_uds_anio_anterior_lfl) as venta_uds_anio_anterior_lfl, sum(venta_neta_anio_actual) as venta_neta_anio_actual, sum(venta_neta_anio_anterior) as venta_neta_anio_anterior, sum(venta_neta_anio_actual_lfl) as venta_neta_anio_actual_lfl, sum(venta_neta_anio_anterior_lfl) as venta_neta_anio_anterior_lfl, sum(margen_anio_actual) as margen_anio_actual, sum(margen_anio_anterior) as margen_anio_anterior, sum(margen_anio_actual_lfl) as margen_anio_actual_lfl, sum(margen_anio_anterior_lfl) as margen_anio_anterior_lfl, sum(stock_unidades_actual) as stock_unidades_actual, sum(stock_unidades_anterior) as stock_unidades_anterior, sum(stock_anio_actual) as stock_anio_actual, sum(stock_anio_actual_alm) as stock_anio_actual_alm, sum(stock_anio_anterior) as stock_anio_anterior, sum(stock_anio_anterior_alm) as stock_anio_anterior_alm, sum(venta_uds_ultima_semana_anio_actual) as venta_uds_ultima_semana_anio_actual, sum(venta_uds_ultima_semana_anio_anterior) as venta_uds_ultima_semana_anio_anterior, sum(venta_neta_ultima_semana_anio_actual) as venta_neta_ultima_semana_anio_actual, sum(margen_ultima_semana_anio_actual) as margen_ultima_semana_anio_actual from ventas_stock_stg group by brand_desc,mercado,branch_no ) a group by brand_desc,mercado,branch_no order by Vta_AAct; insert into marca select * from marca_subtotal; drop table if exists marca_1; create table marca_1 as select a.*, replace(concat(format(((a.Vta_AAct)/(b.Vta_AAct))*100,2),'%'),'.',',') as peso_1, replace(concat(format(((a.Mg_AAct)/(b.Mg_AAct))*100,2),'%'),'.',',') as peso_2, replace(concat(format(((a.St_AAct_tot)/(b.St_AAct_tot))*100,2),'%'),'.',',') as peso_3, replace(cast(((a.Vta_AAct/b.Vta_AAct)/(a.St_AAct_tot/b.St_AAct_tot)) as dec(10,2)),'.',',') as peso_4, replace(cast(((a.Mg_AAct/b.Mg_AAct)/(a.St_AAct_tot/b.St_AAct_tot)) as dec(10,2)),'.',',') as peso_5 from marca a left join marca_subtotal b on a.marca_1=b.marca_1 and a.branch_no=b.branch_no order by marca_1,totales,Vta_AAct desc; drop table if exists marca_total; create table marca_total as select 'TOTAL' as totales, '' as mercado, '' as branch_no, '' as marca_1, '' as marca, cast(sum(a.venta_uds_anio_actual) as dec (10,0)) as Vta_uds_AAct, cast(sum(a.venta_uds_anio_anterior) as dec (10,0)) as Vta_uds_AAnt, cast((sum(a.venta_uds_anio_actual)-sum(a.venta_uds_anio_anterior))/sum(a.venta_uds_anio_anterior) as dec (10,4)) as Dif_uds_perc, cast(sum(a.venta_uds_anio_actual_lfl) as dec (10,0)) as Vta_uds_AAct_lfl, cast(sum(a.venta_uds_anio_anterior_lfl) as dec (10,0)) as Vta_uds_AAnt_lfl, cast((sum(a.venta_uds_anio_actual_lfl)-sum(a.venta_uds_anio_anterior_lfl))/sum(a.venta_uds_anio_anterior_lfl) as dec (10,4)) as Dif_uds_perc_lfl, cast(sum(a.venta_neta_anio_actual) as dec(10,0)) as Vta_AAct, cast(sum(a.venta_neta_anio_anterior) as dec(10,0)) as Vta_AAnt, cast(sum(a.venta_neta_anio_actual)-sum(a.venta_neta_anio_anterior) as dec(10,0)) as Dif_vta, cast((sum(a.venta_neta_anio_actual)-sum(a.venta_neta_anio_anterior))/sum(a.venta_neta_anio_anterior) as dec (10,4)) as Dif_vta_per, cast(sum(a.venta_neta_anio_actual_lfl) as dec(10,0)) as Vta_AAct_lfl, cast(sum(a.venta_neta_anio_anterior_lfl) as dec(10,0)) as Vta_AAnt_lfl, cast((sum(a.venta_neta_anio_actual_lfl)-sum(a.venta_neta_anio_anterior_lfl))/sum(a.venta_neta_anio_anterior_lfl) as dec (10,4)) as Dif_vta_per_lfl, cast(sum(a.margen_anio_actual) as dec(10,0)) as Mg_AAct, cast(sum(a.margen_anio_anterior) as dec(10,0)) as Mg_AAnt, cast(sum(a.margen_anio_actual)-sum(a.margen_anio_anterior) as dec(10,0)) as Dif_Mg, cast((sum(a.margen_anio_actual)-sum(a.margen_anio_anterior))/sum(a.margen_anio_anterior) as dec (10,4)) as Dif_Mg_per, cast((sum(a.margen_anio_actual_lfl)-sum(a.margen_anio_anterior_lfl))/sum(a.margen_anio_anterior_lfl) as dec (10,4)) as Dif_Mg_per_lfl, cast(sum(a.margen_anio_actual)/sum(a.venta_neta_anio_actual) as dec (10,4)) as GR_AAct, cast(sum(a.margen_anio_anterior)/sum(a.venta_neta_anio_anterior) as dec (10,4)) as GR_AAnt, cast(sum(a.stock_unidades_actual) as dec(10,0)) as St_uds_Aact, cast(sum(a.stock_unidades_actual)-sum(a.stock_unidades_anterior) as dec(10,0)) as Dif_St_uds, cast((sum(a.stock_unidades_actual)-sum(a.stock_unidades_anterior))/sum(a.stock_unidades_anterior) as dec (10,4)) as Dif_St_uds_per, cast(sum(a.stock_anio_actual)-sum(a.stock_anio_actual_alm) as dec(10,0)) as St_AAct_tda, cast(sum(a.stock_anio_actual_alm) as dec(10,0)) as St_AAct_alm, cast(sum(a.stock_anio_actual) as dec(10,0)) as St_AAct_tot, cast(sum(a.stock_anio_anterior)-sum(a.stock_anio_anterior_alm) as dec(10,0)) as St_AAnt_tda, cast(sum(a.stock_anio_anterior_alm) as dec (10,0)) as St_AAnt_alm, cast(sum(a.stock_anio_anterior) as dec (10,0)) as St_AAnt_tot, cast(sum(a.stock_anio_actual)-sum(a.stock_anio_anterior) as dec (10,0)) as Dif_St, cast((sum(a.stock_anio_actual)-sum(a.stock_anio_anterior))/sum(a.stock_anio_anterior) as dec (10,4)) as Dif_St_per, cast(sum(a.stock_unidades_actual)/sum(a.venta_uds_ultima_semana_anio_actual) as dec(10,2)) as cob_AAct, cast(sum(a.stock_unidades_anterior)/sum(a.venta_uds_ultima_semana_anio_anterior) as dec(10,2)) as cob_AAnt, cast(sum(a.venta_neta_ultima_semana_anio_actual)/sum(a.stock_anio_actual) as dec(10,2)) as vta_sobre_stock, cast(sum(a.margen_ultima_semana_anio_actual)/sum(a.stock_anio_actual) as dec(10,2)) as mg_sobre_stock, '' as peso_1, '' as peso_2, '' as peso_3, '' as peso_4, '' as peso_5 from (select sum(venta_uds_anio_actual) as venta_uds_anio_actual, sum(venta_uds_anio_anterior) as venta_uds_anio_anterior, sum(venta_uds_anio_actual_lfl) as venta_uds_anio_actual_lfl, sum(venta_uds_anio_anterior_lfl) as venta_uds_anio_anterior_lfl, sum(venta_neta_anio_actual) as venta_neta_anio_actual, sum(venta_neta_anio_anterior) as venta_neta_anio_anterior, sum(venta_neta_anio_actual_lfl) as venta_neta_anio_actual_lfl, sum(venta_neta_anio_anterior_lfl) as venta_neta_anio_anterior_lfl, sum(margen_anio_actual) as margen_anio_actual, sum(margen_anio_anterior) as margen_anio_anterior, sum(margen_anio_actual_lfl) as margen_anio_actual_lfl, sum(margen_anio_anterior_lfl) as margen_anio_anterior_lfl, sum(stock_unidades_actual) as stock_unidades_actual, sum(stock_unidades_anterior) as stock_unidades_anterior, sum(stock_anio_actual) as stock_anio_actual, sum(stock_anio_actual_alm) as stock_anio_actual_alm, sum(stock_anio_anterior) as stock_anio_anterior, sum(stock_anio_anterior_alm) as stock_anio_anterior_alm, sum(venta_uds_ultima_semana_anio_actual) as venta_uds_ultima_semana_anio_actual, sum(venta_uds_ultima_semana_anio_anterior) as venta_uds_ultima_semana_anio_anterior, sum(venta_neta_ultima_semana_anio_actual) as venta_neta_ultima_semana_anio_actual, sum(margen_ultima_semana_anio_actual) as margen_ultima_semana_anio_actual from ventas_stock_stg ) a; insert into marca_1 select * from marca_total; drop table if exists adm_marca_acumulado; create table adm_marca_acumulado as select a.totales, a.mercado, a.branch_no, c.store_type, a.marca_1, a.marca, REPLACE(FORMAT(a.Vta_uds_AAct,0),',','.') as Vta_uds_AAct, REPLACE(FORMAT(a.Vta_uds_AAnt,0),',','.') as Vta_uds_AAnt, replace(CONCAT(FORMAT(a.Dif_uds_perc*100,2),'%'),'.',',') as Dif_uds_perc, REPLACE(FORMAT(a.Vta_uds_AAct_lfl,0),',','.') as Vta_uds_AAct_lfl, REPLACE(FORMAT(a.Vta_uds_AAnt_lfl,0),',','.') as Vta_uds_AAnt_lfl, replace(CONCAT(FORMAT(a.Dif_uds_perc_lfl*100,2),'%'),'.',',') as Dif_uds_perc_lfl, REPLACE(FORMAT(a.Vta_AAct,0),',','.') as Vta_AAct, REPLACE(FORMAT(a.Vta_AAnt, 0),',','.') as Vta_AAnt, REPLACE(FORMAT(a.Dif_vta,0),',','.') as Dif_vta, replace(CONCAT(FORMAT(a.Dif_vta_per*100,2),'%'),'.',',') as Dif_vta_per, REPLACE(FORMAT(a.Vta_AAct_lfl,0),',','.') as Vta_AAct_lfl, REPLACE(FORMAT(a.Vta_AAnt_lfl, 0),',','.') as Vta_AAnt_lfl, replace(CONCAT(FORMAT(a.Dif_vta_per_lfl*100,2),'%'),'.',',') as Dif_vta_per_lfl, REPLACE(FORMAT(a.Mg_AAct,0),',','.') as Mg_AAct, REPLACE(FORMAT(a.Mg_AAnt,0),',','.') as Mg_AAnt, REPLACE(FORMAT(a.Dif_Mg, 0),',','.') as Dif_Mg, replace(CONCAT(FORMAT(a.Dif_Mg_per*100,2),'%'),'.',',') as Dif_Mg_per, replace(CONCAT(FORMAT(a.Dif_Mg_per_lfl*100,2),'%'),'.',',') as Dif_Mg_per_lfl, replace(CONCAT(FORMAT(a.GR_AAct*100,2),'%'),'.',',') as GR_AAct, replace(CONCAT(FORMAT(a.GR_AAnt*100,2),'%'),'.',',') as GR_AAnt, REPLACE(FORMAT(a.St_uds_Aact,0),',','.') as St_uds_Aact, REPLACE(FORMAT(a.Dif_St_uds,0),',','.') as Dif_St_uds, replace(CONCAT(FORMAT(a.Dif_St_uds_per*100,2),'%'),'.',',') as Dif_St_uds_per, REPLACE(FORMAT(a.St_AAct_tda,0),',','.') as St_AAct_tda, REPLACE(FORMAT(a.St_AAct_alm,0),',','.') as St_AAct_alm, REPLACE(FORMAT(a.St_AAct_tot,0),',','.') as St_AAct_tot, REPLACE(FORMAT(a.St_AAnt_tda,0),',','.') as St_AAnt_tda, REPLACE(FORMAT(a.St_AAnt_alm,0),',','.') as St_AAnt_alm, REPLACE(FORMAT(a.St_AAnt_tot,0),',','.') as St_AAnt_tot, REPLACE(FORMAT(a.Dif_St,0),',','.') as Dif_St, replace(CONCAT(FORMAT(a.Dif_St_per*100,2),'%'),'.',',') as Dif_St_per, replace(cast(a.cob_AAct as dec(10,2)),'.',',') as cob_AAct, replace(cast(a.cob_AAnt as dec(10,2)),'.',',') as cob_AAnt, (CASE WHEN a.totales in ('SUBTOTAL','TOTAL') then replace(cast(((a.Vta_AAct/b.Vta_AAct)/(a.St_AAct_tot/b.St_AAct_tot)) as dec (10,2)),'.',',') else a.peso_4 end) as vta_sobre_stock, (CASE WHEN a.totales in ('SUBTOTAL','TOTAL') then replace(cast(((a.Mg_AAct/b.Mg_AAct)/(a.St_AAct_tot/b.St_AAct_tot)) as dec(10,2)),'.',',') else a.peso_5 end) as mg_sobre_stock, (case when a.totales in ('SUBTOTAL','TOTAL') then replace(concat(format(((a.Vta_AAct)/(b.Vta_AAct))*100,2),'%'),'.',',') else a.peso_1 end) as Mix_vtas, (case when a.totales in ('SUBTOTAL','TOTAL') then replace(concat(format(((a.Mg_AAct)/(b.Mg_AAct))*100,2),'%'),'.',',') else a.peso_2 end) as Mix_Mg, (CASE WHEN a.totales IN ('SUBTOTAL','TOTAL') then replace(concat(format(((a.St_AAct_tot)/(b.St_AAct_tot))*100,2),'%'),'.',',') else a.peso_3 end) as Mix_Stock from marca_1 a left join marca_total b on a.totales in ('SUBTOTAL','TOTAL') left join md_dbg.branch c on a.branch_no=c.branch_no having Vta_uds_AAct<>0 or Vta_uds_AAnt<>0 or Vta_AAct<>0 or Vta_AAnt<>0; /* eliminamos tablas temporales */ drop table if exists marca_1; drop table if exists marca; drop table if exists marca_subtotal; drop table if exists marca_total; -- Creamos la tabla adm_marca_mensual para la temporalidad mensual, solo si es la última semana_jd del mes_jd actual if FIN_MES_JD then drop table if exists marca; create table marca as select ' ' as totales, a.mercado, a.branch_no, a.brand_desc as marca_1, a.marca, cast(sum(a.venta_uds_anio_actual) as dec (10,0)) as Vta_uds_AAct, cast(sum(a.venta_uds_anio_anterior) as dec (10,0)) as Vta_uds_AAnt, cast((sum(a.venta_uds_anio_actual)-sum(a.venta_uds_anio_anterior))/sum(a.venta_uds_anio_anterior) as dec (10,4)) as Dif_uds_perc, cast(sum(a.venta_uds_anio_actual_lfl) as dec (10,0)) as Vta_uds_AAct_lfl, cast(sum(a.venta_uds_anio_anterior_lfl) as dec (10,0)) as Vta_uds_AAnt_lfl, cast((sum(a.venta_uds_anio_actual_lfl)-sum(a.venta_uds_anio_anterior_lfl))/sum(a.venta_uds_anio_anterior_lfl) as dec (10,4)) as Dif_uds_perc_lfl, cast(sum(a.venta_neta_anio_actual) as dec(10,0)) as Vta_AAct, cast(sum(a.venta_neta_anio_anterior) as dec(10,0)) as Vta_AAnt, cast(sum(a.venta_neta_anio_actual)-sum(a.venta_neta_anio_anterior) as dec(10,0)) as Dif_vta, cast((sum(a.venta_neta_anio_actual)-sum(a.venta_neta_anio_anterior))/sum(a.venta_neta_anio_anterior) as dec (10,4)) as Dif_vta_per, cast(sum(a.venta_neta_anio_actual_lfl) as dec(10,0)) as Vta_AAct_lfl, cast(sum(a.venta_neta_anio_anterior_lfl) as dec(10,0)) as Vta_AAnt_lfl, cast((sum(a.venta_neta_anio_actual_lfl)-sum(a.venta_neta_anio_anterior_lfl))/sum(a.venta_neta_anio_anterior_lfl) as dec (10,4)) as Dif_vta_per_lfl, cast(sum(a.margen_anio_actual) as dec(10,0)) as Mg_AAct, cast(sum(a.margen_anio_anterior) as dec(10,0)) as Mg_AAnt, cast(sum(a.margen_anio_actual)-sum(a.margen_anio_anterior) as dec(10,0)) as Dif_Mg, cast((sum(a.margen_anio_actual)-sum(a.margen_anio_anterior))/sum(a.margen_anio_anterior) as dec (10,4)) as Dif_Mg_per, cast((sum(a.margen_anio_actual_lfl)-sum(a.margen_anio_anterior_lfl))/sum(a.margen_anio_anterior_lfl) as dec (10,4)) as Dif_Mg_per_lfl, cast(sum(a.margen_anio_actual)/sum(a.venta_neta_anio_actual) as dec (10,4)) as GR_AAct, cast(sum(a.margen_anio_anterior)/sum(a.venta_neta_anio_anterior) as dec (10,4)) as GR_AAnt, cast(sum(a.stock_unidades_actual) as dec(10,0)) as St_uds_Aact, cast(sum(a.stock_unidades_actual)-sum(a.stock_unidades_anterior) as dec(10,0)) as Dif_St_uds, cast((sum(a.stock_unidades_actual)-sum(a.stock_unidades_anterior))/sum(a.stock_unidades_anterior) as dec (10,4)) as Dif_St_uds_per, cast(sum(a.stock_anio_actual)-sum(a.stock_anio_actual_alm) as dec(10,0)) as St_AAct_tda, cast(sum(a.stock_anio_actual_alm) as dec(10,0)) as St_AAct_alm, cast(sum(a.stock_anio_actual) as dec(10,0)) as St_AAct_tot, cast(sum(a.stock_anio_anterior)-sum(a.stock_anio_anterior_alm) as dec(10,0)) as St_AAnt_tda, cast(sum(a.stock_anio_anterior_alm) as dec (10,0)) as St_AAnt_alm, cast(sum(a.stock_anio_anterior) as dec (10,0)) as St_AAnt_tot, cast(sum(a.stock_anio_actual)-sum(a.stock_anio_anterior) as dec (10,0)) as Dif_St, cast((sum(a.stock_anio_actual)-sum(a.stock_anio_anterior))/sum(a.stock_anio_anterior) as dec (10,4)) as Dif_St_per, cast(sum(a.stock_unidades_actual)/sum(a.venta_uds_ultima_semana_anio_actual) as dec(10,2)) as cob_AAct, cast(sum(a.stock_unidades_anterior)/sum(a.venta_uds_ultima_semana_anio_anterior) as dec(10,2)) as cob_AAnt, cast(sum(a.venta_neta_ultima_semana_anio_actual)/sum(a.stock_anio_actual) as dec(10,2)) as vta_sobre_stock, cast(sum(a.margen_ultima_semana_anio_actual)/sum(a.stock_anio_actual) as dec(10,2)) as mg_sobre_stock from (select mercado, branch_no, brand_desc, marca, sum(venta_uds_anio_actual) as venta_uds_anio_actual, sum(venta_uds_anio_anterior) as venta_uds_anio_anterior, sum(venta_uds_anio_actual_lfl) as venta_uds_anio_actual_lfl, sum(venta_uds_anio_anterior_lfl) as venta_uds_anio_anterior_lfl, sum(venta_neta_anio_actual) as venta_neta_anio_actual, sum(venta_neta_anio_anterior) as venta_neta_anio_anterior, sum(venta_neta_anio_actual_lfl) as venta_neta_anio_actual_lfl, sum(venta_neta_anio_anterior_lfl) as venta_neta_anio_anterior_lfl, sum(margen_anio_actual) as margen_anio_actual, sum(margen_anio_anterior) as margen_anio_anterior, sum(margen_anio_actual_lfl) as margen_anio_actual_lfl, sum(margen_anio_anterior_lfl) as margen_anio_anterior_lfl, sum(stock_unidades_actual) as stock_unidades_actual, sum(stock_unidades_anterior) as stock_unidades_anterior, sum(stock_anio_actual) as stock_anio_actual, sum(stock_anio_actual_alm) as stock_anio_actual_alm, sum(stock_anio_anterior) as stock_anio_anterior, sum(stock_anio_anterior_alm) as stock_anio_anterior_alm, sum(venta_uds_ultima_semana_anio_actual) as venta_uds_ultima_semana_anio_actual, sum(venta_uds_ultima_semana_anio_anterior) as venta_uds_ultima_semana_anio_anterior, sum(venta_neta_ultima_semana_anio_actual) as venta_neta_ultima_semana_anio_actual, sum(margen_ultima_semana_anio_actual) as margen_ultima_semana_anio_actual from ventas_stock_stg where cast(semana as unsigned) between SEMANA_INICIAL_MES_JD and SEMANA_FINAL group by marca,brand_desc,branch_no,mercado ) a group by marca,marca_1,branch_no,mercado order by marca,Vta_AAct desc; drop table if exists marca_subtotal; create table marca_subtotal as select 'SUBTOTAL' as totales, a.mercado, a.branch_no, a.brand_desc as marca_1, '' as marca, cast(sum(a.venta_uds_anio_actual) as dec (10,0)) as Vta_uds_AAct, cast(sum(a.venta_uds_anio_anterior) as dec (10,0)) as Vta_uds_AAnt, cast((sum(a.venta_uds_anio_actual)-sum(a.venta_uds_anio_anterior))/sum(a.venta_uds_anio_anterior) as dec (10,4)) as Dif_uds_perc, cast(sum(a.venta_uds_anio_actual_lfl) as dec (10,0)) as Vta_uds_AAct_lfl, cast(sum(a.venta_uds_anio_anterior_lfl) as dec (10,0)) as Vta_uds_AAnt_lfl, cast((sum(a.venta_uds_anio_actual_lfl)-sum(a.venta_uds_anio_anterior_lfl))/sum(a.venta_uds_anio_anterior_lfl) as dec (10,4)) as Dif_uds_perc_lfl, cast(sum(a.venta_neta_anio_actual) as dec(10,0)) as Vta_AAct, cast(sum(a.venta_neta_anio_anterior) as dec(10,0)) as Vta_AAnt, cast(sum(a.venta_neta_anio_actual)-sum(a.venta_neta_anio_anterior) as dec(10,0)) as Dif_vta, cast((sum(a.venta_neta_anio_actual)-sum(a.venta_neta_anio_anterior))/sum(a.venta_neta_anio_anterior) as dec (10,4)) as Dif_vta_per, cast(sum(a.venta_neta_anio_actual_lfl) as dec(10,0)) as Vta_AAct_lfl, cast(sum(a.venta_neta_anio_anterior_lfl) as dec(10,0)) as Vta_AAnt_lfl, cast((sum(a.venta_neta_anio_actual_lfl)-sum(a.venta_neta_anio_anterior_lfl))/sum(a.venta_neta_anio_anterior_lfl) as dec (10,4)) as Dif_vta_per_lfl, cast(sum(a.margen_anio_actual) as dec(10,0)) as Mg_AAct, cast(sum(a.margen_anio_anterior) as dec(10,0)) as Mg_AAnt, cast(sum(a.margen_anio_actual)-sum(a.margen_anio_anterior) as dec(10,0)) as Dif_Mg, cast((sum(a.margen_anio_actual)-sum(a.margen_anio_anterior))/sum(a.margen_anio_anterior) as dec (10,4)) as Dif_Mg_per, cast((sum(a.margen_anio_actual_lfl)-sum(a.margen_anio_anterior_lfl))/sum(a.margen_anio_anterior_lfl) as dec (10,4)) as Dif_Mg_per_lfl, cast(sum(a.margen_anio_actual)/sum(a.venta_neta_anio_actual) as dec (10,4)) as GR_AAct, cast(sum(a.margen_anio_anterior)/sum(a.venta_neta_anio_anterior) as dec (10,4)) as GR_AAnt, cast(sum(a.stock_unidades_actual) as dec(10,0)) as St_uds_Aact, cast(sum(a.stock_unidades_actual)-sum(a.stock_unidades_anterior) as dec(10,0)) as Dif_St_uds, cast((sum(a.stock_unidades_actual)-sum(a.stock_unidades_anterior))/sum(a.stock_unidades_anterior) as dec (10,4)) as Dif_St_uds_per, cast(sum(a.stock_anio_actual)-sum(a.stock_anio_actual_alm) as dec(10,0)) as St_AAct_tda, cast(sum(a.stock_anio_actual_alm) as dec(10,0)) as St_AAct_alm, cast(sum(a.stock_anio_actual) as dec(10,0)) as St_AAct_tot, cast(sum(a.stock_anio_anterior)-sum(a.stock_anio_anterior_alm) as dec(10,0)) as St_AAnt_tda, cast(sum(a.stock_anio_anterior_alm) as dec (10,0)) as St_AAnt_alm, cast(sum(a.stock_anio_anterior) as dec (10,0)) as St_AAnt_tot, cast(sum(a.stock_anio_actual)-sum(a.stock_anio_anterior) as dec (10,0)) as Dif_St, cast((sum(a.stock_anio_actual)-sum(a.stock_anio_anterior))/sum(a.stock_anio_anterior) as dec (10,4)) as Dif_St_per, cast(sum(a.stock_unidades_actual)/sum(a.venta_uds_ultima_semana_anio_actual) as dec(10,2)) as cob_AAct, cast(sum(a.stock_unidades_anterior)/sum(a.venta_uds_ultima_semana_anio_anterior) as dec(10,2)) as cob_AAnt, cast(sum(a.venta_neta_ultima_semana_anio_actual)/sum(a.stock_anio_actual) as dec(10,2)) as vta_sobre_stock, cast(sum(a.margen_ultima_semana_anio_actual)/sum(a.stock_anio_actual) as dec(10,2)) as mg_sobre_stock from ( select mercado, branch_no, brand_desc, sum(venta_uds_anio_actual) as venta_uds_anio_actual, sum(venta_uds_anio_anterior) as venta_uds_anio_anterior, sum(venta_uds_anio_actual_lfl) as venta_uds_anio_actual_lfl, sum(venta_uds_anio_anterior_lfl) as venta_uds_anio_anterior_lfl, sum(venta_neta_anio_actual) as venta_neta_anio_actual, sum(venta_neta_anio_anterior) as venta_neta_anio_anterior, sum(venta_neta_anio_actual_lfl) as venta_neta_anio_actual_lfl, sum(venta_neta_anio_anterior_lfl) as venta_neta_anio_anterior_lfl, sum(margen_anio_actual) as margen_anio_actual, sum(margen_anio_anterior) as margen_anio_anterior, sum(margen_anio_actual_lfl) as margen_anio_actual_lfl, sum(margen_anio_anterior_lfl) as margen_anio_anterior_lfl, sum(stock_unidades_actual) as stock_unidades_actual, sum(stock_unidades_anterior) as stock_unidades_anterior, sum(stock_anio_actual) as stock_anio_actual, sum(stock_anio_actual_alm) as stock_anio_actual_alm, sum(stock_anio_anterior) as stock_anio_anterior, sum(stock_anio_anterior_alm) as stock_anio_anterior_alm, sum(venta_uds_ultima_semana_anio_actual) as venta_uds_ultima_semana_anio_actual, sum(venta_uds_ultima_semana_anio_anterior) as venta_uds_ultima_semana_anio_anterior, sum(venta_neta_ultima_semana_anio_actual) as venta_neta_ultima_semana_anio_actual, sum(margen_ultima_semana_anio_actual) as margen_ultima_semana_anio_actual from ventas_stock_stg where cast(semana as unsigned) between SEMANA_INICIAL_MES_JD and SEMANA_FINAL group by brand_desc,mercado,branch_no ) a group by brand_desc,mercado,branch_no order by Vta_AAct; insert into marca select * from marca_subtotal; drop table if exists marca_1; create table marca_1 as select a.*, replace(concat(format(((a.Vta_AAct)/(b.Vta_AAct))*100,2),'%'),'.',',') as peso_1, replace(concat(format(((a.Mg_AAct)/(b.Mg_AAct))*100,2),'%'),'.',',') as peso_2, replace(concat(format(((a.St_AAct_tot)/(b.St_AAct_tot))*100,2),'%'),'.',',') as peso_3, replace(cast(((a.Vta_AAct/b.Vta_AAct)/(a.St_AAct_tot/b.St_AAct_tot)) as dec(10,2)),'.',',') as peso_4, replace(cast(((a.Mg_AAct/b.Mg_AAct)/(a.St_AAct_tot/b.St_AAct_tot)) as dec(10,2)),'.',',') as peso_5 from marca a left join marca_subtotal b on a.marca_1=b.marca_1 and a.branch_no=b.branch_no order by marca_1,totales,Vta_AAct desc; drop table if exists marca_total; create table marca_total as select 'TOTAL' as totales, '' as mercado, '' as branch_no, '' as marca_1, '' as marca, cast(sum(a.venta_uds_anio_actual) as dec (10,0)) as Vta_uds_AAct, cast(sum(a.venta_uds_anio_anterior) as dec (10,0)) as Vta_uds_AAnt, cast((sum(a.venta_uds_anio_actual)-sum(a.venta_uds_anio_anterior))/sum(a.venta_uds_anio_anterior) as dec (10,4)) as Dif_uds_perc, cast(sum(a.venta_uds_anio_actual_lfl) as dec (10,0)) as Vta_uds_AAct_lfl, cast(sum(a.venta_uds_anio_anterior_lfl) as dec (10,0)) as Vta_uds_AAnt_lfl, cast((sum(a.venta_uds_anio_actual_lfl)-sum(a.venta_uds_anio_anterior_lfl))/sum(a.venta_uds_anio_anterior_lfl) as dec (10,4)) as Dif_uds_perc_lfl, cast(sum(a.venta_neta_anio_actual) as dec(10,0)) as Vta_AAct, cast(sum(a.venta_neta_anio_anterior) as dec(10,0)) as Vta_AAnt, cast(sum(a.venta_neta_anio_actual)-sum(a.venta_neta_anio_anterior) as dec(10,0)) as Dif_vta, cast((sum(a.venta_neta_anio_actual)-sum(a.venta_neta_anio_anterior))/sum(a.venta_neta_anio_anterior) as dec (10,4)) as Dif_vta_per, cast(sum(a.venta_neta_anio_actual_lfl) as dec(10,0)) as Vta_AAct_lfl, cast(sum(a.venta_neta_anio_anterior_lfl) as dec(10,0)) as Vta_AAnt_lfl, cast((sum(a.venta_neta_anio_actual_lfl)-sum(a.venta_neta_anio_anterior_lfl))/sum(a.venta_neta_anio_anterior_lfl) as dec (10,4)) as Dif_vta_per_lfl, cast(sum(a.margen_anio_actual) as dec(10,0)) as Mg_AAct, cast(sum(a.margen_anio_anterior) as dec(10,0)) as Mg_AAnt, cast(sum(a.margen_anio_actual)-sum(a.margen_anio_anterior) as dec(10,0)) as Dif_Mg, cast((sum(a.margen_anio_actual)-sum(a.margen_anio_anterior))/sum(a.margen_anio_anterior) as dec (10,4)) as Dif_Mg_per, cast((sum(a.margen_anio_actual_lfl)-sum(a.margen_anio_anterior_lfl))/sum(a.margen_anio_anterior_lfl) as dec (10,4)) as Dif_Mg_per_lfl, cast(sum(a.margen_anio_actual)/sum(a.venta_neta_anio_actual) as dec (10,4)) as GR_AAct, cast(sum(a.margen_anio_anterior)/sum(a.venta_neta_anio_anterior) as dec (10,4)) as GR_AAnt, cast(sum(a.stock_unidades_actual) as dec(10,0)) as St_uds_Aact, cast(sum(a.stock_unidades_actual)-sum(a.stock_unidades_anterior) as dec(10,0)) as Dif_St_uds, cast((sum(a.stock_unidades_actual)-sum(a.stock_unidades_anterior))/sum(a.stock_unidades_anterior) as dec (10,4)) as Dif_St_uds_per, cast(sum(a.stock_anio_actual)-sum(a.stock_anio_actual_alm) as dec(10,0)) as St_AAct_tda, cast(sum(a.stock_anio_actual_alm) as dec(10,0)) as St_AAct_alm, cast(sum(a.stock_anio_actual) as dec(10,0)) as St_AAct_tot, cast(sum(a.stock_anio_anterior)-sum(a.stock_anio_anterior_alm) as dec(10,0)) as St_AAnt_tda, cast(sum(a.stock_anio_anterior_alm) as dec (10,0)) as St_AAnt_alm, cast(sum(a.stock_anio_anterior) as dec (10,0)) as St_AAnt_tot, cast(sum(a.stock_anio_actual)-sum(a.stock_anio_anterior) as dec (10,0)) as Dif_St, cast((sum(a.stock_anio_actual)-sum(a.stock_anio_anterior))/sum(a.stock_anio_anterior) as dec (10,4)) as Dif_St_per, cast(sum(a.stock_unidades_actual)/sum(a.venta_uds_ultima_semana_anio_actual) as dec(10,2)) as cob_AAct, cast(sum(a.stock_unidades_anterior)/sum(a.venta_uds_ultima_semana_anio_anterior) as dec(10,2)) as cob_AAnt, cast(sum(a.venta_neta_ultima_semana_anio_actual)/sum(a.stock_anio_actual) as dec(10,2)) as vta_sobre_stock, cast(sum(a.margen_ultima_semana_anio_actual)/sum(a.stock_anio_actual) as dec(10,2)) as mg_sobre_stock, '' as peso_1, '' as peso_2, '' as peso_3, '' as peso_4, '' as peso_5 from (select sum(venta_uds_anio_actual) as venta_uds_anio_actual, sum(venta_uds_anio_anterior) as venta_uds_anio_anterior, sum(venta_uds_anio_actual_lfl) as venta_uds_anio_actual_lfl, sum(venta_uds_anio_anterior_lfl) as venta_uds_anio_anterior_lfl, sum(venta_neta_anio_actual) as venta_neta_anio_actual, sum(venta_neta_anio_anterior) as venta_neta_anio_anterior, sum(venta_neta_anio_actual_lfl) as venta_neta_anio_actual_lfl, sum(venta_neta_anio_anterior_lfl) as venta_neta_anio_anterior_lfl, sum(margen_anio_actual) as margen_anio_actual, sum(margen_anio_anterior) as margen_anio_anterior, sum(margen_anio_actual_lfl) as margen_anio_actual_lfl, sum(margen_anio_anterior_lfl) as margen_anio_anterior_lfl, sum(stock_unidades_actual) as stock_unidades_actual, sum(stock_unidades_anterior) as stock_unidades_anterior, sum(stock_anio_actual) as stock_anio_actual, sum(stock_anio_actual_alm) as stock_anio_actual_alm, sum(stock_anio_anterior) as stock_anio_anterior, sum(stock_anio_anterior_alm) as stock_anio_anterior_alm, sum(venta_uds_ultima_semana_anio_actual) as venta_uds_ultima_semana_anio_actual, sum(venta_uds_ultima_semana_anio_anterior) as venta_uds_ultima_semana_anio_anterior, sum(venta_neta_ultima_semana_anio_actual) as venta_neta_ultima_semana_anio_actual, sum(margen_ultima_semana_anio_actual) as margen_ultima_semana_anio_actual from ventas_stock_stg where cast(semana as unsigned) between SEMANA_INICIAL_MES_JD and SEMANA_FINAL ) a; insert into marca_1 select * from marca_total; drop table if exists adm_marca_mensual; create table adm_marca_mensual as select a.totales, a.mercado, a.branch_no, c.store_type, a.marca_1, a.marca, REPLACE(FORMAT(a.Vta_uds_AAct,0),',','.') as Vta_uds_AAct, REPLACE(FORMAT(a.Vta_uds_AAnt,0),',','.') as Vta_uds_AAnt, replace(CONCAT(FORMAT(a.Dif_uds_perc*100,2),'%'),'.',',') as Dif_uds_perc, REPLACE(FORMAT(a.Vta_uds_AAct_lfl,0),',','.') as Vta_uds_AAct_lfl, REPLACE(FORMAT(a.Vta_uds_AAnt_lfl,0),',','.') as Vta_uds_AAnt_lfl, replace(CONCAT(FORMAT(a.Dif_uds_perc_lfl*100,2),'%'),'.',',') as Dif_uds_perc_lfl, REPLACE(FORMAT(a.Vta_AAct,0),',','.') as Vta_AAct, REPLACE(FORMAT(a.Vta_AAnt, 0),',','.') as Vta_AAnt, REPLACE(FORMAT(a.Dif_vta,0),',','.') as Dif_vta, replace(CONCAT(FORMAT(a.Dif_vta_per*100,2),'%'),'.',',') as Dif_vta_per, REPLACE(FORMAT(a.Vta_AAct_lfl,0),',','.') as Vta_AAct_lfl, REPLACE(FORMAT(a.Vta_AAnt_lfl, 0),',','.') as Vta_AAnt_lfl, replace(CONCAT(FORMAT(a.Dif_vta_per_lfl*100,2),'%'),'.',',') as Dif_vta_per_lfl, REPLACE(FORMAT(a.Mg_AAct,0),',','.') as Mg_AAct, REPLACE(FORMAT(a.Mg_AAnt,0),',','.') as Mg_AAnt, REPLACE(FORMAT(a.Dif_Mg, 0),',','.') as Dif_Mg, replace(CONCAT(FORMAT(a.Dif_Mg_per*100,2),'%'),'.',',') as Dif_Mg_per, replace(CONCAT(FORMAT(a.Dif_Mg_per_lfl*100,2),'%'),'.',',') as Dif_Mg_per_lfl, replace(CONCAT(FORMAT(a.GR_AAct*100,2),'%'),'.',',') as GR_AAct, replace(CONCAT(FORMAT(a.GR_AAnt*100,2),'%'),'.',',') as GR_AAnt, REPLACE(FORMAT(a.St_uds_Aact,0),',','.') as St_uds_Aact, REPLACE(FORMAT(a.Dif_St_uds,0),',','.') as Dif_St_uds, replace(CONCAT(FORMAT(a.Dif_St_uds_per*100,2),'%'),'.',',') as Dif_St_uds_per, REPLACE(FORMAT(a.St_AAct_tda,0),',','.') as St_AAct_tda, REPLACE(FORMAT(a.St_AAct_alm,0),',','.') as St_AAct_alm, REPLACE(FORMAT(a.St_AAct_tot,0),',','.') as St_AAct_tot, REPLACE(FORMAT(a.St_AAnt_tda,0),',','.') as St_AAnt_tda, REPLACE(FORMAT(a.St_AAnt_alm,0),',','.') as St_AAnt_alm, REPLACE(FORMAT(a.St_AAnt_tot,0),',','.') as St_AAnt_tot, REPLACE(FORMAT(a.Dif_St,0),',','.') as Dif_St, replace(CONCAT(FORMAT(a.Dif_St_per*100,2),'%'),'.',',') as Dif_St_per, replace(cast(a.cob_AAct as dec(10,2)),'.',',') as cob_AAct, replace(cast(a.cob_AAnt as dec(10,2)),'.',',') as cob_AAnt, (CASE WHEN a.totales in ('SUBTOTAL','TOTAL') then replace(cast(((a.Vta_AAct/b.Vta_AAct)/(a.St_AAct_tot/b.St_AAct_tot)) as dec (10,2)),'.',',') else a.peso_4 end) as vta_sobre_stock, (CASE WHEN a.totales in ('SUBTOTAL','TOTAL') then replace(cast(((a.Mg_AAct/b.Mg_AAct)/(a.St_AAct_tot/b.St_AAct_tot)) as dec(10,2)),'.',',') else a.peso_5 end) as mg_sobre_stock, (case when a.totales in ('SUBTOTAL','TOTAL') then replace(concat(format(((a.Vta_AAct)/(b.Vta_AAct))*100,2),'%'),'.',',') else a.peso_1 end) as Mix_vtas, (case when a.totales in ('SUBTOTAL','TOTAL') then replace(concat(format(((a.Mg_AAct)/(b.Mg_AAct))*100,2),'%'),'.',',') else a.peso_2 end) as Mix_Mg, (CASE WHEN a.totales IN ('SUBTOTAL','TOTAL') then replace(concat(format(((a.St_AAct_tot)/(b.St_AAct_tot))*100,2),'%'),'.',',') else a.peso_3 end) as Mix_Stock from marca_1 a left join marca_total b on a.totales in ('SUBTOTAL','TOTAL') left join md_dbg.branch c on a.branch_no=c.branch_no having Vta_uds_AAct<>0 or Vta_uds_AAnt<>0 or Vta_AAct<>0 or Vta_AAnt<>0; /* eliminamos tablas temporales */ drop table if exists marca_1; drop table if exists marca; drop table if exists marca_subtotal; drop table if exists marca_total; end if; /*---------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ /* TICKETS, FOOTFALL Y OBJETIVOS POR TIENDA Y DIA */ /* tickets areas año actual */ /*modificacion compara 19-22*/ drop table if exists tickets_areas_aact_pre; create table tickets_areas_aact_pre as select distinct a.branch_no, a.sales_Date, a.sales_id, coalesce(b.area,d.area) as area from consultas.rep_pr_sales_detail a left join consultas.articulos b on a.product_code=b.product_code left join consultas.articulos_pt d on a.product_code=d.product_code where sales_date>=FECHA_INICIAL and sales_Date<=FECHA_FINAL and branch_no in (select branch_no from tiendas_ica); drop table if exists tickets_areas_aact_pre_1; create table tickets_areas_aact_pre_1 as select branch_no, sales_Date, (case when area='Textil' then count(distinct sales_id,sales_date,branch_no,area) else 0 end) as num_tickets_aact_textil, (case when area='Calzado' then count(distinct sales_id,sales_date,branch_no,area) else 0 end) as num_tickets_aact_calzado, (case when area in ('Complementos','Material técnico') then count(distinct sales_id,sales_date,branch_no,area) else 0 end) as num_tickets_aact_acc from tickets_areas_aact_pre group by branch_no,sales_Date,area; drop table if exists tickets_areas_aact; create table tickets_areas_aact as select a.branch_no, b.semana_jd as semana, weekday(a.sales_Date) as dia, sum(a.num_tickets_aact_textil) as num_tickets_aact_textil, sum(a.num_tickets_aact_calzado) as num_tickets_aact_calzado, sum(a.num_tickets_aact_acc) as num_tickets_aact_acc from tickets_areas_aact_pre_1 a left join md_dbg.calendario_detalle_jd_v2 b on a.sales_date=b.fecha group by branch_no,sales_Date; alter table tickets_areas_aact add index (branch_no,semana,dia); /* tickets areas año anterior */ /*modificacion compara 19-22*/ drop table if exists tickets_areas_aant_pre; create table tickets_areas_aant_pre as select distinct a.branch_no, a.sales_Date, a.sales_id, coalesce(b.area,d.area) as area /*from md_Dbg.sales_detail_especial_19_20 a */ from consultas.rep_pr_sales_detail a left join consultas.articulos b on a.product_code=b.product_code left join consultas.articulos_pt d on a.product_code=d.product_code where sales_date>=FECHA_INICIAL_ANT and sales_Date<=FECHA_FINAL_ANT and branch_no in (select branch_no from tiendas_ica); drop table if exists tickets_areas_aant_pre_1; create table tickets_areas_aant_pre_1 as select branch_no, sales_Date, (case when area='Textil' then count(distinct sales_id,sales_date,branch_no,area) else 0 end) as num_tickets_aant_textil, (case when area='Calzado' then count(distinct sales_id,sales_date,branch_no,area) else 0 end) as num_tickets_aant_calzado, (case when area in ('Complementos','Material técnico') then count(distinct sales_id,sales_date,branch_no,area) else 0 end) as num_tickets_aant_acc from tickets_areas_aant_pre group by branch_no,sales_Date,area; drop table if exists tickets_areas_aant; create table tickets_areas_aant as select a.branch_no, b.semana_jd as semana, weekday(a.sales_Date) as dia, sum(a.num_tickets_aant_textil) as num_tickets_aant_textil, sum(a.num_tickets_aant_calzado) as num_tickets_aant_calzado, sum(a.num_tickets_aant_acc) as num_tickets_aant_acc from tickets_areas_aant_pre_1 a left join md_dbg.calendario_detalle_jd_v2 b on a.sales_date=b.fecha_comp group by branch_no,sales_Date; alter table tickets_areas_aant add index (branch_no,semana,dia); /* tickets totales año actual */ /*modificacion compara 19-22*/ drop table if exists tickets_aact; create table tickets_aact as select a.branch_no, a.semana, a.dia, sum(a.num_tickets_aact) as num_tickets_aact from (select a.branch_no, b.semana_jd as semana, weekday(a.sales_Date) as dia, count(distinct a.sales_id,a.sales_date,a.branch_no) as num_tickets_aact from md_Dbg.sales_head a left join md_dbg.calendario_detalle_jd_v2 b on a.sales_date=b.fecha where a.sales_date>=FECHA_INICIAL and a.sales_Date<=FECHA_FINAL and branch_no in (select branch_no from tiendas_ica) group by a.branch_no,a.sales_Date) a group by a.branch_no,a.semana,a.dia; alter table tickets_aact add index (branch_no,semana,dia); /* tickets totales año anterior */ /*modificacion compara 19-22*/ drop table if exists tickets_aant; create table tickets_aant as select a.branch_no, a.semana, a.dia, sum(a.num_tickets_aant) as num_tickets_aant from (select a.branch_no, b.semana_jd as semana, weekday(a.sales_Date) as dia, count(distinct a.sales_id,a.sales_date,a.branch_no) as num_tickets_aant /*from md_Dbg.sales_detail_especial_19_20 a */ from consultas.rep_pr_sales_detail a left join md_dbg.calendario_detalle_jd_v2 b on a.sales_date=b.fecha where a.sales_date>=FECHA_INICIAL_ANT and a.sales_Date<=FECHA_FINAL_ANT and branch_no in (select branch_no from tiendas_ica) group by a.branch_no,a.sales_Date) a group by a.branch_no,a.semana,a.dia; alter table tickets_aant add index (branch_no,semana,dia); /* tickets totales año actual SIN SERVICIOS */ drop table if exists tmp_pre_spr_tickets_aact_NO_SERV; create table tmp_pre_spr_tickets_aact_NO_SERV select a.branch_no, a.sales_Date, a.sales_id, d.product_code from md_Dbg.sales_head a INNER JOIN consultas.rep_pr_sales_detail d ON a.sales_id = d.sales_id and a.branch_no = d.branch_no and a.sales_Date = d.sales_date INNER JOIN tiendas_ica t ON a.branch_no = t.branch_no where a.sales_date>=FECHA_INICIAL and a.sales_Date<=FECHA_FINAL; alter table tmp_pre_spr_tickets_aact_NO_SERV add index idx(branch_no, sales_id, sales_date); alter table tmp_pre_spr_tickets_aact_NO_SERV add index idx_product(product_code); drop table if exists tmp_spr_tickets_aact_NO_SERV; create table tmp_spr_tickets_aact_NO_SERV select a.branch_no, b.semana_jd as semana, weekday(a.sales_Date) as dia, count(distinct a.sales_id,a.sales_date,a.branch_no) as num_tickets_aact from tmp_pre_spr_tickets_aact_NO_SERV a INNER JOIN consultas.articulos_NO_SERV_ica p ON a.product_code = p.product_code left join md_dbg.calendario_detalle_jd_v2 b on a.sales_date=b.fecha group by a.branch_no,a.sales_Date; /*modificacion compara 19-22*/ drop table if exists tmp_tickets_aact_NO_SERV; create table tmp_tickets_aact_NO_SERV SELECT * FROM tmp_spr_tickets_aact_NO_SERV; drop table if exists tickets_aact_NO_SERV; create table tickets_aact_NO_SERV select a.branch_no, a.semana, a.dia, sum(a.num_tickets_aact) as num_tickets_aact FROM tmp_tickets_aact_NO_SERV a group by a.branch_no,a.semana,a.dia; alter table tickets_aact_NO_SERV add index (branch_no,semana,dia); /* tickets totales año anterior SIN SERVICIOS */ /*modificacion compara 19-22*/ drop table if exists tmp_pre_spr_tickets_aant_NO_SERV; create table tmp_pre_spr_tickets_aant_NO_SERV select a.branch_no, a.sales_Date, a.sales_id, a.product_code from consultas.rep_pr_sales_detail a /*from md_Dbg.sales_detail_especial_19_20 a */ -- INNER JOIN md_Dbg.sales_detail d ON a.sales_id = d.sales_id and a.branch_no = d.branch_no and a.sales_Date = d.sales_date INNER JOIN tiendas_ica t ON a.branch_no = t.branch_no where a.sales_date>=FECHA_INICIAL_ANT and a.sales_Date<=FECHA_FINAL_ANT; alter table tmp_pre_spr_tickets_aant_NO_SERV add index idx(branch_no, sales_id, sales_date); alter table tmp_pre_spr_tickets_aant_NO_SERV add index idx_product(product_code); drop table if exists tmp_spr_tickets_aant_NO_SERV; create table tmp_spr_tickets_aant_NO_SERV select a.branch_no, b.semana_jd as semana, weekday(a.sales_Date) as dia, count(distinct a.sales_id,a.sales_date,a.branch_no) as num_tickets_aant from tmp_pre_spr_tickets_aant_NO_SERV a INNER JOIN consultas.articulos_total_NO_SERV_ica p ON a.product_code = p.product_code left join md_dbg.calendario_detalle_jd_v2 b on a.sales_date=b.fecha group by a.branch_no,a.sales_Date; /*modificacion compara 19-22*/ drop table if exists tmp_tickets_aant_NO_SERV; create table tmp_tickets_aant_NO_SERV SELECT * FROM tmp_spr_tickets_aant_NO_SERV; drop table if exists tickets_aant_NO_SERV; create table tickets_aant_NO_SERV select a.branch_no, a.semana, a.dia, sum(a.num_tickets_aant) as num_tickets_aant FROM tmp_tickets_aant_NO_SERV a group by a.branch_no,a.semana,a.dia; alter table tickets_aant_NO_SERV add index (branch_no,semana,dia); /* footfall año actual */ drop table if exists footfall_aact; create table footfall_aact as select a.branch_no, b.semana_jd as semana, weekday(a.Date) as dia, sum(footfall_in) as entradas_aact from md_Dbg.footfall a left join md_Dbg.calendario_Detalle_jd_v2 b on a.Date=b.fecha where date>=FECHA_INICIAL and Date<=FECHA_FINAL and branch_no in (select branch_no from tiendas_ica) group by branch_no,date; alter table footfall_aact add index (branch_no,semana,dia); /* footfall año anterior */ /*modificacion compara 19-22*/ drop table if exists footfall_aant; create table footfall_aant as select a.branch_no, b.semana_jd as semana, weekday(a.Date) as dia, sum(footfall_in) as entradas_aant from md_Dbg.footfall a left join md_Dbg.calendario_Detalle_jd_v2 b on a.Date=b.fecha_comp /*where date>=FECHA_INICIAL_ANT and Date<=FECHA_FINAL_ANT*/ /*where date>=FECHA_INICIAL_ANT_ANT_ANT and Date<=FECHA_FINAL_ANT_ANT_ANT*/ where date>=FECHA_INICIAL_ANT and Date<=FECHA_FINAL_ANT and branch_no in (select branch_no from tiendas_ica) group by branch_no,date; alter table footfall_aant add index (branch_no,semana,dia); /* creamos una tabla maestra de IVA generalizado para cada uno de los mercado */ drop table if exists tmp_iva_aact; create table tmp_iva_aact as select distinct a.percentage/100 as perc_iva_aact,b.mercado, a.id_mercado, ini_date_vat, fin_date_vat from md_Dbg.vats a left join md_dbg.branch b on a.id_mercado=b.id_mercado where VAT_CODE='GEN' and VAT_GROUP='NAV' and ((FECHA_INICIAL>=ini_date_vat and FECHA_INICIAL<=fin_date_vat) or (FECHA_FINAL>=ini_date_vat and FECHA_FINAL<=fin_date_vat) ); drop table if exists tmp_iva_aact_branch; create table tmp_iva_aact_branch as select distinct a.percentage/100 as perc_iva_aact, a.branch_no, b.mercado, a.id_mercado, ini_date_vat, fin_date_vat from md_Dbg.vats_branch_exception a left join md_dbg.branch b on a.id_mercado=b.id_mercado where VAT_CODE='GEN' and VAT_GROUP='NAV' and ((FECHA_INICIAL>=ini_date_vat and FECHA_INICIAL<=fin_date_vat) or (FECHA_FINAL>=ini_date_vat and FECHA_FINAL<=fin_date_vat) ); drop table if exists iva_aact; create table iva_aact as select a.branch_no, COALESCE(c.perc_iva_aact,b.perc_iva_aact) as perc_iva_aact, coalesce(c.ini_date_vat,b.ini_date_vat) as ini_date_vat, coalesce(c.fin_date_vat,b.fin_date_vat) as fin_date_vat from md_dbg.branch a left outer join consultas.tmp_iva_aact b on a.id_mercado = b.id_mercado left outer join consultas.tmp_iva_aact_branch c on a.branch_no = c.branch_no ; alter table iva_aact add index idx(branch_no, ini_date_vat, fin_date_vat); drop table if exists tmp_iva_aant; create table tmp_iva_aant as select distinct a.percentage/100 as perc_iva_aant,b.mercado, a.id_mercado from md_Dbg.vats a left join md_dbg.branch b on a.id_mercado=b.id_mercado where VAT_CODE='GEN' and VAT_GROUP='NAV' and ((FECHA_INICIAL_ANT>=ini_date_vat and FECHA_INICIAL_ANT<=fin_date_vat) or (FECHA_FINAL_ANT>=ini_date_vat and FECHA_FINAL_ANT<=fin_date_vat) ); drop table if exists tmp_iva_aant_branch; create table tmp_iva_aant_branch as select distinct a.percentage/100 as perc_iva_aant, a.branch_no, b.mercado, a.id_mercado from md_Dbg.vats_branch_exception a left join md_dbg.branch b on a.id_mercado=b.id_mercado where VAT_CODE='GEN' and VAT_GROUP='NAV' and ((FECHA_INICIAL_ANT>=ini_date_vat and FECHA_INICIAL_ANT<=fin_date_vat) or (FECHA_FINAL_ANT>=ini_date_vat and FECHA_FINAL_ANT<=fin_date_vat) ); drop table if exists iva_aant; create table iva_aant as select a.branch_no, COALESCE(c.perc_iva_aant,b.perc_iva_aant) as perc_iva_aact from md_dbg.branch a left outer join consultas.tmp_iva_aant b on a.id_mercado = b.id_mercado left outer join consultas.tmp_iva_aant_branch c on a.branch_no = c.branch_no ; alter table iva_aant add index idx(branch_no); /*------------------------------------------------------------------------------*/ -- LBI-190 -- nuevos campos "Store_Target_TY" y "GPV_Store_Target_TY" /* objetivo año actual */ DROP table if exists tmp_objetivo_aact; create table tmp_objetivo_aact as select a.branch_no, b.fecha, b.semana_jd as semana, weekday(a.Date) as dia, sum(case when a.min_target is null or a.min_target='' then 0 else a.min_target end) as objetivo, sum(case when a.max_target is null or a.max_target='' then 0 else a.max_target end) as objetivo_max_target, avg(a.Mrg_Target_Perc) as Objetivo_MG_perc from md_dbg.target a left join md_dbg.calendario_Detalle_jd_v2 b on a.Date=b.fecha where date>=FECHA_INICIAL and date<=FECHA_FINAL and a.branch_no in (select branch_no from tiendas_ica) group by b.fecha,a.branch_no,b.semana_jd,weekday(a.Date); alter table tmp_objetivo_aact add index idx(branch_no,fecha,dia,semana); -- LBI-190 -- nuevos campos "Store_Target_TY" y "GPV_Store_Target_TY" DROP table if exists objetivo_aact; create table objetivo_aact as select o.branch_no, o.fecha, o.semana, o.dia, o.objetivo, o.Objetivo_MG_perc /*, format(coalesce(o.objetivo/(1+i.perc_iva_aact),0),2,'de_DE') as Target_TY , format(coalesce((o.objetivo/((1+i.perc_iva_aact)))*(o.Objetivo_MG_perc/100),0),2,'de_DE') as GPV_Target_TY*/ , coalesce(o.objetivo/(1+i.perc_iva_aact),0) as Target_TY , coalesce(o.objetivo_max_target/(1+i.perc_iva_aact),0) as Store_Target_TY , coalesce((o.objetivo/((1+i.perc_iva_aact)))*(o.Objetivo_MG_perc/100),0) as GPV_Target_TY , coalesce((o.objetivo_max_target/((1+i.perc_iva_aact)))*(o.Objetivo_MG_perc/100),0) as GPV_Store_Target_TY from tmp_objetivo_aact o left join iva_aact i on o.branch_no = i.branch_no and o.fecha between i.ini_date_vat and i.fin_date_vat; alter table objetivo_aact add index idx(branch_no,fecha,dia,semana); /* objetivo año anterior */ /*modificacion compara 19-22*/ DROP table if exists objetivo_aant; create table objetivo_aant as select a.branch_no, b.semana_jd as semana, weekday(a.Date) as dia, sum(case when a.min_target is null or a.min_target='' then 0 else a.min_target end) as objetivo, avg(a.Mrg_Target_Perc) as Objetivo_MG_perc from md_dbg.target a left join md_dbg.calendario_Detalle_jd_v2 b on a.Date=b.fecha where date>=FECHA_INICIAL_ANT and date<=FECHA_FINAL_ANT and a.branch_no in (select branch_no from tiendas_ica) /*AND a.branch_no NOT IN ('911','9PT','1722','1723','9FR','9IT')*/ group by a.branch_no,b.semana_jd,weekday(a.Date); /*insert into objetivo_aant select a.branch_no, b.semana_jd as semana, weekday(a.Date) as dia, sum(case when a.min_target is null or a.min_target='' then 0 else a.min_target end) as objetivo, avg(a.Mrg_Target_Perc) as Objetivo_MG_perc from md_dbg.target a left join md_dbg.calendario_Detalle_jd_v2 b on a.Date=b.fecha where date>=FECHA_INICIAL_ANT and date<=FECHA_FINAL_ANT and a.branch_no in (select branch_no from tiendas_ica) AND a.branch_no IN ('911','9PT','1722','1723','9FR','9IT') group by a.branch_no,b.semana_jd,weekday(a.Date);*/ alter table objetivo_aant add index idx(branch_no,dia,semana); /* eliminamos tablas temporales */ drop table if exists ventas_anio_actual; drop table if exists ventas_anio_anterior; drop table if exists ventas_stock_actual_agru; drop table if exists ventas_stock_anterior_agru; drop table if exists tickets_areas_aant_pre; drop table if exists tickets_areas_aant_pre_1; drop table if exists tickets_areas_aacT_pre; drop table if exists tickets_areas_aact_pre_1; drop table if exists tickets_areas_aact_pre_12; drop table if exists tickets_areas_aact_pre_11; /* TICKETS POR TIENDA Y DIA PARA APP */ /* tickets areas año actual APP */ drop table if exists tickets_areas_aact_pre_app; create table tickets_areas_aact_pre_app as select distinct a.branch_no, a.sales_Date, a.sales_id, b.area from consultas.rep_pr_sales_detail a left join consultas.articulos b on a.product_code=b.product_code where sales_date>=FECHA_INICIAL and sales_Date<=FECHA_FINAL and a.branch_no in (select branch_no from tiendas_ica) and a.channel_code='3'; drop table if exists tickets_areas_aact_pre_1_app; create table tickets_areas_aact_pre_1_app as select branch_no, sales_Date, (case when area='Textil' then count(distinct sales_id,sales_date,branch_no,area) else 0 end) as num_tickets_aact_textil, (case when area='Calzado' then count(distinct sales_id,sales_date,branch_no,area) else 0 end) as num_tickets_aact_calzado, (case when area in ('Complementos','Material técnico') then count(distinct sales_id,sales_date,branch_no,area) else 0 end) as num_tickets_aact_acc from tickets_areas_aact_pre_app group by branch_no,sales_Date,area; drop table if exists tickets_areas_aact_app; create table tickets_areas_aact_app as select a.branch_no, b.semana_jd as semana, weekday(a.sales_Date) as dia, sum(a.num_tickets_aact_textil) as num_tickets_aact_textil, sum(a.num_tickets_aact_calzado) as num_tickets_aact_calzado, sum(a.num_tickets_aact_acc) as num_tickets_aact_acc from tickets_areas_aact_pre_1_app a left join md_dbg.calendario_detalle_jd_v2 b on a.sales_date=b.fecha group by branch_no,sales_Date; alter table tickets_areas_aact_app add index (branch_no,semana,dia); /* tickets areas año anterior APP */ /*modificacion compara 19-22*/ drop table if exists tickets_areas_aant_pre_app; create table tickets_areas_aant_pre_app as select distinct a.branch_no, a.sales_Date, a.sales_id, ifnull(b.area,c.area) area /*from md_Dbg.sales_detail a left join consultas.articulos b on a.product_code=b.product_code*/ /*from md_Dbg.sales_detail_especial_19_20 a*/ from consultas.rep_pr_sales_detail a left join consultas.articulos b on a.product_code=b.product_code left join consultas.articulos_pt c on a.product_code=c.product_code where sales_date>=FECHA_INICIAL_ANT and sales_Date<=FECHA_FINAL_ANT and a.branch_no in (select branch_no from tiendas_ica) and a.channel_code='3'; drop table if exists tickets_areas_aant_pre_1_app; create table tickets_areas_aant_pre_1_app as select branch_no, sales_Date, (case when area='Textil' then count(distinct sales_id,sales_date,branch_no,area) else 0 end) as num_tickets_aant_textil, (case when area='Calzado' then count(distinct sales_id,sales_date,branch_no,area) else 0 end) as num_tickets_aant_calzado, (case when area in ('Complementos','Material técnico') then count(distinct sales_id,sales_date,branch_no,area) else 0 end) as num_tickets_aant_acc from tickets_areas_aant_pre_app group by branch_no,sales_Date,area; drop table if exists tickets_areas_aant_app; create table tickets_areas_aant_app as select a.branch_no, b.semana_jd as semana, weekday(a.sales_Date) as dia, sum(a.num_tickets_aant_textil) as num_tickets_aant_textil, sum(a.num_tickets_aant_calzado) as num_tickets_aant_calzado, sum(a.num_tickets_aant_acc) as num_tickets_aant_acc from tickets_areas_aant_pre_1_app a left join md_dbg.calendario_detalle_jd_v2 b on a.sales_date=b.fecha_comp group by branch_no,sales_Date; alter table tickets_areas_aant_app add index (branch_no,semana,dia); /* tickets totales año actual APP */ drop table if exists tickets_aact_app; create table tickets_aact_app as select a.branch_no, b.semana_jd as semana, weekday(a.sales_Date) as dia, count(distinct a.sales_id,a.sales_date,a.branch_no) as num_tickets_aact from consultas.rep_pr_sales_detail a left join md_dbg.calendario_detalle_jd_v2 b on a.sales_date=b.fecha where a.sales_date>=FECHA_INICIAL and a.sales_Date<=FECHA_FINAL and a.branch_no in (select branch_no from tiendas_ica) and a.channel_code='3' group by a.branch_no,a.sales_Date; alter table tickets_aact_app add index (branch_no,semana,dia); /* tickets totales año anterior APP */ /*modificacion compara 19-21*/ drop table if exists tickets_aant_app; create table tickets_aant_app as select a.branch_no, b.semana_jd as semana, weekday(a.sales_Date) as dia, count(distinct sales_id,sales_date,branch_no) as num_tickets_aant /*from md_Dbg.sales_detail a left join md_Dbg.calendario_Detalle_jd_v2 b*/ /*from md_Dbg.sales_detail_especial_19_20 a left join md_Dbg.calendario_Detalle_jd_v2 b*/ from consultas.rep_pr_sales_detail a left join md_Dbg.calendario_Detalle_jd_v2 b on a.sales_Date=b.fecha_comp where sales_date>=FECHA_INICIAL_ANT and sales_Date<=FECHA_FINAL_ANT and a.branch_no in (select branch_no from tiendas_ica) and a.channel_code='3' group by branch_no,sales_Date; alter table tickets_aant_app add index (branch_no,semana,dia); /* eliminamos tablas temporales APP */ drop table if exists ventas_anio_actual_app; drop table if exists ventas_anio_anterior_app; drop table if exists ventas_stock_actual_agru_app; drop table if exists ventas_stock_anterior_agru_app; drop table if exists tickets_areas_aant_pre_app; drop table if exists tickets_areas_aant_pre_1_app; drop table if exists tickets_areas_aact_pre_app; drop table if exists tickets_areas_aact_pre_1_app; /************************************************************************************************/ -- ---------------------------------------------------------------------------------------------------- /* TABLA PRODUCTO (VS2021/2019) */ /* TABLA PRODUCTO (VS2022/2019) -> Revision 2022/02/02 */ -- ---------------------------------------------------------------------------------------------------- if SEMANA_INICIAL = 1 AND SEMANA_FINAL = 1 then insert into PRODUCTO_historico (Anio_jd, Week_Number, semana, Mercado, Fare_Code, udp_code, udp_name, area, serie, seccion_a, seccion, familia, disenyo, marca, brand_desc, product_code, descripcion, Temporada, Temporalidad, Descatalogado, precio_original, precio_actual, venta_uds_anio_actual, venta_uds_anio_anterior, venta_uds_anio_actual_lfl, venta_uds_anio_anterior_lfl, venta_neta_anio_Actual, venta_neta_anio_anterior, venta_neta_anio_Actual_lfl, venta_neta_anio_anterior_lfl, venta_bruta_anio_Actual, venta_bruta_anio_anterior, margen_anio_actual, margen_anio_anterior, margen_anio_actual_lfl, margen_anio_anterior_lfl, stock_anio_actual, stock_anio_anterior, stock_unidades_actual, stock_unidades_anterior, stock_anio_actual_alm, stock_anio_anterior_alm, stock_uds_anio_actual_alm, stock_uds_anio_anterior_alm) select ANIO_ACTUAL-1 as Anio_jd, Week_Number, semana, Mercado, Fare_Code, udp_code, udp_name, area, serie, seccion_a, seccion, familia, disenyo, marca, brand_desc, product_code, descripcion, Temporada, Temporalidad, Descatalogado, precio_original, precio_actual, venta_uds_anio_actual, venta_uds_anio_anterior, venta_uds_anio_actual_lfl, venta_uds_anio_anterior_lfl, venta_neta_anio_Actual, venta_neta_anio_anterior, venta_neta_anio_Actual_lfl, venta_neta_anio_anterior_lfl, venta_bruta_anio_Actual, venta_bruta_anio_anterior, margen_anio_actual, margen_anio_anterior, margen_anio_actual_lfl, margen_anio_anterior_lfl, stock_anio_actual, stock_anio_anterior, stock_unidades_actual, stock_unidades_anterior, stock_anio_actual_alm, stock_anio_anterior_alm, stock_uds_anio_actual_alm, stock_uds_anio_anterior_alm from PRODUCTO_stg; truncate table PRODUCTO_stg; else delete from PRODUCTO_stg where Week_Number between SEMANA_INICIAL and SEMANA_FINAL; end if; -- Insertamos en la tabla PRODUCTO_stg los nuevos datos calculados en ventas_stock1 insert into PRODUCTO_stg (Week_Number, semana, Mercado, Fare_Code, udp_code, udp_name, area, serie, seccion_a, seccion, familia, disenyo, marca, brand_desc, product_code, descripcion, Temporada, Temporalidad, Descatalogado, precio_original, precio_actual, venta_uds_anio_actual, venta_uds_anio_anterior, venta_uds_anio_actual_lfl, venta_uds_anio_anterior_lfl, venta_neta_anio_Actual, venta_neta_anio_anterior, venta_neta_anio_Actual_lfl, venta_neta_anio_anterior_lfl, venta_bruta_anio_Actual, venta_bruta_anio_anterior, margen_anio_actual, margen_anio_anterior, margen_anio_actual_lfl, margen_anio_anterior_lfl, stock_anio_actual, stock_anio_anterior, stock_unidades_actual, stock_unidades_anterior, stock_anio_actual_alm, stock_anio_anterior_alm, stock_uds_anio_actual_alm, stock_uds_anio_anterior_alm) select cast(semana as unsigned) as Week_Number, semana, Mercado, Fare_Code, udp_code, udp_name, area, serie, seccion_a, seccion, familia, disenyo, marca, brand_desc, product_code, descripcion, Temporada, Temporalidad, Descatalogado, precio_original, precio_actual, sum(venta_uds_anio_actual) as venta_uds_anio_actual, sum(venta_uds_anio_anterior) as venta_uds_anio_anterior, sum(venta_uds_anio_actual_lfl) as venta_uds_anio_actual_lfl, sum(venta_uds_anio_anterior_lfl) as venta_uds_anio_anterior_lfl, sum(venta_neta_anio_Actual) as venta_neta_anio_Actual, sum(venta_neta_anio_anterior) as venta_neta_anio_anterior, sum(venta_neta_anio_Actual_lfl) as venta_neta_anio_Actual_lfl, sum(venta_neta_anio_anterior_lfl) as venta_neta_anio_anterior_lfl, sum(venta_bruta_anio_Actual) as venta_bruta_anio_Actual, sum(venta_bruta_anio_anterior) as venta_bruta_anio_anterior, sum(margen_anio_actual) as margen_anio_actual, sum(margen_anio_anterior) as margen_anio_anterior, sum(margen_anio_actual_lfl) as margen_anio_actual_lfl, sum(margen_anio_anterior_lfl) as margen_anio_anterior_lfl, sum(stock_anio_actual) as stock_anio_actual, sum(stock_anio_anterior) as stock_anio_anterior, sum(stock_unidades_actual) as stock_unidades_actual, sum(stock_unidades_anterior) as stock_unidades_anterior, sum(stock_anio_actual_alm) as stock_anio_actual_alm, sum(stock_anio_anterior_alm) as stock_anio_anterior_alm, sum(stock_uds_anio_actual_alm) as stock_uds_anio_actual_alm, sum(stock_uds_anio_anterior_alm) as stock_uds_anio_anterior_alm from ventas_stock1 group by cast(semana as unsigned), semana, Mercado,Fare_Code,udp_code,udp_name,area,serie,seccion_a,seccion,familia,disenyo,marca,brand_desc,product_code,descripcion,Temporada,Temporalidad,Descatalogado,precio_original,precio_actual; -- Generamos la tabla producto semanal, para la extración semanal de los excels DROP table if exists PRODUCTO_semanal; create table PRODUCTO_semanal as select a.Mercado as Market, a.Fare_Code, a.udp_code as UDP_Code, a.udp_name as UDP_Name, a.area as Area, (case when a.area='Textil' then 'APPAREL' when a.area='Calzado' then 'FWR' when a.area in ('Material técnico','Complementos') then 'ACC&&HEQ' when a.area in ('Promociones','Otros','na','Servicios') then 'OTHER' end) as Area2, a.serie as Gender, (case when a.serie in ('UNISEX') and a.area in ('Textil','Calzado','Complementos','Material técnico') then 'UNISEX' when a.serie in ('UNISEX','na') and a.area in ('Promociones','Otros','Servicios','na') then 'OTHER' when a.serie in ('CABALLERO','XMAN') and a.area in ('Textil','Calzado','Complementos','Material técnico') then 'MAN' when a.serie in ('SEÑORA','LADY','JR. x SRA.') and a.area in ('Textil','Calzado','Complementos','Material técnico') then 'WOMAN' else 'JCI' end) as Gender2, a.seccion_a as Activity, a.seccion as Section, a.familia as Category, a.disenyo as Style, a.marca as Brand, a.brand_desc as Brand_Type, a.product_code as Product_Id, a.descripcion as Product_Description, a.Temporada, a.Temporalidad, a.Descatalogado, format(a.precio_original,2,'de_DE') as First_Price, format(a.precio_actual,2,'de_DE') as Current_Price, format(sum(a.venta_uds_anio_actual),0,'de_DE') as Units_Sold_TY, format(sum(a.venta_uds_anio_anterior),0,'de_DE') as Units_Sold_LY, format(sum(a.venta_uds_anio_actual_lfl),0,'de_DE') as Units_Sold_LFL_TY, format(sum(a.venta_uds_anio_anterior_lfl),0,'de_DE') as Units_Sold_LFL_LY, format(sum(a.venta_neta_anio_Actual),2,'de_DE') as Net_Sales_TY, format(sum(a.venta_neta_anio_anterior),2,'de_DE') as Net_Sales_LY, format(sum(a.venta_neta_anio_Actual_lfl),2,'de_DE') as Net_Sales_LFL_TY, format(sum(a.venta_neta_anio_anterior_lfl),2,'de_DE') as Net_Sales_LFL_LY, format(sum(a.venta_bruta_anio_Actual),2,'de_DE') as Gross_Sales_TY, format(sum(a.venta_bruta_anio_anterior),2,'de_DE') as Gross_Sales_LY, format(sum(a.margen_anio_actual),2,'de_DE') as GPV_TY, format(sum(a.margen_anio_anterior),2,'de_DE') as GPV_LY, format(sum(a.margen_anio_actual_lfl),2,'de_DE') as GPV_LFL_TY, format(sum(a.margen_anio_anterior_lfl),2,'de_DE') as GPV_LFL_LY, format(sum(a.stock_anio_actual),2,'de_DE') as Total_Stk_Eur_TY, format(sum(a.stock_anio_anterior),2,'de_DE') as Total_Stk_Eur_LY, format(sum(a.stock_unidades_actual),0,'de_DE') as Total_Stk_Units_TY, format(sum(a.stock_unidades_anterior),0,'de_DE') as Total_Stk_Units_LY, format(sum(a.stock_anio_actual)-sum(stock_anio_actual_alm),2,'de_DE') as Branch_Stk_Eur_TY, format(sum(a.stock_anio_anterior)-sum(stock_anio_anterior_alm),2,'de_DE') as Branch_Stk_Eur_LY, format(sum(a.stock_unidades_actual)-sum(stock_uds_anio_actual_alm),0,'de_DE') as Branch_Stk_Units_TY, format(sum(a.stock_unidades_anterior)-sum(stock_uds_anio_anterior_alm),0,'de_DE') as Branch_Stk_Units_LY, format(sum(a.stock_anio_actual_alm),2,'de_DE') as WH_Stk_Eur_TY, format(sum(a.stock_anio_anterior_alm),2,'de_DE') as WH_Stk_Eur_LY, format(sum(a.stock_uds_anio_actual_alm),0,'de_DE') as WH_Stk_Units_TY, format(sum(a.stock_uds_anio_anterior_alm),0,'de_DE') as WH_Stk_Units_LY from PRODUCTO_stg a where Week_Number = SEMANA_FINAL group by Mercado, Fare_Code, udp_code,udp_name,area,(case when area='Textil' then 'APPAREL' when area='Calzado' then 'FWR' when area in ('Material técnico','Complementos') then 'ACC&&HEQ' when area in ('Promociones','Otros','na','Servicios') then 'OTHER' end),serie,(case when serie in ('UNISEX') and area in ('Textil','Calzado','Complementos','Material técnico') then 'UNISEX' when serie in ('UNISEX','na') and area in ('Promociones','Otros','Servicios','na') then 'OTHER' when serie in ('CABALLERO','XMAN') and area in ('Textil','Calzado','Complementos','Material técnico') then 'MAN' when serie in ('SEÑORA','LADY','JR. x SRA.') and area in ('Textil','Calzado','Complementos','Material técnico') then 'MAN' else 'JCI' end),seccion_a,seccion,familia,disenyo,marca,brand_desc,product_code,descripcion,Temporada, Temporalidad,Descatalogado,precio_original,precio_actual; -- Generamos la tabla producto acumulado, para la extración acumulada de los excels DROP table if exists PRODUCTO_acumulado; create table PRODUCTO_acumulado as select a.Mercado as Market, a.Fare_Code, a.udp_code as UDP_Code, a.udp_name as UDP_Name, a.area as Area, (case when a.area='Textil' then 'APPAREL' when a.area='Calzado' then 'FWR' when a.area in ('Material técnico','Complementos') then 'ACC&&HEQ' when a.area in ('Promociones','Otros','na','Servicios') then 'OTHER' end) as Area2, a.serie as Gender, (case when a.serie in ('UNISEX') and a.area in ('Textil','Calzado','Complementos','Material técnico') then 'UNISEX' when a.serie in ('UNISEX','na') and a.area in ('Promociones','Otros','Servicios','na') then 'OTHER' when a.serie in ('CABALLERO','XMAN') and a.area in ('Textil','Calzado','Complementos','Material técnico') then 'MAN' when a.serie in ('SEÑORA','LADY','JR. x SRA.') and a.area in ('Textil','Calzado','Complementos','Material técnico') then 'WOMAN' else 'JCI' end) as Gender2, a.seccion_a as Activity, a.seccion as Section, a.familia as Category, a.disenyo as Style, a.marca as Brand, a.brand_desc as Brand_Type, a.product_code as Product_Id, a.descripcion as Product_Description, a.Temporada, a.Temporalidad, a.Descatalogado, format(a.precio_original,2,'de_DE') as First_Price, format(a.precio_actual,2,'de_DE') as Current_Price, format(sum(a.venta_uds_anio_actual),0,'de_DE') as Units_Sold_TY, format(sum(a.venta_uds_anio_anterior),0,'de_DE') as Units_Sold_LY, format(sum(a.venta_uds_anio_actual_lfl),0,'de_DE') as Units_Sold_LFL_TY, format(sum(a.venta_uds_anio_anterior_lfl),0,'de_DE') as Units_Sold_LFL_LY, format(sum(a.venta_neta_anio_Actual),2,'de_DE') as Net_Sales_TY, format(sum(a.venta_neta_anio_anterior),2,'de_DE') as Net_Sales_LY, format(sum(a.venta_neta_anio_Actual_lfl),2,'de_DE') as Net_Sales_LFL_TY, format(sum(a.venta_neta_anio_anterior_lfl),2,'de_DE') as Net_Sales_LFL_LY, format(sum(a.venta_bruta_anio_Actual),2,'de_DE') as Gross_Sales_TY, format(sum(a.venta_bruta_anio_anterior),2,'de_DE') as Gross_Sales_LY, format(sum(a.margen_anio_actual),2,'de_DE') as GPV_TY, format(sum(a.margen_anio_anterior),2,'de_DE') as GPV_LY, format(sum(a.margen_anio_actual_lfl),2,'de_DE') as GPV_LFL_TY, format(sum(a.margen_anio_anterior_lfl),2,'de_DE') as GPV_LFL_LY, format(sum(a.stock_anio_actual),2,'de_DE') as Total_Stk_Eur_TY, format(sum(a.stock_anio_anterior),2,'de_DE') as Total_Stk_Eur_LY, format(sum(a.stock_unidades_actual),0,'de_DE') as Total_Stk_Units_TY, format(sum(a.stock_unidades_anterior),0,'de_DE') as Total_Stk_Units_LY, format(sum(a.stock_anio_actual)-sum(stock_anio_actual_alm),2,'de_DE') as Branch_Stk_Eur_TY, format(sum(a.stock_anio_anterior)-sum(stock_anio_anterior_alm),2,'de_DE') as Branch_Stk_Eur_LY, format(sum(a.stock_unidades_actual)-sum(stock_uds_anio_actual_alm),0,'de_DE') as Branch_Stk_Units_TY, format(sum(a.stock_unidades_anterior)-sum(stock_uds_anio_anterior_alm),0,'de_DE') as Branch_Stk_Units_LY, format(sum(a.stock_anio_actual_alm),2,'de_DE') as WH_Stk_Eur_TY, format(sum(a.stock_anio_anterior_alm),2,'de_DE') as WH_Stk_Eur_LY, format(sum(a.stock_uds_anio_actual_alm),0,'de_DE') as WH_Stk_Units_TY, format(sum(a.stock_uds_anio_anterior_alm),0,'de_DE') as WH_Stk_Units_LY from PRODUCTO_stg a where Week_Number between 1 and SEMANA_FINAL group by Mercado, Fare_Code, udp_code,udp_name,area,(case when area='Textil' then 'APPAREL' when area='Calzado' then 'FWR' when area in ('Material técnico','Complementos') then 'ACC&&HEQ' when area in ('Promociones','Otros','na','Servicios') then 'OTHER' end),serie,(case when serie in ('UNISEX') and area in ('Textil','Calzado','Complementos','Material técnico') then 'UNISEX' when serie in ('UNISEX','na') and area in ('Promociones','Otros','Servicios','na') then 'OTHER' when serie in ('CABALLERO','XMAN') and area in ('Textil','Calzado','Complementos','Material técnico') then 'MAN' when serie in ('SEÑORA','LADY','JR. x SRA.') and area in ('Textil','Calzado','Complementos','Material técnico') then 'MAN' else 'JCI' end),seccion_a,seccion,familia,disenyo,marca,brand_desc,product_code,descripcion,Temporada, Temporalidad,Descatalogado,precio_original,precio_actual; -- Creamos la tabla producto para la temporalidad mensual, solo si es la última semana_jd del mes_jd actual if FIN_MES_JD then DROP table if exists PRODUCTO_mensual; create table PRODUCTO_mensual as select a.Mercado as Market, a.Fare_Code, a.udp_code as UDP_Code, a.udp_name as UDP_Name, a.area as Area, (case when a.area='Textil' then 'APPAREL' when a.area='Calzado' then 'FWR' when a.area in ('Material técnico','Complementos') then 'ACC&&HEQ' when a.area in ('Promociones','Otros','na','Servicios') then 'OTHER' end) as Area2, a.serie as Gender, (case when a.serie in ('UNISEX') and a.area in ('Textil','Calzado','Complementos','Material técnico') then 'UNISEX' when a.serie in ('UNISEX','na') and a.area in ('Promociones','Otros','Servicios','na') then 'OTHER' when a.serie in ('CABALLERO','XMAN') and a.area in ('Textil','Calzado','Complementos','Material técnico') then 'MAN' when a.serie in ('SEÑORA','LADY','JR. x SRA.') and a.area in ('Textil','Calzado','Complementos','Material técnico') then 'WOMAN' else 'JCI' end) as Gender2, a.seccion_a as Activity, a.seccion as Section, a.familia as Category, a.disenyo as Style, a.marca as Brand, a.brand_desc as Brand_Type, a.product_code as Product_Id, a.descripcion as Product_Description, a.Temporada, a.Temporalidad, a.Descatalogado, format(a.precio_original,2,'de_DE') as First_Price, format(a.precio_actual,2,'de_DE') as Current_Price, format(sum(a.venta_uds_anio_actual),0,'de_DE') as Units_Sold_TY, format(sum(a.venta_uds_anio_anterior),0,'de_DE') as Units_Sold_LY, format(sum(a.venta_uds_anio_actual_lfl),0,'de_DE') as Units_Sold_LFL_TY, format(sum(a.venta_uds_anio_anterior_lfl),0,'de_DE') as Units_Sold_LFL_LY, format(sum(a.venta_neta_anio_Actual),2,'de_DE') as Net_Sales_TY, format(sum(a.venta_neta_anio_anterior),2,'de_DE') as Net_Sales_LY, format(sum(a.venta_neta_anio_Actual_lfl),2,'de_DE') as Net_Sales_LFL_TY, format(sum(a.venta_neta_anio_anterior_lfl),2,'de_DE') as Net_Sales_LFL_LY, format(sum(a.venta_bruta_anio_Actual),2,'de_DE') as Gross_Sales_TY, format(sum(a.venta_bruta_anio_anterior),2,'de_DE') as Gross_Sales_LY, format(sum(a.margen_anio_actual),2,'de_DE') as GPV_TY, format(sum(a.margen_anio_anterior),2,'de_DE') as GPV_LY, format(sum(a.margen_anio_actual_lfl),2,'de_DE') as GPV_LFL_TY, format(sum(a.margen_anio_anterior_lfl),2,'de_DE') as GPV_LFL_LY, format(sum(a.stock_anio_actual),2,'de_DE') as Total_Stk_Eur_TY, format(sum(a.stock_anio_anterior),2,'de_DE') as Total_Stk_Eur_LY, format(sum(a.stock_unidades_actual),0,'de_DE') as Total_Stk_Units_TY, format(sum(a.stock_unidades_anterior),0,'de_DE') as Total_Stk_Units_LY, format(sum(a.stock_anio_actual)-sum(stock_anio_actual_alm),2,'de_DE') as Branch_Stk_Eur_TY, format(sum(a.stock_anio_anterior)-sum(stock_anio_anterior_alm),2,'de_DE') as Branch_Stk_Eur_LY, format(sum(a.stock_unidades_actual)-sum(stock_uds_anio_actual_alm),0,'de_DE') as Branch_Stk_Units_TY, format(sum(a.stock_unidades_anterior)-sum(stock_uds_anio_anterior_alm),0,'de_DE') as Branch_Stk_Units_LY, format(sum(a.stock_anio_actual_alm),2,'de_DE') as WH_Stk_Eur_TY, format(sum(a.stock_anio_anterior_alm),2,'de_DE') as WH_Stk_Eur_LY, format(sum(a.stock_uds_anio_actual_alm),0,'de_DE') as WH_Stk_Units_TY, format(sum(a.stock_uds_anio_anterior_alm),0,'de_DE') as WH_Stk_Units_LY from PRODUCTO_stg a where Week_Number between SEMANA_INICIAL_MES_JD and SEMANA_FINAL group by Mercado, Fare_Code, udp_code,udp_name,area,(case when area='Textil' then 'APPAREL' when area='Calzado' then 'FWR' when area in ('Material técnico','Complementos') then 'ACC&&HEQ' when area in ('Promociones','Otros','na','Servicios') then 'OTHER' end),serie,(case when serie in ('UNISEX') and area in ('Textil','Calzado','Complementos','Material técnico') then 'UNISEX' when serie in ('UNISEX','na') and area in ('Promociones','Otros','Servicios','na') then 'OTHER' when serie in ('CABALLERO','XMAN') and area in ('Textil','Calzado','Complementos','Material técnico') then 'MAN' when serie in ('SEÑORA','LADY','JR. x SRA.') and area in ('Textil','Calzado','Complementos','Material técnico') then 'MAN' else 'JCI' end),seccion_a,seccion,familia,disenyo,marca,brand_desc,product_code,descripcion,Temporada, Temporalidad,Descatalogado,precio_original,precio_actual; end if; -- ---------------------------------------------------------------------------------------------------- /* TABLA PRODUCTO (VS2020) */ /* TABLA PRODUCTO (VS2021) -> Revision 2022/02/02 */ -- ---------------------------------------------------------------------------------------------------- -- Borramos la tabla PRODUCTO_VS2021_stg si nos encontramos en la semana 1 del año actual jd /* if SEMANA_INICIAL = 1 then truncate table PRODUCTO_VS2021_stg; else delete from PRODUCTO_VS2021_stg where Week_Number between SEMANA_INICIAL and SEMANA_FINAL; end if; */ -- Insertamos en la tabla PRODUCTO_VS2021_stg los nuevos datos calculados en ventas_stock1_especial /* insert into PRODUCTO_VS2021_stg (Week_Number, semana, Mercado, Fare_Code, udp_code, udp_name, area, serie, seccion_a, seccion, familia, disenyo, marca, brand_desc, product_code, descripcion, Temporada, Temporalidad, Descatalogado, precio_original, precio_actual, venta_uds_anio_actual, venta_uds_anio_anterior, venta_uds_anio_actual_lfl, venta_uds_anio_anterior_lfl, venta_neta_anio_Actual, venta_neta_anio_anterior, venta_neta_anio_Actual_lfl, venta_neta_anio_anterior_lfl, venta_bruta_anio_Actual, venta_bruta_anio_anterior, margen_anio_actual, margen_anio_anterior, margen_anio_actual_lfl, margen_anio_anterior_lfl, stock_anio_actual, stock_anio_anterior, stock_unidades_actual, stock_unidades_anterior, stock_anio_actual_alm, stock_anio_anterior_alm, stock_uds_anio_actual_alm, stock_uds_anio_anterior_alm) select cast(semana as unsigned) as Week_Number, semana, Mercado, Fare_Code, udp_code, udp_name, area, serie, seccion_a, seccion, familia, disenyo, marca, brand_desc, product_code, descripcion, Temporada, Temporalidad, Descatalogado, precio_original, precio_actual, sum(venta_uds_anio_actual) as venta_uds_anio_actual, sum(venta_uds_anio_anterior) as venta_uds_anio_anterior, sum(venta_uds_anio_actual_lfl) as venta_uds_anio_actual_lfl, sum(venta_uds_anio_anterior_lfl) as venta_uds_anio_anterior_lfl, sum(venta_neta_anio_Actual) as venta_neta_anio_Actual, sum(venta_neta_anio_anterior) as venta_neta_anio_anterior, sum(venta_neta_anio_Actual_lfl) as venta_neta_anio_Actual_lfl, sum(venta_neta_anio_anterior_lfl) as venta_neta_anio_anterior_lfl, sum(venta_bruta_anio_Actual) as venta_bruta_anio_Actual, sum(venta_bruta_anio_anterior) as venta_bruta_anio_anterior, sum(margen_anio_actual) as margen_anio_actual, sum(margen_anio_anterior) as margen_anio_anterior, sum(margen_anio_actual_lfl) as margen_anio_actual_lfl, sum(margen_anio_anterior_lfl) as margen_anio_anterior_lfl, sum(stock_anio_actual) as stock_anio_actual, sum(stock_anio_anterior) as stock_anio_anterior, sum(stock_unidades_actual) as stock_unidades_actual, sum(stock_unidades_anterior) as stock_unidades_anterior, sum(stock_anio_actual_alm) as stock_anio_actual_alm, sum(stock_anio_anterior_alm) as stock_anio_anterior_alm, sum(stock_uds_anio_actual_alm) as stock_uds_anio_actual_alm, sum(stock_uds_anio_anterior_alm) as stock_uds_anio_anterior_alm from ventas_stock1_especial group by cast(semana as unsigned), semana, Mercado,Fare_Code,udp_code,udp_name,area,serie,seccion_a,seccion,familia,disenyo,marca,brand_desc,product_code,descripcion,Temporada,Temporalidad,Descatalogado,precio_original,precio_actual; */ -- Generamos la tabla producto vs2021 semanal, para la extración semanal de los excels /* DROP table if exists PRODUCTO_VS2021_semanal; create table PRODUCTO_VS2021_semanal as select a.Mercado as Market, a.Fare_Code, a.udp_code as UDP_Code, a.udp_name as UDP_Name, a.area as Area, (case when a.area='Textil' then 'APPAREL' when a.area='Calzado' then 'FWR' when a.area in ('Material técnico','Complementos') then 'ACC&&HEQ' when a.area in ('Promociones','Otros','na','Servicios') then 'OTHER' end) as Area2, a.serie as Gender, (case when a.serie in ('UNISEX') and a.area in ('Textil','Calzado','Complementos','Material técnico') then 'UNISEX' when a.serie in ('UNISEX','na') and a.area in ('Promociones','Otros','Servicios','na') then 'OTHER' when a.serie in ('CABALLERO','XMAN') and a.area in ('Textil','Calzado','Complementos','Material técnico') then 'MAN' when a.serie in ('SEÑORA','LADY','JR. x SRA.') and a.area in ('Textil','Calzado','Complementos','Material técnico') then 'WOMAN' else 'JCI' end) as Gender2, a.seccion_a as Activity, a.seccion as Section, a.familia as Category, a.disenyo as Style, a.marca as Brand, a.brand_desc as Brand_Type, a.product_code as Product_Id, a.descripcion as Product_Description, a.Temporada, a.Temporalidad, a.Descatalogado, format(a.precio_original,2,'de_DE') as First_Price, format(a.precio_actual,2,'de_DE') as Current_Price, format(sum(a.venta_uds_anio_actual),0,'de_DE') as Units_Sold_TY, format(sum(a.venta_uds_anio_anterior),0,'de_DE') as Units_Sold_LY, format(sum(a.venta_uds_anio_actual_lfl),0,'de_DE') as Units_Sold_LFL_TY, format(sum(a.venta_uds_anio_anterior_lfl),0,'de_DE') as Units_Sold_LFL_LY, format(sum(a.venta_neta_anio_Actual),2,'de_DE') as Net_Sales_TY, format(sum(a.venta_neta_anio_anterior),2,'de_DE') as Net_Sales_LY, format(sum(a.venta_neta_anio_Actual_lfl),2,'de_DE') as Net_Sales_LFL_TY, format(sum(a.venta_neta_anio_anterior_lfl),2,'de_DE') as Net_Sales_LFL_LY, format(sum(a.venta_bruta_anio_Actual),2,'de_DE') as Gross_Sales_TY, format(sum(a.venta_bruta_anio_anterior),2,'de_DE') as Gross_Sales_LY, format(sum(a.margen_anio_actual),2,'de_DE') as GPV_TY, format(sum(a.margen_anio_anterior),2,'de_DE') as GPV_LY, format(sum(a.margen_anio_actual_lfl),2,'de_DE') as GPV_LFL_TY, format(sum(a.margen_anio_anterior_lfl),2,'de_DE') as GPV_LFL_LY, format(sum(a.stock_anio_actual),2,'de_DE') as Total_Stk_Eur_TY, format(sum(a.stock_anio_anterior),2,'de_DE') as Total_Stk_Eur_LY, format(sum(a.stock_unidades_actual),0,'de_DE') as Total_Stk_Units_TY, format(sum(a.stock_unidades_anterior),0,'de_DE') as Total_Stk_Units_LY, format(sum(a.stock_anio_actual)-sum(stock_anio_actual_alm),2,'de_DE') as Branch_Stk_Eur_TY, format(sum(a.stock_anio_anterior)-sum(stock_anio_anterior_alm),2,'de_DE') as Branch_Stk_Eur_LY, format(sum(a.stock_unidades_actual)-sum(stock_uds_anio_actual_alm),0,'de_DE') as Branch_Stk_Units_TY, format(sum(a.stock_unidades_anterior)-sum(stock_uds_anio_anterior_alm),0,'de_DE') as Branch_Stk_Units_LY, format(sum(a.stock_anio_actual_alm),2,'de_DE') as WH_Stk_Eur_TY, format(sum(a.stock_anio_anterior_alm),2,'de_DE') as WH_Stk_Eur_LY, format(sum(a.stock_uds_anio_actual_alm),0,'de_DE') as WH_Stk_Units_TY, format(sum(a.stock_uds_anio_anterior_alm),0,'de_DE') as WH_Stk_Units_LY from PRODUCTO_VS2021_stg a where Week_Number = SEMANA_FINAL group by Mercado, Fare_Code, udp_code,udp_name,area,(case when area='Textil' then 'APPAREL' when area='Calzado' then 'FWR' when area in ('Material técnico','Complementos') then 'ACC&&HEQ' when area in ('Promociones','Otros','na','Servicios') then 'OTHER' end),serie,(case when serie in ('UNISEX') and area in ('Textil','Calzado','Complementos','Material técnico') then 'UNISEX' when serie in ('UNISEX','na') and area in ('Promociones','Otros','Servicios','na') then 'OTHER' when serie in ('CABALLERO','XMAN') and area in ('Textil','Calzado','Complementos','Material técnico') then 'MAN' when serie in ('SEÑORA','LADY','JR. x SRA.') and area in ('Textil','Calzado','Complementos','Material técnico') then 'MAN' else 'JCI' end),seccion_a,seccion,familia,disenyo,marca,brand_desc,product_code,descripcion,Temporada, Temporalidad,Descatalogado,precio_original,precio_actual; */ -- ---------------------------------------------------------------------------------------------------- /* TABLA PRODUCTO APP */ -- ---------------------------------------------------------------------------------------------------- -- Borramos la tabla PRODUCTO_APP_stg si nos encontramos en la semana 1 del año actual jd if SEMANA_INICIAL = 1 then truncate table PRODUCTO_APP_stg; else delete from PRODUCTO_APP_stg where Week_Number between SEMANA_INICIAL and SEMANA_FINAL; end if; -- Insertamos en la tabla PRODUCTO_APP_stg los nuevos datos calculados en ventas_stock_app insert into PRODUCTO_APP_stg (Week_Number, semana, Mercado, Fare_Code, udp_code, udp_name, area, serie, seccion_a, seccion, familia, disenyo, marca, brand_desc, product_code, descripcion, Temporada, Temporalidad, Descatalogado, precio_original, precio_actual, venta_uds_anio_actual, venta_uds_anio_anterior, venta_uds_anio_actual_lfl, venta_uds_anio_anterior_lfl, venta_neta_anio_Actual, venta_neta_anio_anterior, venta_neta_anio_Actual_lfl, venta_neta_anio_anterior_lfl, venta_bruta_anio_Actual, venta_bruta_anio_anterior, margen_anio_actual, margen_anio_anterior, margen_anio_actual_lfl, margen_anio_anterior_lfl, stock_anio_actual, stock_anio_anterior, stock_unidades_actual, stock_unidades_anterior, stock_anio_actual_alm, stock_anio_anterior_alm, stock_uds_anio_actual_alm, stock_uds_anio_anterior_alm) select cast(semana as unsigned) as Week_Number, semana, Mercado, Fare_Code, udp_code, udp_name, area, serie, seccion_a, seccion, familia, disenyo, marca, brand_desc, product_code, descripcion, Temporada, Temporalidad, Descatalogado, precio_original, precio_actual, sum(venta_uds_anio_actual) as venta_uds_anio_actual, sum(venta_uds_anio_anterior) as venta_uds_anio_anterior, sum(venta_uds_anio_actual_lfl) as venta_uds_anio_actual_lfl, sum(venta_uds_anio_anterior_lfl) as venta_uds_anio_anterior_lfl, sum(venta_neta_anio_Actual) as venta_neta_anio_Actual, sum(venta_neta_anio_anterior) as venta_neta_anio_anterior, sum(venta_neta_anio_Actual_lfl) as venta_neta_anio_Actual_lfl, sum(venta_neta_anio_anterior_lfl) as venta_neta_anio_anterior_lfl, sum(venta_bruta_anio_Actual) as venta_bruta_anio_Actual, sum(venta_bruta_anio_anterior) as venta_bruta_anio_anterior, sum(margen_anio_actual) as margen_anio_actual, sum(margen_anio_anterior) as margen_anio_anterior, sum(margen_anio_actual_lfl) as margen_anio_actual_lfl, sum(margen_anio_anterior_lfl) as margen_anio_anterior_lfl, sum(stock_anio_actual) as stock_anio_actual, sum(stock_anio_anterior) as stock_anio_anterior, sum(stock_unidades_actual) as stock_unidades_actual, sum(stock_unidades_anterior) as stock_unidades_anterior, sum(stock_anio_actual_alm) as stock_anio_actual_alm, sum(stock_anio_anterior_alm) as stock_anio_anterior_alm, sum(stock_uds_anio_actual_alm) as stock_uds_anio_actual_alm, sum(stock_uds_anio_anterior_alm) as stock_uds_anio_anterior_alm from ventas_stock_app group by cast(semana as unsigned), semana, Mercado,Fare_Code,udp_code,udp_name,area,serie,seccion_a,seccion,familia,disenyo,marca,brand_desc,product_code,descripcion,Temporada,Temporalidad,Descatalogado,precio_original,precio_actual; -- Generamos la tabla producto APP semanal, para la extración semanal de los excels DROP table if exists PRODUCTO_APP_semanal; create table PRODUCTO_APP_semanal as select a.Mercado as Market, a.Fare_Code, a.udp_code as UDP_Code, a.udp_name as UDP_Name, a.area as Area, (case when a.area='Textil' then 'APPAREL' when a.area='Calzado' then 'FWR' when a.area in ('Material técnico','Complementos') then 'ACC&&HEQ' when a.area in ('Promociones','Otros','na','Servicios') then 'OTHER' end) as Area2, a.serie as Gender, (case when a.serie in ('UNISEX') and a.area in ('Textil','Calzado','Complementos','Material técnico') then 'UNISEX' when a.serie in ('UNISEX','na') and a.area in ('Promociones','Otros','Servicios','na') then 'OTHER' when a.serie in ('CABALLERO','XMAN') and a.area in ('Textil','Calzado','Complementos','Material técnico') then 'MAN' when a.serie in ('SEÑORA','LADY','JR. x SRA.') and a.area in ('Textil','Calzado','Complementos','Material técnico') then 'WOMAN' else 'JCI' end) as Gender2, a.seccion_a as Activity, a.seccion as Section, a.familia as Category, a.disenyo as Style, a.marca as Brand, a.brand_desc as Brand_Type, a.product_code as Product_Id, a.descripcion as Product_Description, a.Temporada, a.Temporalidad, a.Descatalogado, format(a.precio_original,2,'de_DE') as First_Price, format(a.precio_actual,2,'de_DE') as Current_Price, format(sum(a.venta_uds_anio_actual),0,'de_DE') as Units_Sold_TY, format(sum(a.venta_uds_anio_anterior),0,'de_DE') as Units_Sold_LY, format(sum(a.venta_uds_anio_actual_lfl),0,'de_DE') as Units_Sold_LFL_TY, format(sum(a.venta_uds_anio_anterior_lfl),0,'de_DE') as Units_Sold_LFL_LY, format(sum(a.venta_neta_anio_Actual),2,'de_DE') as Net_Sales_TY, format(sum(a.venta_neta_anio_anterior),2,'de_DE') as Net_Sales_LY, format(sum(a.venta_neta_anio_Actual_lfl),2,'de_DE') as Net_Sales_LFL_TY, format(sum(a.venta_neta_anio_anterior_lfl),2,'de_DE') as Net_Sales_LFL_LY, format(sum(a.venta_bruta_anio_Actual),2,'de_DE') as Gross_Sales_TY, format(sum(a.venta_bruta_anio_anterior),2,'de_DE') as Gross_Sales_LY, format(sum(a.margen_anio_actual),2,'de_DE') as GPV_TY, format(sum(a.margen_anio_anterior),2,'de_DE') as GPV_LY, format(sum(a.margen_anio_actual_lfl),2,'de_DE') as GPV_LFL_TY, format(sum(a.margen_anio_anterior_lfl),2,'de_DE') as GPV_LFL_LY, format(sum(a.stock_anio_actual),2,'de_DE') as Total_Stk_Eur_TY, format(sum(a.stock_anio_anterior),2,'de_DE') as Total_Stk_Eur_LY, format(sum(a.stock_unidades_actual),0,'de_DE') as Total_Stk_Units_TY, format(sum(a.stock_unidades_anterior),0,'de_DE') as Total_Stk_Units_LY, format(sum(a.stock_anio_actual)-sum(stock_anio_actual_alm),2,'de_DE') as Branch_Stk_Eur_TY, format(sum(a.stock_anio_anterior)-sum(stock_anio_anterior_alm),2,'de_DE') as Branch_Stk_Eur_LY, format(sum(a.stock_unidades_actual)-sum(stock_uds_anio_actual_alm),0,'de_DE') as Branch_Stk_Units_TY, format(sum(a.stock_unidades_anterior)-sum(stock_uds_anio_anterior_alm),0,'de_DE') as Branch_Stk_Units_LY, format(sum(a.stock_anio_actual_alm),2,'de_DE') as WH_Stk_Eur_TY, format(sum(a.stock_anio_anterior_alm),2,'de_DE') as WH_Stk_Eur_LY, format(sum(a.stock_uds_anio_actual_alm),0,'de_DE') as WH_Stk_Units_TY, format(sum(a.stock_uds_anio_anterior_alm),0,'de_DE') as WH_Stk_Units_LY from PRODUCTO_APP_stg a where Week_Number = SEMANA_FINAL group by Mercado, Fare_Code, udp_code,udp_name,area,(case when area='Textil' then 'APPAREL' when area='Calzado' then 'FWR' when area in ('Material técnico','Complementos') then 'ACC&&HEQ' when area in ('Promociones','Otros','na','Servicios') then 'OTHER' end),serie,(case when serie in ('UNISEX') and area in ('Textil','Calzado','Complementos','Material técnico') then 'UNISEX' when serie in ('UNISEX','na') and area in ('Promociones','Otros','Servicios','na') then 'OTHER' when serie in ('CABALLERO','XMAN') and area in ('Textil','Calzado','Complementos','Material técnico') then 'MAN' when serie in ('SEÑORA','LADY','JR. x SRA.') and area in ('Textil','Calzado','Complementos','Material técnico') then 'MAN' else 'JCI' end),seccion_a,seccion,familia,disenyo,marca,brand_desc,product_code,descripcion,Temporada, Temporalidad,Descatalogado,precio_original,precio_actual; -- Generamos la tabla producto APP acumulado, para la extración acumulada de los excels DROP table if exists PRODUCTO_APP_acumulado; create table PRODUCTO_APP_acumulado as select a.Mercado as Market, a.Fare_Code, a.udp_code as UDP_Code, a.udp_name as UDP_Name, a.area as Area, (case when a.area='Textil' then 'APPAREL' when a.area='Calzado' then 'FWR' when a.area in ('Material técnico','Complementos') then 'ACC&&HEQ' when a.area in ('Promociones','Otros','na','Servicios') then 'OTHER' end) as Area2, a.serie as Gender, (case when a.serie in ('UNISEX') and a.area in ('Textil','Calzado','Complementos','Material técnico') then 'UNISEX' when a.serie in ('UNISEX','na') and a.area in ('Promociones','Otros','Servicios','na') then 'OTHER' when a.serie in ('CABALLERO','XMAN') and a.area in ('Textil','Calzado','Complementos','Material técnico') then 'MAN' when a.serie in ('SEÑORA','LADY','JR. x SRA.') and a.area in ('Textil','Calzado','Complementos','Material técnico') then 'WOMAN' else 'JCI' end) as Gender2, a.seccion_a as Activity, a.seccion as Section, a.familia as Category, a.disenyo as Style, a.marca as Brand, a.brand_desc as Brand_Type, a.product_code as Product_Id, a.descripcion as Product_Description, a.Temporada, a.Temporalidad, a.Descatalogado, format(a.precio_original,2,'de_DE') as First_Price, format(a.precio_actual,2,'de_DE') as Current_Price, format(sum(a.venta_uds_anio_actual),0,'de_DE') as Units_Sold_TY, format(sum(a.venta_uds_anio_anterior),0,'de_DE') as Units_Sold_LY, format(sum(a.venta_uds_anio_actual_lfl),0,'de_DE') as Units_Sold_LFL_TY, format(sum(a.venta_uds_anio_anterior_lfl),0,'de_DE') as Units_Sold_LFL_LY, format(sum(a.venta_neta_anio_Actual),2,'de_DE') as Net_Sales_TY, format(sum(a.venta_neta_anio_anterior),2,'de_DE') as Net_Sales_LY, format(sum(a.venta_neta_anio_Actual_lfl),2,'de_DE') as Net_Sales_LFL_TY, format(sum(a.venta_neta_anio_anterior_lfl),2,'de_DE') as Net_Sales_LFL_LY, format(sum(a.venta_bruta_anio_Actual),2,'de_DE') as Gross_Sales_TY, format(sum(a.venta_bruta_anio_anterior),2,'de_DE') as Gross_Sales_LY, format(sum(a.margen_anio_actual),2,'de_DE') as GPV_TY, format(sum(a.margen_anio_anterior),2,'de_DE') as GPV_LY, format(sum(a.margen_anio_actual_lfl),2,'de_DE') as GPV_LFL_TY, format(sum(a.margen_anio_anterior_lfl),2,'de_DE') as GPV_LFL_LY, format(sum(a.stock_anio_actual),2,'de_DE') as Total_Stk_Eur_TY, format(sum(a.stock_anio_anterior),2,'de_DE') as Total_Stk_Eur_LY, format(sum(a.stock_unidades_actual),0,'de_DE') as Total_Stk_Units_TY, format(sum(a.stock_unidades_anterior),0,'de_DE') as Total_Stk_Units_LY, format(sum(a.stock_anio_actual)-sum(stock_anio_actual_alm),2,'de_DE') as Branch_Stk_Eur_TY, format(sum(a.stock_anio_anterior)-sum(stock_anio_anterior_alm),2,'de_DE') as Branch_Stk_Eur_LY, format(sum(a.stock_unidades_actual)-sum(stock_uds_anio_actual_alm),0,'de_DE') as Branch_Stk_Units_TY, format(sum(a.stock_unidades_anterior)-sum(stock_uds_anio_anterior_alm),0,'de_DE') as Branch_Stk_Units_LY, format(sum(a.stock_anio_actual_alm),2,'de_DE') as WH_Stk_Eur_TY, format(sum(a.stock_anio_anterior_alm),2,'de_DE') as WH_Stk_Eur_LY, format(sum(a.stock_uds_anio_actual_alm),0,'de_DE') as WH_Stk_Units_TY, format(sum(a.stock_uds_anio_anterior_alm),0,'de_DE') as WH_Stk_Units_LY from PRODUCTO_APP_stg a where Week_Number between 1 and SEMANA_FINAL group by Mercado, Fare_Code, udp_code,udp_name,area,(case when area='Textil' then 'APPAREL' when area='Calzado' then 'FWR' when area in ('Material técnico','Complementos') then 'ACC&&HEQ' when area in ('Promociones','Otros','na','Servicios') then 'OTHER' end),serie,(case when serie in ('UNISEX') and area in ('Textil','Calzado','Complementos','Material técnico') then 'UNISEX' when serie in ('UNISEX','na') and area in ('Promociones','Otros','Servicios','na') then 'OTHER' when serie in ('CABALLERO','XMAN') and area in ('Textil','Calzado','Complementos','Material técnico') then 'MAN' when serie in ('SEÑORA','LADY','JR. x SRA.') and area in ('Textil','Calzado','Complementos','Material técnico') then 'MAN' else 'JCI' end),seccion_a,seccion,familia,disenyo,marca,brand_desc,product_code,descripcion,Temporada, Temporalidad,Descatalogado,precio_original,precio_actual; -- Creamos la tabla producto APP para la temporalidad mensual, solo si es la última semana_jd del mes_jd actual if FIN_MES_JD then DROP table if exists PRODUCTO_APP_mensual; create table PRODUCTO_APP_mensual as select a.Mercado as Market, a.Fare_Code, a.udp_code as UDP_Code, a.udp_name as UDP_Name, a.area as Area, (case when a.area='Textil' then 'APPAREL' when a.area='Calzado' then 'FWR' when a.area in ('Material técnico','Complementos') then 'ACC&&HEQ' when a.area in ('Promociones','Otros','na','Servicios') then 'OTHER' end) as Area2, a.serie as Gender, (case when a.serie in ('UNISEX') and a.area in ('Textil','Calzado','Complementos','Material técnico') then 'UNISEX' when a.serie in ('UNISEX','na') and a.area in ('Promociones','Otros','Servicios','na') then 'OTHER' when a.serie in ('CABALLERO','XMAN') and a.area in ('Textil','Calzado','Complementos','Material técnico') then 'MAN' when a.serie in ('SEÑORA','LADY','JR. x SRA.') and a.area in ('Textil','Calzado','Complementos','Material técnico') then 'WOMAN' else 'JCI' end) as Gender2, a.seccion_a as Activity, a.seccion as Section, a.familia as Category, a.disenyo as Style, a.marca as Brand, a.brand_desc as Brand_Type, a.product_code as Product_Id, a.descripcion as Product_Description, a.Temporada, a.Temporalidad, a.Descatalogado, format(a.precio_original,2,'de_DE') as First_Price, format(a.precio_actual,2,'de_DE') as Current_Price, format(sum(a.venta_uds_anio_actual),0,'de_DE') as Units_Sold_TY, format(sum(a.venta_uds_anio_anterior),0,'de_DE') as Units_Sold_LY, format(sum(a.venta_uds_anio_actual_lfl),0,'de_DE') as Units_Sold_LFL_TY, format(sum(a.venta_uds_anio_anterior_lfl),0,'de_DE') as Units_Sold_LFL_LY, format(sum(a.venta_neta_anio_Actual),2,'de_DE') as Net_Sales_TY, format(sum(a.venta_neta_anio_anterior),2,'de_DE') as Net_Sales_LY, format(sum(a.venta_neta_anio_Actual_lfl),2,'de_DE') as Net_Sales_LFL_TY, format(sum(a.venta_neta_anio_anterior_lfl),2,'de_DE') as Net_Sales_LFL_LY, format(sum(a.venta_bruta_anio_Actual),2,'de_DE') as Gross_Sales_TY, format(sum(a.venta_bruta_anio_anterior),2,'de_DE') as Gross_Sales_LY, format(sum(a.margen_anio_actual),2,'de_DE') as GPV_TY, format(sum(a.margen_anio_anterior),2,'de_DE') as GPV_LY, format(sum(a.margen_anio_actual_lfl),2,'de_DE') as GPV_LFL_TY, format(sum(a.margen_anio_anterior_lfl),2,'de_DE') as GPV_LFL_LY, format(sum(a.stock_anio_actual),2,'de_DE') as Total_Stk_Eur_TY, format(sum(a.stock_anio_anterior),2,'de_DE') as Total_Stk_Eur_LY, format(sum(a.stock_unidades_actual),0,'de_DE') as Total_Stk_Units_TY, format(sum(a.stock_unidades_anterior),0,'de_DE') as Total_Stk_Units_LY, format(sum(a.stock_anio_actual)-sum(stock_anio_actual_alm),2,'de_DE') as Branch_Stk_Eur_TY, format(sum(a.stock_anio_anterior)-sum(stock_anio_anterior_alm),2,'de_DE') as Branch_Stk_Eur_LY, format(sum(a.stock_unidades_actual)-sum(stock_uds_anio_actual_alm),0,'de_DE') as Branch_Stk_Units_TY, format(sum(a.stock_unidades_anterior)-sum(stock_uds_anio_anterior_alm),0,'de_DE') as Branch_Stk_Units_LY, format(sum(a.stock_anio_actual_alm),2,'de_DE') as WH_Stk_Eur_TY, format(sum(a.stock_anio_anterior_alm),2,'de_DE') as WH_Stk_Eur_LY, format(sum(a.stock_uds_anio_actual_alm),0,'de_DE') as WH_Stk_Units_TY, format(sum(a.stock_uds_anio_anterior_alm),0,'de_DE') as WH_Stk_Units_LY from PRODUCTO_APP_stg a where Week_Number between SEMANA_INICIAL_MES_JD and SEMANA_FINAL group by Mercado, Fare_Code, udp_code,udp_name,area,(case when area='Textil' then 'APPAREL' when area='Calzado' then 'FWR' when area in ('Material técnico','Complementos') then 'ACC&&HEQ' when area in ('Promociones','Otros','na','Servicios') then 'OTHER' end),serie,(case when serie in ('UNISEX') and area in ('Textil','Calzado','Complementos','Material técnico') then 'UNISEX' when serie in ('UNISEX','na') and area in ('Promociones','Otros','Servicios','na') then 'OTHER' when serie in ('CABALLERO','XMAN') and area in ('Textil','Calzado','Complementos','Material técnico') then 'MAN' when serie in ('SEÑORA','LADY','JR. x SRA.') and area in ('Textil','Calzado','Complementos','Material técnico') then 'MAN' else 'JCI' end),seccion_a,seccion,familia,disenyo,marca,brand_desc,product_code,descripcion,Temporada, Temporalidad,Descatalogado,precio_original,precio_actual; end if; -- ---------------------------------------------------------------------------------------------------- /* TABLA PRODUCTO WEB */ -- ---------------------------------------------------------------------------------------------------- -- Borramos la tabla PRODUCTO_WEB_stg si nos encontramos en la semana 1 del año actual jd if SEMANA_INICIAL = 1 then truncate table PRODUCTO_WEB_stg; else delete from PRODUCTO_WEB_stg where Week_Number between SEMANA_INICIAL and SEMANA_FINAL; end if; -- Insertamos en la tabla PRODUCTO_WEB_stg los nuevos datos calculados en ventas_stock1 insert into PRODUCTO_WEB_stg (Week_Number, semana, Mercado, Fare_Code, udp_code, udp_name, area, serie, seccion_a, seccion, familia, disenyo, marca, brand_desc, product_code, descripcion, Temporada, Temporalidad, Descatalogado, precio_original, precio_actual, venta_uds_anio_actual, venta_uds_anio_anterior, venta_uds_anio_actual_lfl, venta_uds_anio_anterior_lfl, venta_neta_anio_Actual, venta_neta_anio_anterior, venta_neta_anio_Actual_lfl, venta_neta_anio_anterior_lfl, venta_bruta_anio_Actual, venta_bruta_anio_anterior, margen_anio_actual, margen_anio_anterior, margen_anio_actual_lfl, margen_anio_anterior_lfl, stock_anio_actual, stock_anio_anterior, stock_unidades_actual, stock_unidades_anterior, stock_anio_actual_alm, stock_anio_anterior_alm, stock_uds_anio_actual_alm, stock_uds_anio_anterior_alm) select cast(semana as unsigned) as Week_Number, semana, Mercado, Fare_Code, udp_code, udp_name, area, serie, seccion_a, seccion, familia, disenyo, marca, brand_desc, product_code, descripcion, Temporada, Temporalidad, Descatalogado, precio_original, precio_actual, sum(venta_uds_anio_actual) as venta_uds_anio_actual, sum(venta_uds_anio_anterior) as venta_uds_anio_anterior, sum(venta_uds_anio_actual_lfl) as venta_uds_anio_actual_lfl, sum(venta_uds_anio_anterior_lfl) as venta_uds_anio_anterior_lfl, sum(venta_neta_anio_Actual) as venta_neta_anio_Actual, sum(venta_neta_anio_anterior) as venta_neta_anio_anterior, sum(venta_neta_anio_Actual_lfl) as venta_neta_anio_Actual_lfl, sum(venta_neta_anio_anterior_lfl) as venta_neta_anio_anterior_lfl, sum(venta_bruta_anio_Actual) as venta_bruta_anio_Actual, sum(venta_bruta_anio_anterior) as venta_bruta_anio_anterior, sum(margen_anio_actual) as margen_anio_actual, sum(margen_anio_anterior) as margen_anio_anterior, sum(margen_anio_actual_lfl) as margen_anio_actual_lfl, sum(margen_anio_anterior_lfl) as margen_anio_anterior_lfl, sum(stock_anio_actual) as stock_anio_actual, sum(stock_anio_anterior) as stock_anio_anterior, sum(stock_unidades_actual) as stock_unidades_actual, sum(stock_unidades_anterior) as stock_unidades_anterior, sum(stock_anio_actual_alm) as stock_anio_actual_alm, sum(stock_anio_anterior_alm) as stock_anio_anterior_alm, sum(stock_uds_anio_actual_alm) as stock_uds_anio_actual_alm, sum(stock_uds_anio_anterior_alm) as stock_uds_anio_anterior_alm from ventas_stock1 where branch_no in ('911','9PT','1722','1723','9FR','9IT') group by cast(semana as unsigned), semana, Mercado,Fare_Code,udp_code,udp_name,area,serie,seccion_a,seccion,familia,disenyo,marca,brand_desc,product_code,descripcion,Temporada,Temporalidad,Descatalogado,precio_original,precio_actual; -- Generamos la tabla producto WEB semanal, para la extración semanal de los excels DROP table if exists PRODUCTO_WEB_semanal; create table PRODUCTO_WEB_semanal as select a.Mercado as Market, a.Fare_Code, a.udp_code as UDP_Code, a.udp_name as UDP_Name, a.area as Area, (case when a.area='Textil' then 'APPAREL' when a.area='Calzado' then 'FWR' when a.area in ('Material técnico','Complementos') then 'ACC&&HEQ' when a.area in ('Promociones','Otros','na','Servicios') then 'OTHER' end) as Area2, a.serie as Gender, (case when a.serie in ('UNISEX') and a.area in ('Textil','Calzado','Complementos','Material técnico') then 'UNISEX' when a.serie in ('UNISEX','na') and a.area in ('Promociones','Otros','Servicios','na') then 'OTHER' when a.serie in ('CABALLERO','XMAN') and a.area in ('Textil','Calzado','Complementos','Material técnico') then 'MAN' when a.serie in ('SEÑORA','LADY','JR. x SRA.') and a.area in ('Textil','Calzado','Complementos','Material técnico') then 'WOMAN' else 'JCI' end) as Gender2, a.seccion_a as Activity, a.seccion as Section, a.familia as Category, a.disenyo as Style, a.marca as Brand, a.brand_desc as Brand_Type, a.product_code as Product_Id, a.descripcion as Product_Description, a.Temporada, a.Temporalidad, a.Descatalogado, format(a.precio_original,2,'de_DE') as First_Price, format(a.precio_actual,2,'de_DE') as Current_Price, format(sum(a.venta_uds_anio_actual),0,'de_DE') as Units_Sold_TY, format(sum(a.venta_uds_anio_anterior),0,'de_DE') as Units_Sold_LY, format(sum(a.venta_uds_anio_actual_lfl),0,'de_DE') as Units_Sold_LFL_TY, format(sum(a.venta_uds_anio_anterior_lfl),0,'de_DE') as Units_Sold_LFL_LY, format(sum(a.venta_neta_anio_Actual),2,'de_DE') as Net_Sales_TY, format(sum(a.venta_neta_anio_anterior),2,'de_DE') as Net_Sales_LY, format(sum(a.venta_neta_anio_Actual_lfl),2,'de_DE') as Net_Sales_LFL_TY, format(sum(a.venta_neta_anio_anterior_lfl),2,'de_DE') as Net_Sales_LFL_LY, format(sum(a.venta_bruta_anio_Actual),2,'de_DE') as Gross_Sales_TY, format(sum(a.venta_bruta_anio_anterior),2,'de_DE') as Gross_Sales_LY, format(sum(a.margen_anio_actual),2,'de_DE') as GPV_TY, format(sum(a.margen_anio_anterior),2,'de_DE') as GPV_LY, format(sum(a.margen_anio_actual_lfl),2,'de_DE') as GPV_LFL_TY, format(sum(a.margen_anio_anterior_lfl),2,'de_DE') as GPV_LFL_LY, format(sum(a.stock_anio_actual),2,'de_DE') as Total_Stk_Eur_TY, format(sum(a.stock_anio_anterior),2,'de_DE') as Total_Stk_Eur_LY, format(sum(a.stock_unidades_actual),0,'de_DE') as Total_Stk_Units_TY, format(sum(a.stock_unidades_anterior),0,'de_DE') as Total_Stk_Units_LY, format(sum(a.stock_anio_actual)-sum(stock_anio_actual_alm),2,'de_DE') as Branch_Stk_Eur_TY, format(sum(a.stock_anio_anterior)-sum(stock_anio_anterior_alm),2,'de_DE') as Branch_Stk_Eur_LY, format(sum(a.stock_unidades_actual)-sum(stock_uds_anio_actual_alm),0,'de_DE') as Branch_Stk_Units_TY, format(sum(a.stock_unidades_anterior)-sum(stock_uds_anio_anterior_alm),0,'de_DE') as Branch_Stk_Units_LY, format(sum(a.stock_anio_actual_alm),2,'de_DE') as WH_Stk_Eur_TY, format(sum(a.stock_anio_anterior_alm),2,'de_DE') as WH_Stk_Eur_LY, format(sum(a.stock_uds_anio_actual_alm),0,'de_DE') as WH_Stk_Units_TY, format(sum(a.stock_uds_anio_anterior_alm),0,'de_DE') as WH_Stk_Units_LY from PRODUCTO_WEB_stg a where Week_Number = SEMANA_FINAL group by Mercado, Fare_Code, udp_code, udp_name, area, (case when area='Textil' then 'APPAREL' when area='Calzado' then 'FWR' when area in ('Material técnico','Complementos') then 'ACC&&HEQ' when area in ('Promociones','Otros','na','Servicios') then 'OTHER' end),serie,(case when serie in ('UNISEX') and area in ('Textil','Calzado','Complementos','Material técnico') then 'UNISEX' when serie in ('UNISEX','na') and area in ('Promociones','Otros','Servicios','na') then 'OTHER' when serie in ('CABALLERO','XMAN') and area in ('Textil','Calzado','Complementos','Material técnico') then 'MAN' when serie in ('SEÑORA','LADY','JR. x SRA.') and area in ('Textil','Calzado','Complementos','Material técnico') then 'MAN' else 'JCI' end),seccion_a,seccion,familia,disenyo,marca,brand_desc,product_code,descripcion,Temporada, Temporalidad,Descatalogado,precio_original,precio_actual; -- Generamos la tabla producto WEB acumulado, para la extración acumulada de los excels DROP table if exists PRODUCTO_WEB_acumulado; create table PRODUCTO_WEB_acumulado as select a.Mercado as Market, a.Fare_Code, a.udp_code as UDP_Code, a.udp_name as UDP_Name, a.area as Area, (case when a.area='Textil' then 'APPAREL' when a.area='Calzado' then 'FWR' when a.area in ('Material técnico','Complementos') then 'ACC&&HEQ' when a.area in ('Promociones','Otros','na','Servicios') then 'OTHER' end) as Area2, a.serie as Gender, (case when a.serie in ('UNISEX') and a.area in ('Textil','Calzado','Complementos','Material técnico') then 'UNISEX' when a.serie in ('UNISEX','na') and a.area in ('Promociones','Otros','Servicios','na') then 'OTHER' when a.serie in ('CABALLERO','XMAN') and a.area in ('Textil','Calzado','Complementos','Material técnico') then 'MAN' when a.serie in ('SEÑORA','LADY','JR. x SRA.') and a.area in ('Textil','Calzado','Complementos','Material técnico') then 'WOMAN' else 'JCI' end) as Gender2, a.seccion_a as Activity, a.seccion as Section, a.familia as Category, a.disenyo as Style, a.marca as Brand, a.brand_desc as Brand_Type, a.product_code as Product_Id, a.descripcion as Product_Description, a.Temporada, a.Temporalidad, a.Descatalogado, format(a.precio_original,2,'de_DE') as First_Price, format(a.precio_actual,2,'de_DE') as Current_Price, format(sum(a.venta_uds_anio_actual),0,'de_DE') as Units_Sold_TY, format(sum(a.venta_uds_anio_anterior),0,'de_DE') as Units_Sold_LY, format(sum(a.venta_uds_anio_actual_lfl),0,'de_DE') as Units_Sold_LFL_TY, format(sum(a.venta_uds_anio_anterior_lfl),0,'de_DE') as Units_Sold_LFL_LY, format(sum(a.venta_neta_anio_Actual),2,'de_DE') as Net_Sales_TY, format(sum(a.venta_neta_anio_anterior),2,'de_DE') as Net_Sales_LY, format(sum(a.venta_neta_anio_Actual_lfl),2,'de_DE') as Net_Sales_LFL_TY, format(sum(a.venta_neta_anio_anterior_lfl),2,'de_DE') as Net_Sales_LFL_LY, format(sum(a.venta_bruta_anio_Actual),2,'de_DE') as Gross_Sales_TY, format(sum(a.venta_bruta_anio_anterior),2,'de_DE') as Gross_Sales_LY, format(sum(a.margen_anio_actual),2,'de_DE') as GPV_TY, format(sum(a.margen_anio_anterior),2,'de_DE') as GPV_LY, format(sum(a.margen_anio_actual_lfl),2,'de_DE') as GPV_LFL_TY, format(sum(a.margen_anio_anterior_lfl),2,'de_DE') as GPV_LFL_LY, format(sum(a.stock_anio_actual),2,'de_DE') as Total_Stk_Eur_TY, format(sum(a.stock_anio_anterior),2,'de_DE') as Total_Stk_Eur_LY, format(sum(a.stock_unidades_actual),0,'de_DE') as Total_Stk_Units_TY, format(sum(a.stock_unidades_anterior),0,'de_DE') as Total_Stk_Units_LY, format(sum(a.stock_anio_actual)-sum(stock_anio_actual_alm),2,'de_DE') as Branch_Stk_Eur_TY, format(sum(a.stock_anio_anterior)-sum(stock_anio_anterior_alm),2,'de_DE') as Branch_Stk_Eur_LY, format(sum(a.stock_unidades_actual)-sum(stock_uds_anio_actual_alm),0,'de_DE') as Branch_Stk_Units_TY, format(sum(a.stock_unidades_anterior)-sum(stock_uds_anio_anterior_alm),0,'de_DE') as Branch_Stk_Units_LY, format(sum(a.stock_anio_actual_alm),2,'de_DE') as WH_Stk_Eur_TY, format(sum(a.stock_anio_anterior_alm),2,'de_DE') as WH_Stk_Eur_LY, format(sum(a.stock_uds_anio_actual_alm),0,'de_DE') as WH_Stk_Units_TY, format(sum(a.stock_uds_anio_anterior_alm),0,'de_DE') as WH_Stk_Units_LY from PRODUCTO_WEB_stg a where Week_Number between 1 and SEMANA_FINAL group by Mercado, Fare_Code, udp_code,udp_name,area,(case when area='Textil' then 'APPAREL' when area='Calzado' then 'FWR' when area in ('Material técnico','Complementos') then 'ACC&&HEQ' when area in ('Promociones','Otros','na','Servicios') then 'OTHER' end),serie,(case when serie in ('UNISEX') and area in ('Textil','Calzado','Complementos','Material técnico') then 'UNISEX' when serie in ('UNISEX','na') and area in ('Promociones','Otros','Servicios','na') then 'OTHER' when serie in ('CABALLERO','XMAN') and area in ('Textil','Calzado','Complementos','Material técnico') then 'MAN' when serie in ('SEÑORA','LADY','JR. x SRA.') and area in ('Textil','Calzado','Complementos','Material técnico') then 'MAN' else 'JCI' end),seccion_a,seccion,familia,disenyo,marca,brand_desc,product_code,descripcion,Temporada, Temporalidad,Descatalogado,precio_original,precio_actual; -- Creamos la tabla producto WEB para la temporalidad mensual, solo si es la última semana_jd del mes_jd actual if FIN_MES_JD then DROP table if exists PRODUCTO_WEB_mensual; create table PRODUCTO_WEB_mensual as select a.Mercado as Market, a.Fare_Code, a.udp_code as UDP_Code, a.udp_name as UDP_Name, a.area as Area, (case when a.area='Textil' then 'APPAREL' when a.area='Calzado' then 'FWR' when a.area in ('Material técnico','Complementos') then 'ACC&&HEQ' when a.area in ('Promociones','Otros','na','Servicios') then 'OTHER' end) as Area2, a.serie as Gender, (case when a.serie in ('UNISEX') and a.area in ('Textil','Calzado','Complementos','Material técnico') then 'UNISEX' when a.serie in ('UNISEX','na') and a.area in ('Promociones','Otros','Servicios','na') then 'OTHER' when a.serie in ('CABALLERO','XMAN') and a.area in ('Textil','Calzado','Complementos','Material técnico') then 'MAN' when a.serie in ('SEÑORA','LADY','JR. x SRA.') and a.area in ('Textil','Calzado','Complementos','Material técnico') then 'WOMAN' else 'JCI' end) as Gender2, a.seccion_a as Activity, a.seccion as Section, a.familia as Category, a.disenyo as Style, a.marca as Brand, a.brand_desc as Brand_Type, a.product_code as Product_Id, a.descripcion as Product_Description, a.Temporada, a.Temporalidad, a.Descatalogado, format(a.precio_original,2,'de_DE') as First_Price, format(a.precio_actual,2,'de_DE') as Current_Price, format(sum(a.venta_uds_anio_actual),0,'de_DE') as Units_Sold_TY, format(sum(a.venta_uds_anio_anterior),0,'de_DE') as Units_Sold_LY, format(sum(a.venta_uds_anio_actual_lfl),0,'de_DE') as Units_Sold_LFL_TY, format(sum(a.venta_uds_anio_anterior_lfl),0,'de_DE') as Units_Sold_LFL_LY, format(sum(a.venta_neta_anio_Actual),2,'de_DE') as Net_Sales_TY, format(sum(a.venta_neta_anio_anterior),2,'de_DE') as Net_Sales_LY, format(sum(a.venta_neta_anio_Actual_lfl),2,'de_DE') as Net_Sales_LFL_TY, format(sum(a.venta_neta_anio_anterior_lfl),2,'de_DE') as Net_Sales_LFL_LY, format(sum(a.venta_bruta_anio_Actual),2,'de_DE') as Gross_Sales_TY, format(sum(a.venta_bruta_anio_anterior),2,'de_DE') as Gross_Sales_LY, format(sum(a.margen_anio_actual),2,'de_DE') as GPV_TY, format(sum(a.margen_anio_anterior),2,'de_DE') as GPV_LY, format(sum(a.margen_anio_actual_lfl),2,'de_DE') as GPV_LFL_TY, format(sum(a.margen_anio_anterior_lfl),2,'de_DE') as GPV_LFL_LY, format(sum(a.stock_anio_actual),2,'de_DE') as Total_Stk_Eur_TY, format(sum(a.stock_anio_anterior),2,'de_DE') as Total_Stk_Eur_LY, format(sum(a.stock_unidades_actual),0,'de_DE') as Total_Stk_Units_TY, format(sum(a.stock_unidades_anterior),0,'de_DE') as Total_Stk_Units_LY, format(sum(a.stock_anio_actual)-sum(stock_anio_actual_alm),2,'de_DE') as Branch_Stk_Eur_TY, format(sum(a.stock_anio_anterior)-sum(stock_anio_anterior_alm),2,'de_DE') as Branch_Stk_Eur_LY, format(sum(a.stock_unidades_actual)-sum(stock_uds_anio_actual_alm),0,'de_DE') as Branch_Stk_Units_TY, format(sum(a.stock_unidades_anterior)-sum(stock_uds_anio_anterior_alm),0,'de_DE') as Branch_Stk_Units_LY, format(sum(a.stock_anio_actual_alm),2,'de_DE') as WH_Stk_Eur_TY, format(sum(a.stock_anio_anterior_alm),2,'de_DE') as WH_Stk_Eur_LY, format(sum(a.stock_uds_anio_actual_alm),0,'de_DE') as WH_Stk_Units_TY, format(sum(a.stock_uds_anio_anterior_alm),0,'de_DE') as WH_Stk_Units_LY from PRODUCTO_WEB_stg a where Week_Number between SEMANA_INICIAL_MES_JD and SEMANA_FINAL group by Mercado, Fare_Code, udp_code,udp_name,area,(case when area='Textil' then 'APPAREL' when area='Calzado' then 'FWR' when area in ('Material técnico','Complementos') then 'ACC&&HEQ' when area in ('Promociones','Otros','na','Servicios') then 'OTHER' end),serie,(case when serie in ('UNISEX') and area in ('Textil','Calzado','Complementos','Material técnico') then 'UNISEX' when serie in ('UNISEX','na') and area in ('Promociones','Otros','Servicios','na') then 'OTHER' when serie in ('CABALLERO','XMAN') and area in ('Textil','Calzado','Complementos','Material técnico') then 'MAN' when serie in ('SEÑORA','LADY','JR. x SRA.') and area in ('Textil','Calzado','Complementos','Material técnico') then 'MAN' else 'JCI' end),seccion_a,seccion,familia,disenyo,marca,brand_desc,product_code,descripcion,Temporada, Temporalidad,Descatalogado,precio_original,precio_actual; end if; -- ---------------------------------------------------------------------------------------------------- /* TABLA EVOLUCIÓN SEMANAL PRODUCTO */ -- ---------------------------------------------------------------------------------------------------- /* Sólo se mostrará en el Report cuando estemos calculando los datos acumulados del año */ -- Borramos la tabla EVOL_SEMANAL_PROD si nos encontramos en la semana 1 del año actual jd if SEMANA_INICIAL = 1 AND SEMANA_FINAL = 1 then insert into evol_semanal_prod_historico (Anio_jd, Market, Fare_Code, Area, Area2, Gender, Gender2, Activity, `Section`, Brand, Brand_type, semana, Units_Sold_TY, Units_Sold_LY, Units_Sold_LFL_TY, Units_Sold_LFL_LY, Net_Sales_TY, Net_Sales_LY, Net_Sales_LFL_TY, Net_Sales_LFL_LY, Gross_Sales_TY, Gross_Sales_LY, GPV_TY, GPV_LY, GPV_LFL_TY, GPV_LFL_LY, Total_Stk_Eur_TY, Total_Stk_Eur_LY, Total_Stk_Units_TY, Total_Stk_Units_LY, Branch_Stk_Eur_TY, Branch_Stk_Eur_LY, Branch_Stk_Units_TY, Branch_Stk_Units_LY, WH_Stk_Eur_TY, WH_Stk_Eur_LY, WH_Stk_Units_TY, WH_Stk_Units_LY) select ANIO_ACTUAL-1 as Anio_jd, Market, Fare_Code, Area, Area2, Gender, Gender2, Activity, `section`, Brand, Brand_type, semana, Units_Sold_TY, Units_Sold_LY, Units_Sold_LFL_TY, Units_Sold_LFL_LY, Net_Sales_TY, Net_Sales_LY, Net_Sales_LFL_TY, Net_Sales_LFL_LY, Gross_Sales_TY, Gross_Sales_LY, GPV_TY, GPV_LY, GPV_LFL_TY, GPV_LFL_LY, Total_Stk_Eur_TY, Total_Stk_Eur_LY, Total_Stk_Units_TY, Total_Stk_Units_LY, Branch_Stk_Eur_TY, Branch_Stk_Eur_LY, Branch_Stk_Units_TY, Branch_Stk_Units_LY, WH_Stk_Eur_TY, WH_Stk_Eur_LY, WH_Stk_Units_TY, WH_Stk_Units_LY from EVOL_SEMANAL_PROD; truncate table EVOL_SEMANAL_PROD; else delete from EVOL_SEMANAL_PROD where cast(semana as unsigned) between SEMANA_INICIAL and SEMANA_FINAL; end if; -- Insertamos en la tabla EVOL_SEMANAL_PROD los nuevos datos calculados en ventas_stock1 insert into EVOL_SEMANAL_PROD (Market, Fare_Code, Area, Area2, Gender, Gender2, Activity, `Section`, Brand, Brand_type, semana, Units_Sold_TY, Units_Sold_LY, Units_Sold_LFL_TY, Units_Sold_LFL_LY, Net_Sales_TY, Net_Sales_LY, Net_Sales_LFL_TY, Net_Sales_LFL_LY, Gross_Sales_TY, Gross_Sales_LY, GPV_TY, GPV_LY, GPV_LFL_TY, GPV_LFL_LY, Total_Stk_Eur_TY, Total_Stk_Eur_LY, Total_Stk_Units_TY, Total_Stk_Units_LY, Branch_Stk_Eur_TY, Branch_Stk_Eur_LY, Branch_Stk_Units_TY, Branch_Stk_Units_LY, WH_Stk_Eur_TY, WH_Stk_Eur_LY, WH_Stk_Units_TY, WH_Stk_Units_LY) select a.Mercado as Market, a.Fare_Code, a.area as Area, (case when a.area='Textil' then 'APPAREL' when a.area='Calzado' then 'FWR' when a.area in ('Material técnico','Complementos') then 'ACC&&HEQ' when a.area in ('Promociones','Otros','na','Servicios') then 'OTHER' end) as Area2, a.serie as Gender, (case when a.serie in ('UNISEX') and area in ('Textil','Calzado','Complementos','Material técnico') then 'UNISEX' when a.serie in ('UNISEX','na') and area in ('Promociones','Otros','Servicios','na') then 'OTHER' when a.serie in ('CABALLERO','XMAN') and area in ('Textil','Calzado','Complementos','Material técnico') then 'MAN' when a.serie in ('SEÑORA','LADY','JR. x SRA.') and area in ('Textil','Calzado','Complementos','Material técnico') then 'WOMAN' else 'JCI' end) as Gender2, a.seccion_a as Activity, a.seccion as Section, a.marca as Brand, a.brand_desc as Brand_type, a.semana, format(sum(a.venta_uds_anio_actual),0,'de_DE') as Units_Sold_TY, format(sum(a.venta_uds_anio_anterior),0,'de_DE') as Units_Sold_LY, format(sum(a.venta_uds_anio_actual_lfl),0,'de_DE') as Units_Sold_LFL_TY, format(sum(a.venta_uds_anio_anterior_lfl),0,'de_DE') as Units_Sold_LFL_LY, format(sum(a.venta_neta_anio_Actual),2,'de_DE') as Net_Sales_TY, format(sum(a.venta_neta_anio_anterior),2,'de_DE') as Net_Sales_LY, format(sum(a.venta_neta_anio_Actual_lfl),2,'de_DE') as Net_Sales_LFL_TY, format(sum(a.venta_neta_anio_anterior_lfl),2,'de_DE') as Net_Sales_LFL_LY, format(sum(a.venta_bruta_anio_Actual),2,'de_DE') as Gross_Sales_TY, format(sum(a.venta_bruta_anio_anterior),2,'de_DE') as Gross_Sales_LY, format(sum(a.margen_anio_actual),2,'de_DE') as GPV_TY, format(sum(a.margen_anio_anterior),2,'de_DE') as GPV_LY, format(sum(a.margen_anio_actual_lfl),2,'de_DE') as GPV_LFL_TY, format(sum(a.margen_anio_anterior_lfl),2,'de_DE') as GPV_LFL_LY, format(sum(a.stock_anio_actual),2,'de_DE') as Total_Stk_Eur_TY, format(sum(a.stock_anio_anterior),2,'de_DE') as Total_Stk_Eur_LY, format(sum(a.stock_unidades_actual),0,'de_DE') as Total_Stk_Units_TY, format(sum(a.stock_unidades_anterior),0,'de_DE') as Total_Stk_Units_LY, format(sum(a.stock_anio_actual)-sum(stock_anio_actual_alm),2,'de_DE') as Branch_Stk_Eur_TY, format(sum(a.stock_anio_anterior)-sum(stock_anio_anterior_alm),2,'de_DE') as Branch_Stk_Eur_LY, format(sum(a.stock_unidades_actual)-sum(stock_uds_anio_actual_alm),0,'de_DE') as Branch_Stk_Units_TY, format(sum(a.stock_unidades_anterior)-sum(stock_uds_anio_anterior_alm),0,'de_DE') as Branch_Stk_Units_LY, format(sum(a.stock_anio_actual_alm),2,'de_DE') as WH_Stk_Eur_TY, format(sum(a.stock_anio_anterior_alm),2,'de_DE') as WH_Stk_Eur_LY, format(sum(a.stock_uds_anio_actual_alm),0,'de_DE') as WH_Stk_Units_TY, format(sum(a.stock_uds_anio_anterior_alm),0,'de_DE') as WH_Stk_Units_LY from (select Mercado, Fare_Code, area, serie, seccion_a, seccion, marca, brand_desc, semana, sum(venta_uds_anio_actual) as venta_uds_anio_actual, sum(venta_uds_anio_anterior) as venta_uds_anio_anterior, sum(venta_uds_anio_actual_lfl) as venta_uds_anio_actual_lfl, sum(venta_uds_anio_anterior_lfl) as venta_uds_anio_anterior_lfl, sum(venta_neta_anio_Actual) as venta_neta_anio_Actual, sum(venta_neta_anio_anterior) as venta_neta_anio_anterior, sum(venta_neta_anio_Actual_lfl) as venta_neta_anio_Actual_lfl, sum(venta_neta_anio_anterior_lfl) as venta_neta_anio_anterior_lfl, sum(venta_bruta_anio_Actual) as venta_bruta_anio_Actual, sum(venta_bruta_anio_anterior) as venta_bruta_anio_anterior, sum(margen_anio_actual) as margen_anio_actual, sum(margen_anio_anterior) as margen_anio_anterior, sum(margen_anio_actual_lfl) as margen_anio_actual_lfl, sum(margen_anio_anterior_lfl) as margen_anio_anterior_lfl, sum(stock_anio_actual) as stock_anio_actual, sum(stock_anio_anterior) as stock_anio_anterior, sum(stock_unidades_actual) as stock_unidades_actual, sum(stock_unidades_anterior) as stock_unidades_anterior, sum(stock_anio_actual_alm) as stock_anio_actual_alm, sum(stock_anio_anterior_alm) as stock_anio_anterior_alm, sum(stock_uds_anio_actual_alm) as stock_uds_anio_actual_alm, sum(stock_uds_anio_anterior_alm) as stock_uds_anio_anterior_alm from ventas_stock1 group by Mercado,Fare_Code,area,serie,seccion_a,seccion,marca,brand_desc,semana )a group by a.Mercado,a.Fare_Code,a.area,(case when a.area='Textil' then 'APPAREL' when a.area='Calzado' then 'FWR' when a.area in ('Material técnico','Complementos') then 'ACC&&HEQ' when a.area in ('Promociones','Otros','na','Servicios') then 'OTHER' end),a.serie,(case when a.serie in ('UNISEX') and area in ('Textil','Calzado','Complementos','Material técnico') then 'UNISEX' when a.serie in ('UNISEX','na') and area in ('Promociones','Otros','Servicios','na') then 'OTHER' when a.serie in ('CABALLERO','XMAN') and area in ('Textil','Calzado','Complementos','Material técnico') then 'MAN' when a.serie in ('SEÑORA','LADY','JR. x SRA.') and area in ('Textil','Calzado','Complementos','Material técnico') then 'WOMAN' else 'JCI' end),a.seccion_a,a.seccion,a.marca,a.brand_desc,a.semana; -- ---------------------------------------------------------------------------------------------------- /* TABLA TIENDA */ -- ---------------------------------------------------------------------------------------------------- drop table if exists tienda_pre; create table tienda_pre as select distinct a.branch_no, a.mercado, a.nombre_tienda, a.EsTienda, a.subzona, a.delegado, a.zona, a.territorial from (select distinct branch_no, mercado, nombre_tienda, EsTienda, subzona, delegado, zona, territorial from ventas_stock1) a; drop table if exists tienda_pre1; create table tienda_pre1 as select distinct a.semana_jd as semana, weekday(a.fecha) as dia, b.branch_no, b.mercado, b.nombre_tienda, b.EsTienda, b.subzona, b.delegado, b.zona, b.territorial from md_dbg.calendario_detalle_jd_v2 a,tienda_pre b where a.fecha>=FECHA_INICIAL and a.fecha<=FECHA_FINAL; drop table if exists tienda_pre2; create table tienda_pre2 as select a.branch_no, a.mercado, a.nombre_tienda, a.EsTienda, a.subzona, a.delegado, a.zona, a.territorial, a.semana, a.dia, sum(venta_uds_anio_actual) as vta_uds_Aact, sum(venta_uds_anio_anterior) as vta_uds_Aant, sum(venta_neta_anio_Actual) as vta_neta_Aact, sum(venta_neta_anio_anterior) as vta_neta_Aant, sum(venta_neta_anio_actual_lfl) as vta_neta_Aact_LFL, sum(venta_neta_anio_anterior_lfl) as vta_neta_Aant_LFL, sum(margen_anio_actual) as GPV_Aact, sum(margen_anio_anterior) as GPV_Aant, sum(margen_anio_actual_lfl) as GPV_Aact_LFL, sum(margen_anio_anterior_lfl) as GPV_Aant_LFL, sum(stock_unidades_actual) as stock_uds_Aact, sum(stock_unidades_anterior) as stock_uds_Aant, sum(stock_anio_actual) as stock_val_Aact, sum(stock_anio_anterior) as stock_val_Aant from (select branch_no, mercado, nombre_tienda, EsTienda, subzona, delegado, zona, territorial, semana, dia, sum(venta_uds_anio_actual) as venta_uds_anio_actual, sum(venta_uds_anio_anterior) as venta_uds_anio_anterior, sum(venta_neta_anio_Actual) as venta_neta_anio_Actual, sum(venta_neta_anio_anterior) as venta_neta_anio_anterior, sum(venta_neta_anio_actual_lfl) as venta_neta_anio_actual_lfl, sum(venta_neta_anio_anterior_lfl) as venta_neta_anio_anterior_lfl, sum(margen_anio_actual) as margen_anio_actual, sum(margen_anio_anterior) as margen_anio_anterior, sum(margen_anio_actual_lfl) as margen_anio_actual_lfl, sum(margen_anio_anterior_lfl) as margen_anio_anterior_lfl, sum(stock_unidades_actual) as stock_unidades_actual, sum(stock_unidades_anterior) as stock_unidades_anterior, sum(stock_anio_actual) as stock_anio_actual, sum(stock_anio_anterior) as stock_anio_anterior from ventas_stock1 group by branch_no,mercado,nombre_tienda,EsTienda,subzona,delegado,zona,territorial,semana,dia ) a group by a.branch_no,a.mercado,a.nombre_tienda,a.EsTienda,a.subzona,a.delegado,a.zona,a.territorial,a.semana,a.dia; alter table tienda_pre1 add index (branch_no,dia,semana); alter table tienda_pre2 add index (branch_no,dia,semana); -- LBI-190--(YYYY_mensual_semana_WW_a_WW) -- hojas "STORE" y "STORE APP" -- LBI-190--(YYYY_semana_WW.xls) -- hojas "STORE","STORE APP" y "STORE NO SERV" -- LBI-190--(YYYY_acumulado_semana_01_a_WW_store.xlsx) -- hojas "STORE" if SEMANA_INICIAL = 1 AND SEMANA_FINAL = 1 then insert into TIENDA_historico (Anio_jd, Market, Branch_Id, Branch_Name, Is_Store, Subzona, Delegation_Manager, Region, Territorial_Manager, Week, Weekday, Units_Sold_TY, Units_Sold_LY, Net_Sales_TY, Net_Sales_LY, Net_Sales_TY_LFL, Net_Sales_LY_LFL, Target_TY, GPV_Target_TY, Store_Target_TY, GPV_Store_Target_TY, GPV_TY, GPV_LY, GPV_TY_LFL, GPV_LY_LFL, Stk_Units_TY, Stk_Units_LY, Stk_Euros_TY, Stk_Euros_LY) select ANIO_ACTUAL-1 as Anio_jd, Market, Branch_Id, Branch_Name, Is_Store, Subzona, Delegation_Manager, Region, Territorial_Manager, Week, Weekday, Units_Sold_TY, Units_Sold_LY, Net_Sales_TY, Net_Sales_LY, Net_Sales_TY_LFL, Net_Sales_LY_LFL, Target_TY, GPV_Target_TY, Store_Target_TY, GPV_Store_Target_TY, GPV_TY, GPV_LY, GPV_TY_LFL, GPV_LY_LFL, Stk_Units_TY, Stk_Units_LY, Stk_Euros_TY, Stk_Euros_LY from TIENDA; truncate table TIENDA; else delete from TIENDA where cast(Week as unsigned) between SEMANA_INICIAL and SEMANA_FINAL; end if; -- Insertamos en la tabla TIENDA los nuevos datos calculados en tienda_pre1 y tienda_pre2 insert into TIENDA (Market, Branch_Id, Branch_Name, Is_Store, Subzona, Delegation_Manager, Region, Territorial_Manager, Week, Weekday, Units_Sold_TY, Units_Sold_LY, Net_Sales_TY, Net_Sales_LY, Net_Sales_TY_LFL, Net_Sales_LY_LFL, Target_TY, GPV_Target_TY, Store_Target_TY, GPV_Store_Target_TY, GPV_TY, GPV_LY, GPV_TY_LFL, GPV_LY_LFL, Stk_Units_TY, Stk_Units_LY, Stk_Euros_TY, Stk_Euros_LY) select d.mercado as Market, d.branch_no as Branch_Id, d.nombre_tienda as Branch_Name, d.EsTienda as Is_Store, d.subzona as Subzona, d.delegado as Delegation_Manager, d.zona as Region, d.territorial as Territorial_Manager, d.semana as Week, (CASE when d.dia=5 then 'Sat' when d.dia=4 then 'Fri' when d.dia=3 then 'Thu' when d.dia=2 then 'Wed' when d.dia=1 then 'Tue' when d.dia=0 then 'Mon' when d.dia=6 then 'Sun' end) as Weekday, format(coalesce(a.vta_uds_Aact,0),0,'de_DE') as Units_Sold_TY, format(coalesce(a.vta_uds_Aant,0),0,'de_DE') as Units_Sold_LY, format(coalesce(a.vta_neta_Aact,0),2,'de_DE') as Net_Sales_TY, format(coalesce(a.vta_neta_Aant,0),2,'de_DE') as Net_Sales_LY, format(coalesce(a.vta_neta_Aact_LFL,0),2,'de_DE') as Net_Sales_TY_LFL, format(coalesce(a.vta_neta_Aant_LFL,0),2,'de_DE') as Net_Sales_LY_LFL, /*b.Target_TY AS Target_TY, b.GPV_Target_TY AS GPV_Target_TY,*/ format(coalesce(b.Target_TY,0),2,'de_DE') as Target_TY, format(coalesce(b.GPV_Target_TY,0),2,'de_DE') as GPV_Target_TY, format(coalesce(b.Store_Target_TY,0),2,'de_DE') as Store_Target_TY, format(coalesce(b.GPV_Store_Target_TY,0),2,'de_DE') as GPV_Store_Target_TY, format(coalesce(a.GPV_Aact,0),2,'de_DE') as GPV_TY, format(coalesce(a.GPV_Aant,0),2,'de_DE') as GPV_LY, format(coalesce(a.GPV_Aact_LFL,0),2,'de_DE') as GPV_TY_LFL, format(coalesce(a.GPV_Aant_LFL,0),2,'de_DE') as GPV_LY_LFL, format(coalesce(a.stock_uds_Aact,0),0,'de_DE') as Stk_Units_TY, format(coalesce(a.stock_uds_Aant,0),0,'de_DE') as Stk_Units_LY, format(coalesce(a.stock_val_Aact,0),2,'de_DE') as Stk_Euros_TY, format(coalesce(a.stock_val_Aant,0),2,'de_DE') as Stk_Euros_LY from tienda_pre1 d left join tienda_pre2 a on d.branch_no=a.branch_no and d.dia=a.dia and d.semana=a.semana left join objetivo_aact b on d.branch_no=b.branch_no and d.dia=b.dia and d.semana=b.semana left join objetivo_aant c on d.branch_no=c.branch_no and d.dia=c.dia and d.semana=c.semana; -- ---------------------------------------------------------------------------------------------------- /* TABLA TIENDA SIN SERVICIOS */ -- ---------------------------------------------------------------------------------------------------- alter table ventas_stock1 add index idx_area (area); drop table if exists tienda_pre2_no_serv; create table tienda_pre2_no_serv as select a.branch_no, a.mercado, a.nombre_tienda, a.EsTienda, a.subzona, a.delegado, a.zona, a.territorial, a.semana, a.dia, sum(venta_uds_anio_actual) as vta_uds_Aact, sum(venta_uds_anio_anterior) as vta_uds_Aant, sum(venta_neta_anio_Actual) as vta_neta_Aact, sum(venta_neta_anio_anterior) as vta_neta_Aant, sum(venta_neta_anio_actual_lfl) as vta_neta_Aact_LFL, sum(venta_neta_anio_anterior_lfl) as vta_neta_Aant_LFL, sum(margen_anio_actual) as GPV_Aact, sum(margen_anio_anterior) as GPV_Aant, sum(margen_anio_actual_lfl) as GPV_Aact_LFL, sum(margen_anio_anterior_lfl) as GPV_Aant_LFL, sum(stock_unidades_actual) as stock_uds_Aact, sum(stock_unidades_anterior) as stock_uds_Aant, sum(stock_anio_actual) as stock_val_Aact, sum(stock_anio_anterior) as stock_val_Aant from (select branch_no, mercado, nombre_tienda, EsTienda, subzona, delegado, zona, territorial, semana, dia, sum(venta_uds_anio_actual) as venta_uds_anio_actual, sum(venta_uds_anio_anterior) as venta_uds_anio_anterior, sum(venta_neta_anio_Actual) as venta_neta_anio_Actual, sum(venta_neta_anio_anterior) as venta_neta_anio_anterior, sum(venta_neta_anio_actual_lfl) as venta_neta_anio_actual_lfl, sum(venta_neta_anio_anterior_lfl) as venta_neta_anio_anterior_lfl, sum(margen_anio_actual) as margen_anio_actual, sum(margen_anio_anterior) as margen_anio_anterior, sum(margen_anio_actual_lfl) as margen_anio_actual_lfl, sum(margen_anio_anterior_lfl) as margen_anio_anterior_lfl, sum(stock_unidades_actual) as stock_unidades_actual, sum(stock_unidades_anterior) as stock_unidades_anterior, sum(stock_anio_actual) as stock_anio_actual, sum(stock_anio_anterior) as stock_anio_anterior from ventas_stock1 WHERE area <> 'Servicios' group by branch_no,mercado,nombre_tienda,EsTienda,subzona,delegado,zona,territorial,semana,dia ) a group by a.branch_no,a.mercado,a.nombre_tienda,a.EsTienda,a.subzona,a.delegado,a.zona,a.territorial,a.semana,a.dia; /*alter table ventas_stock1 drop index idx_area; alter table ventas_stock2 drop index idx_area;*/ alter table tienda_pre2_no_serv add index (branch_no,dia,semana); -- LBI-190--(YYYY_semana_WW.xls) -- hojas "STORE","STORE APP" y "STORE NO SERV" -- LBI-190--(YYYY_acumulado_semana_01_a_WW_store_no_serv.xlsx) -- hojass "STORE NO SERV" -- Borramos la tabla TIENDA_NO_SERV si nos encontramos en la semana 1 del año actual jd if SEMANA_INICIAL = 1 then truncate table TIENDA_NO_SERV; else delete from TIENDA_NO_SERV where cast(Week as unsigned) between SEMANA_INICIAL and SEMANA_FINAL; end if; -- Insertamos en la tabla TIENDA_NO_SERV los nuevos datos calculados en tienda_pre1 y tienda_pre2_no_serv insert into TIENDA_NO_SERV (Market, Branch_Id, Branch_Name, Is_Store, Subzona, Delegation_Manager, Region, Territorial_Manager, Week, Weekday, Units_Sold_TY, Units_Sold_LY, Net_Sales_TY, Net_Sales_LY, Net_Sales_TY_LFL, Net_Sales_LY_LFL, Target_TY, GPV_Target_TY, Store_Target_TY, GPV_Store_Target_TY, GPV_TY, GPV_LY, GPV_TY_LFL, GPV_LY_LFL, Stk_Units_TY, Stk_Units_LY, Stk_Euros_TY, Stk_Euros_LY) select d.mercado as Market, d.branch_no as Branch_Id, d.nombre_tienda as Branch_Name, d.EsTienda as Is_Store, d.subzona as Subzona, d.delegado as Delegation_Manager, d.zona as Region, d.territorial as Territorial_Manager, d.semana as Week, (CASE when d.dia=5 then 'Sat' when d.dia=4 then 'Fri' when d.dia=3 then 'Thu' when d.dia=2 then 'Wed' when d.dia=1 then 'Tue' when d.dia=0 then 'Mon' when d.dia=6 then 'Sun' end) as Weekday, format(coalesce(a.vta_uds_Aact,0),0,'de_DE') as Units_Sold_TY, format(coalesce(a.vta_uds_Aant,0),0,'de_DE') as Units_Sold_LY, format(coalesce(a.vta_neta_Aact,0),2,'de_DE') as Net_Sales_TY, format(coalesce(a.vta_neta_Aant,0),2,'de_DE') as Net_Sales_LY, format(coalesce(a.vta_neta_Aact_LFL,0),2,'de_DE') as Net_Sales_TY_LFL, format(coalesce(a.vta_neta_Aant_LFL,0),2,'de_DE') as Net_Sales_LY_LFL, /*b.Target_TY AS Target_TY, b.GPV_Target_TY AS GPV_Target_TY,*/ format(coalesce(b.Target_TY,0),2,'de_DE') as Target_TY, format(coalesce(b.GPV_Target_TY,0),2,'de_DE') as GPV_Target_TY, format(coalesce(b.Store_Target_TY,0),2,'de_DE') as Store_Target_TY, format(coalesce(b.GPV_Store_Target_TY,0),2,'de_DE') as GPV_Store_Target_TY, format(coalesce(a.GPV_Aact,0),2,'de_DE') as GPV_TY, format(coalesce(a.GPV_Aant,0),2,'de_DE') as GPV_LY, format(coalesce(a.GPV_Aact_LFL,0),2,'de_DE') as GPV_TY_LFL, format(coalesce(a.GPV_Aant_LFL,0),2,'de_DE') as GPV_LY_LFL, format(coalesce(a.stock_uds_Aact,0),0,'de_DE') as Stk_Units_TY, format(coalesce(a.stock_uds_Aant,0),0,'de_DE') as Stk_Units_LY, format(coalesce(a.stock_val_Aact,0),2,'de_DE') as Stk_Euros_TY, format(coalesce(a.stock_val_Aant,0),2,'de_DE') as Stk_Euros_LY from tienda_pre1 d left join tienda_pre2_no_serv a on d.branch_no=a.branch_no and d.dia=a.dia and d.semana=a.semana left join objetivo_aact b on d.branch_no=b.branch_no and d.dia=b.dia and d.semana=b.semana left join objetivo_aant c on d.branch_no=c.branch_no and d.dia=c.dia and d.semana=c.semana; drop table if exists tienda_pre; drop table if exists tienda_pre1; drop table if exists tienda_pre2; -- ---------------------------------------------------------------------------------------------------- /* TABLA TIENDA APP */ -- ---------------------------------------------------------------------------------------------------- drop table if exists tienda_pre_app; create table tienda_pre_app as select distinct branch_no, mercado, nombre_tienda, EsTienda, subzona, delegado, zona, territorial from ventas_stock_App; drop table if exists tienda_pre1_app; create table tienda_pre1_app as select distinct a.semana_jd as semana, weekday(a.fecha) as dia, b.branch_no, b.mercado, b.nombre_tienda, b.EsTienda, b.subzona, b.delegado, b.zona, b.territorial from md_dbg.calendario_detalle_jd_v2 a,tienda_pre_app b where a.fecha>=FECHA_INICIAL and a.fecha<=FECHA_FINAL; drop table if exists tienda_pre2_app; create table tienda_pre2_app as select branch_no, mercado, nombre_tienda, EsTienda, subzona, delegado, zona, territorial, semana, dia, sum(venta_uds_anio_actual) as vta_uds_Aact, sum(venta_uds_anio_anterior) as vta_uds_Aant, sum(venta_neta_anio_Actual) as vta_neta_Aact, sum(venta_neta_anio_anterior) as vta_neta_Aant, sum(venta_neta_anio_actual_lfl) as vta_neta_Aact_LFL, sum(venta_neta_anio_anterior_lfl) as vta_neta_Aant_LFL, sum(margen_anio_actual) as GPV_Aact, sum(margen_anio_anterior) as GPV_Aant, sum(margen_anio_actual_lfl) as GPV_Aact_LFL, sum(margen_anio_anterior_lfl) as GPV_Aant_LFL, sum(stock_unidades_actual) as stock_uds_Aact, sum(stock_unidades_anterior) as stock_uds_Aant, sum(stock_anio_actual) as stock_val_Aact, sum(stock_anio_anterior) as stock_val_Aant from ventas_stock_app group by branch_no,mercado,nombre_tienda,EsTienda,subzona,delegado,zona,territorial,semana,dia; alter table tienda_pre1_app add index (branch_no,dia,semana); alter table tienda_pre2_app add index (branch_no,dia,semana); -- LBI-190--(YYYY_mensual_semana_WW_a_WW) -- hojas "STORE" y "STORE APP" -- LBI-190--(YYYY_semana_WW.xls) -- hojas "STORE","STORE APP" y "STORE NO SERV" -- LBI-190--(YYYY_acumulado_semana_01_a_WW_store_app.xlsx) -- hojas "STORE APP" -- Borramos la tabla TIENDA_APP si nos encontramos en la semana 1 del año actual jd if SEMANA_INICIAL = 1 then truncate table TIENDA_APP; else delete from TIENDA_APP where cast(Week as unsigned) between SEMANA_INICIAL and SEMANA_FINAL; end if; -- Insertamos en la tabla TIENDA_APP los nuevos datos calculados en tienda_pre1_app y tienda_pre2_app insert into TIENDA_APP (Market, Branch_Id, Branch_Name, Is_Store, Subzona, Delegation_Manager, Region, Territorial_Manager, Week, Weekday, Units_Sold_TY, Units_Sold_LY, Net_Sales_TY, Net_Sales_LY, Net_Sales_TY_LFL, Net_Sales_LY_LFL, Target_TY, GPV_Target_TY, Store_Target_TY, GPV_Store_Target_TY, GPV_TY, GPV_LY, GPV_TY_LFL, GPV_LY_LFL, Stk_Units_TY, Stk_Units_LY, Stk_Euros_TY, Stk_Euros_LY) select d.mercado as Market, d.branch_no as Branch_Id, d.nombre_tienda as Branch_Name, d.EsTienda as Is_Store, d.subzona as Subzona, d.delegado as Delegation_Manager, d.zona as Region, d.territorial as Territorial_Manager, d.semana as Week, (CASE when d.dia=5 then 'Sat' when d.dia=4 then 'Fri' when d.dia=3 then 'Thu' when d.dia=2 then 'Wed' when d.dia=1 then 'Tue' when d.dia=0 then 'Mon' when d.dia=6 then 'Sun' end) as Weekday, format(coalesce(a.vta_uds_Aact,0),0,'de_DE') as Units_Sold_TY, format(coalesce(a.vta_uds_Aant,0),0,'de_DE') as Units_Sold_LY, format(coalesce(a.vta_neta_Aact,0),2,'de_DE') as Net_Sales_TY, format(coalesce(a.vta_neta_Aant,0),2,'de_DE') as Net_Sales_LY, format(coalesce(a.vta_neta_Aact_LFL,0),2,'de_DE') as Net_Sales_TY_LFL, format(coalesce(a.vta_neta_Aant_LFL,0),2,'de_DE') as Net_Sales_LY_LFL, /*b.Target_TY AS Target_TY, b.GPV_Target_TY AS GPV_Target_TY,*/ format(coalesce(b.Target_TY,0),2,'de_DE') as Target_TY, format(coalesce(b.GPV_Target_TY,0),2,'de_DE') as GPV_Target_TY, format(coalesce(b.Store_Target_TY,0),2,'de_DE') as Store_Target_TY, format(coalesce(b.GPV_Store_Target_TY,0),2,'de_DE') as GPV_Store_Target_TY, format(coalesce(a.GPV_Aact,0),2,'de_DE') as GPV_TY, format(coalesce(a.GPV_Aant,0),2,'de_DE') as GPV_LY, format(coalesce(a.GPV_Aact_LFL,0),2,'de_DE') as GPV_TY_LFL, format(coalesce(a.GPV_Aant_LFL,0),2,'de_DE') as GPV_LY_LFL, format(coalesce(a.stock_uds_Aact,0),0,'de_DE') as Stk_Units_TY, format(coalesce(a.stock_uds_Aant,0),0,'de_DE') as Stk_Units_LY, format(coalesce(a.stock_val_Aact,0),2,'de_DE') as Stk_Euros_TY, format(coalesce(a.stock_val_Aant,0),2,'de_DE') as Stk_Euros_LY from tienda_pre1_app d left join tienda_pre2_app a on d.branch_no=a.branch_no and d.dia=a.dia and d.semana=a.semana left join objetivo_aact b on d.branch_no=b.branch_no and d.dia=b.dia and d.semana=b.semana left join objetivo_aant c on d.branch_no=c.branch_no and d.dia=c.dia and d.semana=c.semana having Units_Sold_TY<>'0' or Units_Sold_LY<>'0'; drop table if exists tienda_pre_app; drop table if exists tienda_pre1_app; drop table if exists tienda_pre2_app; /* drop table if exists objetivo_aact; drop table if exists objetivo_aant; */ -- ---------------------------------------------------------------------------------------------------- /* TABLA TICKET */ -- ---------------------------------------------------------------------------------------------------- drop table if exists ticket_pre_a; create table ticket_pre_a as select distinct a.mercado, a.branch_no, a.nombre_tienda, a.tipo_tienda, a.subzona, a.delegado, a.zona, a.territorial from (select distinct a.mercado, a.branch_no, a.nombre_tienda, a.tipo_tienda, a.subzona, a.delegado, a.zona, a.territorial from ventas_stock1 a where EsTienda=1) a; alter table ticket_pre_a add index (branch_no); drop table if exists ticket_pre_a1; create table ticket_pre_a1 as select distinct a.semana_jd as semana, a.mes_jd as mes, weekday(a.fecha) as dia, b.mercado, b.branch_no, b.nombre_tienda, b.tipo_tienda, b.subzona, b.delegado, b.zona, b.territorial from md_dbg.calendario_detalle_jd_v2 a,ticket_pre_a b where a.fecha>=FECHA_INICIAL and a.fecha<=FECHA_FINAL; alter table ticket_pre_a1 add index idx(branch_no,dia,semana); -- ---------------------------------------------------------------------------------------------------- /* TABLA TICKET */ -- ---------------------------------------------------------------------------------------------------- drop table if exists ticket_pre; create table ticket_pre as select a.mercado, a.branch_no, a.nombre_tienda, a.tipo_tienda, a.subzona, a.delegado, a.zona, a.territorial, a.dia, a.semana, a.mes, sum(venta_uds_anio_actual) as vta_uds_Aact, sum(venta_uds_anio_anterior) as vta_uds_Aant, sum(venta_neta_anio_actual) as vta_neta_Aact, sum(venta_neta_anio_anterior) as vta_neta_Aant, sum(venta_neta_anio_actual_lfl) as vta_neta_Aact_lfl, sum(venta_neta_anio_anterior_lfl) as vta_neta_Aant_lfl, sum(venta_bruta_anio_actual) as vta_bruta_Aact, sum(venta_bruta_anio_anterior) as vta_bruta_Aant, sum(venta_bruta_anio_actual_lfl) as vta_bruta_Aact_lfl, sum(venta_bruta_anio_anterior_lfl) as vta_bruta_Aant_lfl, sum(margen_anio_actual) as mg_aact, sum(margen_anio_anterior) as mg_aant, sum(margen_anio_actual_lfl) as mg_aact_lfl, sum(margen_anio_anterior_lfl) as mg_aant_lfl, sum(stock_unidades_actual) as stock_uds_aact, sum(stock_unidades_anterior) as stock_uds_aant, sum(stock_anio_actual) as stock_aact, sum(stock_anio_anterior) as stock_aant from (select mercado, branch_no, nombre_tienda, tipo_tienda, subzona, delegado, zona, territorial, dia, semana, mes, sum(venta_uds_anio_actual) as venta_uds_anio_actual, sum(venta_uds_anio_anterior) as venta_uds_anio_anterior, sum(venta_neta_anio_actual) as venta_neta_anio_actual, sum(venta_neta_anio_anterior) as venta_neta_anio_anterior, sum(venta_neta_anio_actual_lfl) as venta_neta_anio_actual_lfl, sum(venta_neta_anio_anterior_lfl) as venta_neta_anio_anterior_lfl, sum(venta_bruta_anio_actual) as venta_bruta_anio_actual, sum(venta_bruta_anio_anterior) as venta_bruta_anio_anterior, sum(venta_bruta_anio_actual_lfl) as venta_bruta_anio_actual_lfl, sum(venta_bruta_anio_anterior_lfl) as venta_bruta_anio_anterior_lfl, sum(margen_anio_actual) as margen_anio_actual, sum(margen_anio_anterior) as margen_anio_anterior, sum(margen_anio_actual_lfl) as margen_anio_actual_lfl, sum(margen_anio_anterior_lfl) as margen_anio_anterior_lfl, sum(stock_unidades_actual) as stock_unidades_actual, sum(stock_unidades_anterior) as stock_unidades_anterior, sum(stock_anio_actual) as stock_anio_actual, sum(stock_anio_anterior) as stock_anio_anterior from ventas_stock1 where EsTienda=1 group by branch_no,nombre_tienda,tipo_tienda,subzona,delegado,zona,territorial,dia,semana,mes ) a group by a.branch_no,a.nombre_tienda,a.tipo_tienda,a.subzona,a.delegado,a.zona,a.territorial,a.dia,a.semana,a.mes; /*alter table ticket_pre add index (mercado,branch_no,semana,dia);*/ alter table ticket_pre add index idx_mercado(mercado,branch_no,semana,dia); alter table ticket_pre add index idx(branch_no,semana,dia); -- Copiamos los datos de la tabla TICKET a TICKET_historico y borramos la tabla TICKET si nos encontramos en la semana 1 del año actual jd if SEMANA_INICIAL = 1 AND SEMANA_FINAL = 1 then insert into TICKET_historico (Anio_jd, Market, Branch_Id, Branch_Name, Location, Subzona, Delegation_Manager, Region, Territorial_Manager, Weekday, `Date`, Week, `Month`, FF_TY, FF_LY, FF_TY_LFL, FF_LY_LFL, Transactions_TY, Transactions_LY, Transactions_TY_LFL, Transactions_LY_LFL, Unist_Sold_TY, Unist_Sold_LY, Gross_Sales_TY, Gross_Sales_LY, Gross_Sales_TY_LFL, Gross_Sales_LY_LFL, Transactions_TXT_TY, Transactions_TXT_LY, Transactions_FWR_TY, Transactions_FWR_LY, Transactions_ACC_HEQ_TY, Transactions_ACC_HEQ_LY, Stock_Units_TY, Stock_Units_LY, Stock_Euros_TY, Stock_Euros_LY) select ANIO_ACTUAL-1 as Anio_jd, Market, Branch_Id, Branch_Name, location, Subzona, Delegation_Manager, Region, Territorial_Manager, Weekday, `Date`, Week, `month`, FF_TY, FF_LY, FF_TY_LFL, FF_LY_LFL, Transactions_TY, Transactions_LY, Transactions_TY_LFL, Transactions_LY_LFL, Unist_Sold_TY, Unist_Sold_LY, Gross_Sales_TY, Gross_Sales_LY, Gross_Sales_TY_LFL, Gross_Sales_LY_LFL, Transactions_TXT_TY, Transactions_TXT_LY, Transactions_FWR_TY, Transactions_FWR_LY, Transactions_ACC_HEQ_TY, Transactions_ACC_HEQ_LY, Stock_Units_TY, Stock_Units_LY, Stock_Euros_TY, Stock_Euros_LY from TICKET; truncate table TICKET; else delete from TICKET where cast(Week as unsigned) between SEMANA_INICIAL and SEMANA_FINAL; end if; -- Insertamos en la tabla TICKET los nuevos datos calculados en tienda_pre1 y tienda_pre2 insert into TICKET (Market, Branch_Id, Branch_Name, Location, Subzona, Delegation_Manager, Region, Territorial_Manager, Weekday, `Date`, Week, `Month`, FF_TY, FF_LY, FF_TY_LFL, FF_LY_LFL, Transactions_TY, Transactions_LY, Transactions_TY_LFL, Transactions_LY_LFL, Unist_Sold_TY, Unist_Sold_LY, Gross_Sales_TY, Gross_Sales_LY, Gross_Sales_TY_LFL, Gross_Sales_LY_LFL, Transactions_TXT_TY, Transactions_TXT_LY, Transactions_FWR_TY, Transactions_FWR_LY, Transactions_ACC_HEQ_TY, Transactions_ACC_HEQ_LY, Stock_Units_TY, Stock_Units_LY, Stock_Euros_TY, Stock_Euros_LY) select a1.mercado as Market, a1.branch_no as Branch_Id, a1.nombre_tienda as Branch_Name, a1.tipo_tienda as Location, a1.subzona as Subzona, a1.delegado as Delegation_Manager, a1.zona as Region, a1.territorial as Territorial_Manager, (CASE when a1.dia=5 then 'Sat' when a1.dia=4 then 'Fri' when a1.dia=3 then 'Thu' when a1.dia=2 then 'Wed' when a1.dia=1 then 'Tue' when a1.dia=0 then 'Mon' when a1.dia=6 then 'Sun' end) as Weekday, h.Fecha as Date, a1.semana as Week, a1.mes as Month, format(coalesce(f.entradas_aact,0),0,'de_DE') as FF_TY, format(coalesce(g.entradas_aant,0),0,'de_DE') as FF_LY, format((case when coalesce(f.entradas_aact,0)=0 or coalesce(g.entradas_aant,0)=0 then 0 else coalesce(f.entradas_aact,0) end),0,'de_DE') as FF_TY_LFL, format((case when coalesce(g.entradas_aant,0)=0 or coalesce(f.entradas_aact,0)=0 then 0 else coalesce(g.entradas_aant,0) end),0,'de_DE') as FF_LY_LFL, format(coalesce(b.num_tickets_aact,0),0,'de_DE') as Transactions_TY, format(coalesce(c.num_tickets_aant,0),0,'de_DE') as Transactions_LY, /*format((case when coalesce(f.entradas_aact,0)=0 or coalesce(g.entradas_aant,0)=0 then 0 else coalesce(b.num_tickets_aact,0) end),0,'de_DE') as Transactions_TY_LFL, format((case when coalesce(g.entradas_aant,0)=0 or coalesce(f.entradas_aact,0)=0 then 0 else coalesce(c.num_tickets_aant,0) end),0,'de_DE') as Transactions_LY_LFL,*/ format((case when coalesce(b.num_tickets_aact,0)=0 or coalesce(c.num_tickets_aant,0)=0 then 0 else coalesce(b.num_tickets_aact,0) end),0,'de_DE') as Transactions_TY_LFL, format((case when coalesce(c.num_tickets_aant,0)=0 or coalesce(b.num_tickets_aact,0)=0 then 0 else coalesce(c.num_tickets_aant,0) end),0,'de_DE') as Transactions_LY_LFL, format(coalesce(a.vta_uds_Aact,0),0,'de_DE') as Unist_Sold_TY, format(coalesce(a.vta_uds_Aant,0),0,'de_DE') as Unist_Sold_LY, format(coalesce(a.vta_bruta_Aact,0),2,'de_DE') as Gross_Sales_TY, format(coalesce(a.vta_bruta_Aant,0),2,'de_DE') as Gross_Sales_LY, format(coalesce(a.vta_bruta_Aact_lfl,0),2,'de_DE') as Gross_Sales_TY_LFL, format(coalesce(a.vta_bruta_Aant_lfl,0),2,'de_DE') as Gross_Sales_LY_LFL, format(coalesce(d.num_tickets_aact_textil,0),0,'de_DE') as Transactions_TXT_TY, format(coalesce(e.num_tickets_aant_textil,0),0,'de_DE') as Transactions_TXT_LY, format(coalesce(d.num_tickets_aact_calzado,0),0,'de_DE') as Transactions_FWR_TY, format(coalesce(e.num_tickets_aant_calzado,0),0,'de_DE') as Transactions_FWR_LY, format(coalesce(d.num_tickets_aact_acc,0),0,'de_DE') as Transactions_ACC_HEQ_TY, format(coalesce(e.num_tickets_aant_acc,0),0,'de_DE') as Transactions_ACC_HEQ_LY, format(coalesce(a.stock_uds_aact,0),0,'de_DE') as Stock_Units_TY, format(coalesce(a.stock_uds_aant,0),0,'de_DE') as Stock_Units_LY, format(coalesce(a.stock_aact,0),2,'de_DE') as Stock_Euros_TY, format(coalesce(a.stock_aant,0),2,'de_DE') as Stock_Euros_LY from ticket_pre_a1 a1 left join ticket_pre a on a1.branch_no=a.branch_no and a1.semana=a.semana and a1.dia=a.dia left join tickets_aact b on a1.branch_no=b.branch_no and a1.semana=b.semana and a1.dia=b.dia left join tickets_aant c on a1.branch_no=c.branch_no and a1.semana=c.semana and a1.dia=c.dia left join tickets_areas_aact d on a1.branch_no=d.branch_no and a1.semana=d.semana and a1.dia=d.dia left join tickets_areas_aant e on a1.branch_no=e.branch_no and a1.semana=e.semana and a1.dia=e.dia left join footfall_aact f on a1.branch_no=f.branch_no and a1.semana=f.semana and a1.dia=f.dia left join footfall_aant g on a1.branch_no=g.branch_no and a1.semana=g.semana and a1.dia=g.dia left join md_dbg.calendario_Detalle_jd_v2 h on a1.dia=weekday(h.Fecha) and a1.semana=h.semana_jd and h.anio_jd=ANIO_ACTUAL; -- ---------------------------------------------------------------------------------------------------- /* TABLA TICKET SIN SERVICIOS */ -- ---------------------------------------------------------------------------------------------------- drop table if exists ticket_pre_NO_SERV; create table ticket_pre_NO_SERV as select a.mercado, a.branch_no, a.nombre_tienda, a.tipo_tienda, a.subzona, a.delegado, a.zona, a.territorial, a.dia, a.semana, a.mes, sum(venta_uds_anio_actual) as vta_uds_Aact, sum(venta_uds_anio_anterior) as vta_uds_Aant, sum(venta_neta_anio_actual) as vta_neta_Aact, sum(venta_neta_anio_anterior) as vta_neta_Aant, sum(venta_neta_anio_actual_lfl) as vta_neta_Aact_lfl, sum(venta_neta_anio_anterior_lfl) as vta_neta_Aant_lfl, sum(venta_bruta_anio_actual) as vta_bruta_Aact, sum(venta_bruta_anio_anterior) as vta_bruta_Aant, sum(venta_bruta_anio_actual_lfl) as vta_bruta_Aact_lfl, sum(venta_bruta_anio_anterior_lfl) as vta_bruta_Aant_lfl, sum(margen_anio_actual) as mg_aact, sum(margen_anio_anterior) as mg_aant, sum(margen_anio_actual_lfl) as mg_aact_lfl, sum(margen_anio_anterior_lfl) as mg_aant_lfl, sum(stock_unidades_actual) as stock_uds_aact, sum(stock_unidades_anterior) as stock_uds_aant, sum(stock_anio_actual) as stock_aact, sum(stock_anio_anterior) as stock_aant from (select mercado, branch_no, nombre_tienda, tipo_tienda, subzona, delegado, zona, territorial, dia, semana, mes, sum(venta_uds_anio_actual) as venta_uds_anio_actual, sum(venta_uds_anio_anterior) as venta_uds_anio_anterior, sum(venta_neta_anio_actual) as venta_neta_anio_actual, sum(venta_neta_anio_anterior) as venta_neta_anio_anterior, sum(venta_neta_anio_actual_lfl) as venta_neta_anio_actual_lfl, sum(venta_neta_anio_anterior_lfl) as venta_neta_anio_anterior_lfl, sum(venta_bruta_anio_actual) as venta_bruta_anio_actual, sum(venta_bruta_anio_anterior) as venta_bruta_anio_anterior, sum(venta_bruta_anio_actual_lfl) as venta_bruta_anio_actual_lfl, sum(venta_bruta_anio_anterior_lfl) as venta_bruta_anio_anterior_lfl, sum(margen_anio_actual) as margen_anio_actual, sum(margen_anio_anterior) as margen_anio_anterior, sum(margen_anio_actual_lfl) as margen_anio_actual_lfl, sum(margen_anio_anterior_lfl) as margen_anio_anterior_lfl, sum(stock_unidades_actual) as stock_unidades_actual, sum(stock_unidades_anterior) as stock_unidades_anterior, sum(stock_anio_actual) as stock_anio_actual, sum(stock_anio_anterior) as stock_anio_anterior from ventas_stock1 where EsTienda=1 AND area <> 'Servicios' group by branch_no,nombre_tienda,tipo_tienda,subzona,delegado,zona,territorial,dia,semana,mes ) a group by a.branch_no,a.nombre_tienda,a.tipo_tienda,a.subzona,a.delegado,a.zona,a.territorial,a.dia,a.semana,a.mes; /*alter table ticket_pre_NO_SERV add index (mercado,branch_no,semana,dia);*/ alter table ticket_pre_NO_SERV add index idx_mercado(mercado,branch_no,semana,dia); alter table ticket_pre_NO_SERV add index idx(branch_no,semana,dia); -- Borramos la tabla TICKET_NO_SERV si nos encontramos en la semana 1 del año actual jd if SEMANA_INICIAL = 1 then truncate table TICKET_NO_SERV; else delete from TICKET_NO_SERV where cast(Week as unsigned) between SEMANA_INICIAL and SEMANA_FINAL; end if; -- Insertamos en la tabla TICKET_NO_SERV los nuevos datos calculados en ticket_pre_a1 y ticket_pre_NO_SERV insert into TICKET_NO_SERV (Market, Branch_Id, Branch_Name, Location, Subzona, Delegation_Manager, Region, Territorial_Manager, Weekday, `Date`, Week, `Month`, FF_TY, FF_LY, FF_TY_LFL, FF_LY_LFL, Transactions_TY, Transactions_LY, Transactions_TY_LFL, Transactions_LY_LFL, Unist_Sold_TY, Unist_Sold_LY, Gross_Sales_TY, Gross_Sales_LY, Gross_Sales_TY_LFL, Gross_Sales_LY_LFL, Transactions_TXT_TY, Transactions_TXT_LY, Transactions_FWR_TY, Transactions_FWR_LY, Transactions_ACC_HEQ_TY, Transactions_ACC_HEQ_LY, Stock_Units_TY, Stock_Units_LY, Stock_Euros_TY, Stock_Euros_LY) select a1.mercado as Market, a1.branch_no as Branch_Id, a1.nombre_tienda as Branch_Name, a1.tipo_tienda as Location, a1.subzona as Subzona, a1.delegado as Delegation_Manager, a1.zona as Region, a1.territorial as Territorial_Manager, (CASE when a1.dia=5 then 'Sat' when a1.dia=4 then 'Fri' when a1.dia=3 then 'Thu' when a1.dia=2 then 'Wed' when a1.dia=1 then 'Tue' when a1.dia=0 then 'Mon' when a1.dia=6 then 'Sun' end) as Weekday, h.Fecha as Date, a1.semana as Week, a1.mes as Month, format(coalesce(f.entradas_aact,0),0,'de_DE') as FF_TY, format(coalesce(g.entradas_aant,0),0,'de_DE') as FF_LY, format((case when coalesce(f.entradas_aact,0)=0 or coalesce(g.entradas_aant,0)=0 then 0 else coalesce(f.entradas_aact,0) end),0,'de_DE') as FF_TY_LFL, format((case when coalesce(g.entradas_aant,0)=0 or coalesce(f.entradas_aact,0)=0 then 0 else coalesce(g.entradas_aant,0) end),0,'de_DE') as FF_LY_LFL, format(coalesce(b.num_tickets_aact,0),0,'de_DE') as Transactions_TY, format(coalesce(c.num_tickets_aant,0),0,'de_DE') as Transactions_LY, /*format((case when coalesce(f.entradas_aact,0)=0 or coalesce(g.entradas_aant,0)=0 then 0 else coalesce(b.num_tickets_aact,0) end),0,'de_DE') as Transactions_TY_LFL, format((case when coalesce(g.entradas_aant,0)=0 or coalesce(f.entradas_aact,0)=0 then 0 else coalesce(c.num_tickets_aant,0) end),0,'de_DE') as Transactions_LY_LFL,*/ format((case when coalesce(b.num_tickets_aact,0)=0 or coalesce(c.num_tickets_aant,0)=0 then 0 else coalesce(b.num_tickets_aact,0) end),0,'de_DE') as Transactions_TY_LFL, format((case when coalesce(c.num_tickets_aant,0)=0 or coalesce(b.num_tickets_aact,0)=0 then 0 else coalesce(c.num_tickets_aant,0) end),0,'de_DE') as Transactions_LY_LFL, format(coalesce(a.vta_uds_Aact,0),0,'de_DE') as Unist_Sold_TY, format(coalesce(a.vta_uds_Aant,0),0,'de_DE') as Unist_Sold_LY, format(coalesce(a.vta_bruta_Aact,0),2,'de_DE') as Gross_Sales_TY, format(coalesce(a.vta_bruta_Aant,0),2,'de_DE') as Gross_Sales_LY, format(coalesce(a.vta_bruta_Aact_lfl,0),2,'de_DE') as Gross_Sales_TY_LFL, format(coalesce(a.vta_bruta_Aant_lfl,0),2,'de_DE') as Gross_Sales_LY_LFL, format(coalesce(d.num_tickets_aact_textil,0),0,'de_DE') as Transactions_TXT_TY, format(coalesce(e.num_tickets_aant_textil,0),0,'de_DE') as Transactions_TXT_LY, format(coalesce(d.num_tickets_aact_calzado,0),0,'de_DE') as Transactions_FWR_TY, format(coalesce(e.num_tickets_aant_calzado,0),0,'de_DE') as Transactions_FWR_LY, format(coalesce(d.num_tickets_aact_acc,0),0,'de_DE') as Transactions_ACC_HEQ_TY, format(coalesce(e.num_tickets_aant_acc,0),0,'de_DE') as Transactions_ACC_HEQ_LY, format(coalesce(a.stock_uds_aact,0),0,'de_DE') as Stock_Units_TY, format(coalesce(a.stock_uds_aant,0),0,'de_DE') as Stock_Units_LY, format(coalesce(a.stock_aact,0),2,'de_DE') as Stock_Euros_TY, format(coalesce(a.stock_aant,0),2,'de_DE') as Stock_Euros_LY from ticket_pre_a1 a1 left join ticket_pre_NO_SERV a on a1.branch_no=a.branch_no and a1.semana=a.semana and a1.dia=a.dia left join tickets_aact_NO_SERV b on a1.branch_no=b.branch_no and a1.semana=b.semana and a1.dia=b.dia left join tickets_aant_NO_SERV c on a1.branch_no=c.branch_no and a1.semana=c.semana and a1.dia=c.dia left join tickets_areas_aact d on a1.branch_no=d.branch_no and a1.semana=d.semana and a1.dia=d.dia left join tickets_areas_aant e on a1.branch_no=e.branch_no and a1.semana=e.semana and a1.dia=e.dia left join footfall_aact f on a1.branch_no=f.branch_no and a1.semana=f.semana and a1.dia=f.dia left join footfall_aant g on a1.branch_no=g.branch_no and a1.semana=g.semana and a1.dia=g.dia left join md_dbg.calendario_Detalle_jd_v2 h on a1.dia=weekday(h.Fecha) and a1.semana=h.semana_jd and h.anio_jd=ANIO_ACTUAL; drop table if exists tickets_areas_aact_pre; drop table if exists tickets_areas_aant_pre; -- ---------------------------------------------------------------------------------------------------- /* TABLA TICKET APP */ -- ---------------------------------------------------------------------------------------------------- drop table if exists ticket_pre_app; create table ticket_pre_app as select mercado, branch_no, nombre_tienda, tipo_tienda, subzona, delegado, zona, territorial, dia, semana, mes, sum(venta_uds_anio_actual) as vta_uds_Aact, sum(venta_uds_anio_anterior) as vta_uds_Aant, sum(venta_bruta_anio_actual) as vta_bruta_Aact, sum(venta_bruta_anio_anterior) as vta_bruta_Aant, sum(venta_bruta_anio_actual_LFL) as vta_bruta_Aact_LFL, sum(venta_bruta_anio_anterior_LFL) as vta_bruta_Aant_LFL from ventas_stock_app where EsTienda=1 group by branch_no,nombre_tienda,tipo_tienda,subzona,delegado,zona,territorial,dia,semana,mes having vta_uds_Aact+vta_uds_Aant+vta_bruta_Aact+vta_bruta_Aant<>0; alter table ticket_pre_app add index (branch_no,semana,dia); -- Borramos la tabla TICKET_APP si nos encontramos en la semana 1 del año actual jd if SEMANA_INICIAL = 1 then truncate table TICKET_APP; else delete from TICKET_APP where cast(Week as unsigned) between SEMANA_INICIAL and SEMANA_FINAL; end if; -- Insertamos en la tabla TICKET_APP los nuevos datos calculados en ticket_pre_app y tickets_aact_app insert into TICKET_APP (Market, Branch_Id, Branch_Name, Location, Subzona, Delegation_Manager, Region, Territorial_Manager, Weekday, `Date`, Week, `Month`, FF_TY, FF_LY, FF_TY_LFL, FF_LY_LFL, Transactions_TY, Transactions_LY, Transactions_TY_LFL, Transactions_LY_LFL, Unist_Sold_TY, Unist_Sold_LY, Gross_Sales_TY, Gross_Sales_LY, Gross_Sales_TY_LFL, Gross_Sales_LY_LFL, Transactions_TXT_TY, Transactions_TXT_LY, Transactions_FWR_TY, Transactions_FWR_LY, Transactions_ACC_HEQ_TY, Transactions_ACC_HEQ_LY) select a.mercado as Market, a.branch_no as Branch_Id, a.nombre_tienda as Branch_Name, a.tipo_tienda as Location, a.subzona as Subzona, a.delegado as Delegation_Manager, a.zona as Region, a.territorial as Territorial_Manager, (CASE when a.dia=5 then 'Sat' when a.dia=4 then 'Fri' when a.dia=3 then 'Thu' when a.dia=2 then 'Wed' when a.dia=1 then 'Tue' when a.dia=0 then 'Mon' when a.dia=6 then 'Sun' end) as Weekday, h.Fecha as Date, a.semana as Week, a.mes as Month, format(coalesce(f.entradas_aact,0),0,'de_DE') as FF_TY, format(coalesce(g.entradas_aant,0),0,'de_DE') as FF_LY, format((case when coalesce(f.entradas_aact,0)=0 or coalesce(g.entradas_aant,0)=0 then 0 else coalesce(f.entradas_aact,0) end),0,'de_DE') as FF_TY_LFL, format((case when coalesce(g.entradas_aant,0)=0 or coalesce(f.entradas_aact,0)=0 then 0 else coalesce(g.entradas_aant,0) end),0,'de_DE') as FF_LY_LFL, format(coalesce(b.num_tickets_aact,0),0,'de_DE') as Transactions_TY, format(coalesce(c.num_tickets_aant,0),0,'de_DE') as Transactions_LY, /*format((case when coalesce(f.entradas_aact,0)=0 or coalesce(g.entradas_aant,0)=0 then 0 else coalesce(b.num_tickets_aact,0) end),0,'de_DE') as Transactions_TY_LFL, format((case when coalesce(g.entradas_aant,0)=0 or coalesce(f.entradas_aact,0)=0 then 0 else coalesce(c.num_tickets_aant,0) end),0,'de_DE') as Transactions_LY_LFL,*/ format((case when coalesce(b.num_tickets_aact,0)=0 or coalesce(c.num_tickets_aant,0)=0 then 0 else coalesce(b.num_tickets_aact,0) end),0,'de_DE') as Transactions_TY_LFL, format((case when coalesce(c.num_tickets_aant,0)=0 or coalesce(b.num_tickets_aact,0)=0 then 0 else coalesce(c.num_tickets_aant,0) end),0,'de_DE') as Transactions_LY_LFL, format(a.vta_uds_Aact,0,'de_DE') as Unist_Sold_TY, format(a.vta_uds_Aant,0,'de_DE') as Unist_Sold_LY, format(a.vta_bruta_Aact,2,'de_DE') as Gross_Sales_TY, format(a.vta_bruta_Aant,2,'de_DE') as Gross_Sales_LY, format(a.vta_bruta_Aact_LFL,2,'de_DE') as Gross_Sales_TY_LFL, format(a.vta_bruta_Aant_LFL,2,'de_DE') as Gross_Sales_LY_LFL, format(coalesce(d.num_tickets_aact_textil,0),0,'de_DE') as Transactions_TXT_TY, format(coalesce(e.num_tickets_aant_textil,0),0,'de_DE') as Transactions_TXT_LY, format(coalesce(d.num_tickets_aact_calzado,0),0,'de_DE') as Transactions_FWR_TY, format(coalesce(e.num_tickets_aant_calzado,0),0,'de_DE') as Transactions_FWR_LY, format(coalesce(d.num_tickets_aact_acc,0),0,'de_DE') as Transactions_ACC_HEQ_TY, format(coalesce(e.num_tickets_aant_acc,0),0,'de_DE') as Transactions_ACC_HEQ_LY from ticket_pre_app a left join tickets_aact_app b on a.branch_no=b.branch_no and a.semana=b.semana and a.dia=b.dia left join tickets_aant_app c on a.branch_no=c.branch_no and a.semana=c.semana and a.dia=c.dia left join tickets_areas_aact_app d on a.branch_no=d.branch_no and a.semana=d.semana and a.dia=d.dia left join tickets_areas_aant_app e on a.branch_no=e.branch_no and a.semana=e.semana and a.dia=e.dia left join footfall_aact f on a.branch_no=f.branch_no and a.semana=f.semana and a.dia=f.dia left join footfall_aant g on a.branch_no=g.branch_no and a.semana=g.semana and a.dia=g.dia left join md_dbg.calendario_Detalle_jd_v2 h on a.dia=weekday(h.Fecha) and a.semana=h.semana_jd and h.anio_jd=ANIO_ACTUAL; drop table if exists ticket_pre_app; drop table if exists tickets_aact_app; drop table if exists tickets_aant_app; drop table if exists tickets_areas_aact_pre_app; drop table if exists tickets_areas_aant_pre_app; drop table if exists tickets_areas_aact_app; drop table if exists tickets_areas_aant_app; drop table if exists ticket_pre_app; -- ---------------------------------------------------------------------------------------------------- /* TIENDA-AREA2*/ -- ---------------------------------------------------------------------------------------------------- -- Reusamos el ventas_stock_stg generado en la línea 2398 para marca. Borrar y crear la tabla en cada ejecución por temporalidad. /* jd_tienda_area SEMANAL */ drop table if exists tienda_area; create table tienda_area as select ' ' as totales, a.mercado, a.branch_no, (case when a.area in ('Complementos','Material técnico') then 'Complementos y Técnico' when a.area in ('Promociones','Servicios','Otros','na') then 'Otros' else area end) as area, cast(sum(a.venta_uds_anio_actual) as dec (10,0)) as Vta_uds_AAct, cast((sum(a.venta_uds_anio_actual)-sum(a.venta_uds_anio_anterior))/sum(a.venta_uds_anio_anterior) as dec (10,4)) as Dif_uds_perc, cast(sum(a.venta_neta_anio_actual) as dec(10,0)) as Vta_AAct, cast(sum(a.venta_neta_anio_anterior) as dec(10,0)) as Vta_AAnt, cast(sum(a.venta_neta_anio_actual)-sum(a.venta_neta_anio_anterior) as dec(10,0)) as Dif_vta, cast((sum(a.venta_neta_anio_actual)-sum(a.venta_neta_anio_anterior))/sum(a.venta_neta_anio_anterior) as dec (10,4)) as Dif_vta_per, cast(sum(a.margen_anio_actual) as dec(10,0)) as Mg_AAct, cast(sum(a.margen_anio_anterior) as dec(10,0)) as Mg_AAnt, cast(sum(a.margen_anio_actual)-sum(a.margen_anio_anterior) as dec(10,0)) as Dif_Mg, cast((sum(a.margen_anio_actual)-sum(a.margen_anio_anterior))/sum(a.margen_anio_anterior) as dec (10,4)) as Dif_Mg_per, cast(sum(a.margen_anio_actual)/sum(a.venta_neta_anio_actual) as dec (10,4)) as GR_AAct, cast(sum(a.margen_anio_anterior)/sum(a.venta_neta_anio_anterior) as dec (10,4)) as GR_AAnt, cast(sum(a.stock_unidades_actual) as dec(10,0)) as St_uds_Aact, cast(sum(a.stock_unidades_actual)-sum(a.stock_unidades_anterior) as dec(10,0)) as Dif_St_uds, cast((sum(a.stock_unidades_actual)-sum(a.stock_unidades_anterior))/sum(a.stock_unidades_anterior) as dec (10,4)) as Dif_St_uds_per, cast(sum(a.stock_anio_actual) as dec(10,0)) as St_AAct_tot, cast(sum(a.stock_anio_anterior) as dec (10,0)) as St_AAnt_tot, cast(sum(a.stock_anio_actual)-sum(a.stock_anio_anterior) as dec (10,0)) as Dif_St, cast((sum(a.stock_anio_actual)-sum(a.stock_anio_anterior))/sum(a.stock_anio_anterior) as dec (10,4)) as Dif_St_per, cast(sum(a.stock_unidades_actual)/sum(a.venta_uds_ultima_semana_anio_actual) as dec(10,2)) as cob_AAct, cast(sum(a.stock_unidades_anterior)/sum(a.venta_uds_ultima_semana_anio_anterior) as dec(10,2)) as cob_AAnt from (select mercado, branch_no, area, sum(venta_uds_anio_actual) as venta_uds_anio_actual, sum(venta_uds_anio_anterior) as venta_uds_anio_anterior, sum(venta_neta_anio_actual) as venta_neta_anio_actual, sum(venta_neta_anio_anterior) as venta_neta_anio_anterior, sum(margen_anio_actual) as margen_anio_actual, sum(margen_anio_anterior) as margen_anio_anterior, sum(stock_unidades_actual) as stock_unidades_actual, sum(stock_unidades_anterior) as stock_unidades_anterior, sum(stock_anio_actual) as stock_anio_actual, sum(stock_anio_anterior) as stock_anio_anterior, sum(venta_uds_ultima_semana_anio_actual) as venta_uds_ultima_semana_anio_actual, sum(venta_uds_ultima_semana_anio_anterior) as venta_uds_ultima_semana_anio_anterior from ventas_stock_stg WHERE EsTienda=1 and cast(semana as unsigned) = SEMANA_FINAL group by branch_no,area ) a group by branch_no,(case when area in ('Complementos','Material técnico') then 'Complementos y Técnico' when area in ('Promociones','Servicios','Otros','na') then 'Otros' else area end) order by branch_no,Vta_AAct desc; drop table if exists tienda_area_subtotal; create table tienda_area_subtotal as select 'SUBTOTAL' as totales, a.mercado, a.branch_no, '' as area, cast(sum(a.venta_uds_anio_actual) as dec (10,0)) as Vta_uds_AAct, cast((sum(a.venta_uds_anio_actual)-sum(a.venta_uds_anio_anterior))/sum(a.venta_uds_anio_anterior) as dec (10,4)) as Dif_uds_perc, cast(sum(a.venta_neta_anio_actual) as dec(10,0)) as Vta_AAct, cast(sum(a.venta_neta_anio_anterior) as dec(10,0)) as Vta_AAnt, cast(sum(a.venta_neta_anio_actual)-sum(a.venta_neta_anio_anterior) as dec(10,0)) as Dif_vta, cast((sum(a.venta_neta_anio_actual)-sum(a.venta_neta_anio_anterior))/sum(a.venta_neta_anio_anterior) as dec (10,4)) as Dif_vta_per, cast(sum(a.margen_anio_actual) as dec(10,0)) as Mg_AAct, cast(sum(a.margen_anio_anterior) as dec(10,0)) as Mg_AAnt, cast(sum(a.margen_anio_actual)-sum(a.margen_anio_anterior) as dec(10,0)) as Dif_Mg, cast((sum(a.margen_anio_actual)-sum(a.margen_anio_anterior))/sum(a.margen_anio_anterior) as dec (10,4)) as Dif_Mg_per, cast(sum(a.margen_anio_actual)/sum(a.venta_neta_anio_actual) as dec (10,4)) as GR_AAct, cast(sum(a.margen_anio_anterior)/sum(a.venta_neta_anio_anterior) as dec (10,4)) as GR_AAnt, cast(sum(a.stock_unidades_actual) as dec(10,0)) as St_uds_Aact, cast(sum(a.stock_unidades_actual)-sum(a.stock_unidades_anterior) as dec(10,0)) as Dif_St_uds, cast((sum(a.stock_unidades_actual)-sum(a.stock_unidades_anterior))/sum(a.stock_unidades_anterior) as dec (10,4)) as Dif_St_uds_per, cast(sum(a.stock_anio_actual) as dec(10,0)) as St_AAct_tot, cast(sum(a.stock_anio_anterior) as dec (10,0)) as St_AAnt_tot, cast(sum(a.stock_anio_actual)-sum(a.stock_anio_anterior) as dec (10,0)) as Dif_St, cast((sum(a.stock_anio_actual)-sum(a.stock_anio_anterior))/sum(a.stock_anio_anterior) as dec (10,4)) as Dif_St_per, cast(sum(a.stock_unidades_actual)/sum(a.venta_uds_ultima_semana_anio_actual) as dec(10,2)) as cob_AAct, cast(sum(a.stock_unidades_anterior)/sum(a.venta_uds_ultima_semana_anio_anterior) as dec(10,2)) as cob_AAnt from (select mercado, branch_no, sum(venta_uds_anio_actual) as venta_uds_anio_actual, sum(venta_uds_anio_anterior) as venta_uds_anio_anterior, sum(venta_neta_anio_actual) as venta_neta_anio_actual, sum(venta_neta_anio_anterior) as venta_neta_anio_anterior, sum(margen_anio_actual) as margen_anio_actual, sum(margen_anio_anterior) as margen_anio_anterior, sum(stock_unidades_actual) as stock_unidades_actual, sum(stock_unidades_anterior) as stock_unidades_anterior, sum(stock_anio_actual) as stock_anio_actual, sum(stock_anio_anterior) as stock_anio_anterior, sum(venta_uds_ultima_semana_anio_actual) as venta_uds_ultima_semana_anio_actual, sum(venta_uds_ultima_semana_anio_anterior) as venta_uds_ultima_semana_anio_anterior from ventas_stock_stg WHERE EsTienda=1 and cast(semana as unsigned) = SEMANA_FINAL group by branch_no ) a group by branch_no order by Vta_AAct; insert into tienda_area select * from tienda_area_subtotal; drop table if exists tienda_area_1; create table tienda_area_1 as select a.*, replace(concat(format(((a.Vta_AAct)/(b.Vta_AAct))*100,2),'%'),'.',',') as peso_1, replace(concat(format(((a.Mg_AAct)/(b.Mg_AAct))*100,2),'%'),'.',',') as peso_2, replace(concat(format(((a.St_AAct_tot)/(b.St_AAct_tot))*100,2),'%'),'.',',') as peso_3, replace(cast(((a.Vta_AAct/b.Vta_AAct)/(a.St_AAct_tot/b.St_AAct_tot)) as dec(10,2)),'.',',') as peso_4, replace(cast(((a.Mg_AAct/b.Mg_AAct)/(a.St_AAct_tot/b.St_AAct_tot)) as dec(10,2)),'.',',') as peso_5 from tienda_area a left join tienda_area_subtotal b on a.branch_no=b.branch_no order by branch_no,totales,Vta_AAct desc; drop table if exists tienda_area_total; create table tienda_area_total as select 'TOTAL' as totales, '' as mercado, '' as branch_no, '' as area, cast(sum(a.venta_uds_anio_actual) as dec (10,0)) as Vta_uds_AAct, cast((sum(a.venta_uds_anio_actual)-sum(a.venta_uds_anio_anterior))/sum(a.venta_uds_anio_anterior) as dec (10,4)) as Dif_uds_perc, cast(sum(a.venta_neta_anio_actual) as dec(10,0)) as Vta_AAct, cast(sum(a.venta_neta_anio_anterior) as dec(10,0)) as Vta_AAnt, cast(sum(a.venta_neta_anio_actual)-sum(a.venta_neta_anio_anterior) as dec(10,0)) as Dif_vta, cast((sum(a.venta_neta_anio_actual)-sum(a.venta_neta_anio_anterior))/sum(a.venta_neta_anio_anterior) as dec (10,4)) as Dif_vta_per, cast(sum(a.margen_anio_actual) as dec(10,0)) as Mg_AAct, cast(sum(a.margen_anio_anterior) as dec(10,0)) as Mg_AAnt, cast(sum(a.margen_anio_actual)-sum(a.margen_anio_anterior) as dec(10,0)) as Dif_Mg, cast((sum(a.margen_anio_actual)-sum(a.margen_anio_anterior))/sum(a.margen_anio_anterior) as dec (10,4)) as Dif_Mg_per, cast(sum(a.margen_anio_actual)/sum(a.venta_neta_anio_actual) as dec (10,4)) as GR_AAct, cast(sum(a.margen_anio_anterior)/sum(a.venta_neta_anio_anterior) as dec (10,4)) as GR_AAnt, cast(sum(a.stock_unidades_actual) as dec(10,0)) as St_uds_Aact, cast(sum(a.stock_unidades_actual)-sum(a.stock_unidades_anterior) as dec(10,0)) as Dif_St_uds, cast((sum(a.stock_unidades_actual)-sum(a.stock_unidades_anterior))/sum(a.stock_unidades_anterior) as dec (10,4)) as Dif_St_uds_per, cast(sum(a.stock_anio_actual) as dec(10,0)) as St_AAct_tot, cast(sum(a.stock_anio_anterior) as dec (10,0)) as St_AAnt_tot, cast(sum(a.stock_anio_actual)-sum(a.stock_anio_anterior) as dec (10,0)) as Dif_St, cast((sum(a.stock_anio_actual)-sum(a.stock_anio_anterior))/sum(a.stock_anio_anterior) as dec (10,4)) as Dif_St_per, cast(sum(a.stock_unidades_actual)/sum(a.venta_uds_ultima_semana_anio_actual) as dec(10,2)) as cob_AAct, cast(sum(a.stock_unidades_anterior)/sum(a.venta_uds_ultima_semana_anio_anterior) as dec(10,2)) as cob_AAnt, '' as peso_1, '' as peso_2, '' as peso_3, '' as peso_4, '' as peso_5 from ( select sum(venta_uds_anio_actual) as venta_uds_anio_actual, sum(venta_uds_anio_anterior) as venta_uds_anio_anterior, sum(venta_neta_anio_actual) as venta_neta_anio_actual, sum(venta_neta_anio_anterior) as venta_neta_anio_anterior, sum(margen_anio_actual) as margen_anio_actual, sum(margen_anio_anterior) as margen_anio_anterior, sum(stock_unidades_actual) as stock_unidades_actual, sum(stock_unidades_anterior) as stock_unidades_anterior, sum(stock_anio_actual) as stock_anio_actual, sum(stock_anio_anterior) as stock_anio_anterior, sum(venta_uds_ultima_semana_anio_actual) as venta_uds_ultima_semana_anio_actual, sum(venta_uds_ultima_semana_anio_anterior) as venta_uds_ultima_semana_anio_anterior from ventas_stock_stg WHERE EsTienda=1 and cast(semana as unsigned) = SEMANA_FINAL ) a ; insert into tienda_area_1 select * from tienda_area_total; drop table if exists tienda_area_2; create table tienda_area_2 as select a.*, concat(a.branch_no,' ',b.nombre_tienda) as TIENDA from tienda_area_1 a left join tienda_zona_territorial_tipo_tienda b on a.branch_no=b.branch_no; drop table if exists jd_tienda_area_semanal; create table jd_tienda_area_semanal as select a.totales, a.mercado as Market, a.TIENDA, a.area, REPLACE(FORMAT(a.Vta_uds_AAct,0),',','.') as Vta_uds_AAct, replace(CONCAT(FORMAT(a.Dif_uds_perc*100,2),'%'),'.',',') as Dif_uds_perc, REPLACE(FORMAT(a.Vta_AAct,0),',','.') as Vta_AAct, REPLACE(FORMAT(a.Vta_AAnt, 0),',','.') as Vta_AAnt, REPLACE(FORMAT(a.Dif_vta,0),',','.') as Dif_vta, replace(CONCAT(FORMAT(a.Dif_vta_per*100,2),'%'),'.',',') as Dif_vta_per, REPLACE(FORMAT(a.Mg_AAct,0),',','.') as Mg_AAct, REPLACE(FORMAT(a.Mg_AAnt,0),',','.') as Mg_AAnt, REPLACE(FORMAT(a.Dif_Mg, 0),',','.') as Dif_Mg, replace(CONCAT(FORMAT(a.Dif_Mg_per*100,2),'%'),'.',',') as Dif_Mg_per, replace(CONCAT(FORMAT(a.GR_AAct*100,2),'%'),'.',',') as GR_AAct, replace(CONCAT(FORMAT(a.GR_AAnt*100,2),'%'),'.',',') as GR_AAnt, REPLACE(FORMAT(a.St_uds_Aact,0),',','.') as St_uds_Aact, REPLACE(FORMAT(a.Dif_St_uds,0),',','.') as Dif_St_uds, replace(CONCAT(FORMAT(a.Dif_St_uds_per*100,2),'%'),'.',',') as Dif_St_uds_per, REPLACE(FORMAT(a.St_AAct_tot,0),',','.') as St_AAct_tot, REPLACE(FORMAT(a.St_AAnt_tot,0),',','.') as St_AAnt_tot, REPLACE(FORMAT(a.Dif_St,0),',','.') as Dif_St, replace(CONCAT(FORMAT(a.Dif_St_per*100,2),'%'),'.',',') as Dif_St_per, replace(cast(a.cob_AAct as dec(10,2)),'.',',') as cob_AAct, replace(cast(a.cob_AAnt as dec(10,2)),'.',',') as cob_AAnt, (case when a.totales in ('SUBTOTAL','TOTAL') then replace(cast(((a.Vta_AAct/b.Vta_AAct)/(a.St_AAct_tot/b.St_AAct_tot)) as dec (10,2)),'.',',') else a.peso_4 end) as vta_sobre_stock, (case when a.totales in ('SUBTOTAL','TOTAL') then replace(cast(((a.Mg_AAct/b.Mg_AAct)/(a.St_AAct_tot/b.St_AAct_tot)) as dec(10,2)),'.',',') else a.peso_5 end) as mg_sobre_stock, (case when a.totales in ('SUBTOTAL','TOTAL') then replace(concat(format(((a.Vta_AAct)/(b.Vta_AAct))*100,2),'%'),'.',',') else a.peso_1 end) as Mix_vtas, (case when a.totales in ('SUBTOTAL','TOTAL') then replace(concat(format(((a.Mg_AAct)/(b.Mg_AAct))*100,2),'%'),'.',',') else a.peso_2 end) as Mix_Mg, (case when a.totales in ('SUBTOTAL','TOTAL') then replace(concat(format(((a.St_AAct_tot)/(b.St_AAct_tot))*100,2),'%'),'.',',') else a.peso_3 end) as Mix_Stock from tienda_area_2 a left join tienda_area_total b on a.totales in ('SUBTOTAL','TOTAL'); /* eliminamos tablas temporales */ drop table if exists tienda_area_1; drop table if exists tienda_area_2; drop table if exists tienda_area; drop table if exists tienda_area_subtotal; drop table if exists tienda_area_total; /* ACUMULADO */ -- Volvemos a calcular los datos según la temporalidad acumulado drop table if exists tienda_area; create table tienda_area as select ' ' as totales, a.mercado, a.branch_no, (case when a.area in ('Complementos','Material técnico') then 'Complementos y Técnico' when a.area in ('Promociones','Servicios','Otros','na') then 'Otros' else area end) as area, cast(sum(a.venta_uds_anio_actual) as dec (10,0)) as Vta_uds_AAct, cast((sum(a.venta_uds_anio_actual)-sum(a.venta_uds_anio_anterior))/sum(a.venta_uds_anio_anterior) as dec (10,4)) as Dif_uds_perc, cast(sum(a.venta_neta_anio_actual) as dec(10,0)) as Vta_AAct, cast(sum(a.venta_neta_anio_anterior) as dec(10,0)) as Vta_AAnt, cast(sum(a.venta_neta_anio_actual)-sum(a.venta_neta_anio_anterior) as dec(10,0)) as Dif_vta, cast((sum(a.venta_neta_anio_actual)-sum(a.venta_neta_anio_anterior))/sum(a.venta_neta_anio_anterior) as dec (10,4)) as Dif_vta_per, cast(sum(a.margen_anio_actual) as dec(10,0)) as Mg_AAct, cast(sum(a.margen_anio_anterior) as dec(10,0)) as Mg_AAnt, cast(sum(a.margen_anio_actual)-sum(a.margen_anio_anterior) as dec(10,0)) as Dif_Mg, cast((sum(a.margen_anio_actual)-sum(a.margen_anio_anterior))/sum(a.margen_anio_anterior) as dec (10,4)) as Dif_Mg_per, cast(sum(a.margen_anio_actual)/sum(a.venta_neta_anio_actual) as dec (10,4)) as GR_AAct, cast(sum(a.margen_anio_anterior)/sum(a.venta_neta_anio_anterior) as dec (10,4)) as GR_AAnt, cast(sum(a.stock_unidades_actual) as dec(10,0)) as St_uds_Aact, cast(sum(a.stock_unidades_actual)-sum(a.stock_unidades_anterior) as dec(10,0)) as Dif_St_uds, cast((sum(a.stock_unidades_actual)-sum(a.stock_unidades_anterior))/sum(a.stock_unidades_anterior) as dec (10,4)) as Dif_St_uds_per, cast(sum(a.stock_anio_actual) as dec(10,0)) as St_AAct_tot, cast(sum(a.stock_anio_anterior) as dec (10,0)) as St_AAnt_tot, cast(sum(a.stock_anio_actual)-sum(a.stock_anio_anterior) as dec (10,0)) as Dif_St, cast((sum(a.stock_anio_actual)-sum(a.stock_anio_anterior))/sum(a.stock_anio_anterior) as dec (10,4)) as Dif_St_per, cast(sum(a.stock_unidades_actual)/sum(a.venta_uds_ultima_semana_anio_actual) as dec(10,2)) as cob_AAct, cast(sum(a.stock_unidades_anterior)/sum(a.venta_uds_ultima_semana_anio_anterior) as dec(10,2)) as cob_AAnt from (select mercado, branch_no, area, sum(venta_uds_anio_actual) as venta_uds_anio_actual, sum(venta_uds_anio_anterior) as venta_uds_anio_anterior, sum(venta_neta_anio_actual) as venta_neta_anio_actual, sum(venta_neta_anio_anterior) as venta_neta_anio_anterior, sum(margen_anio_actual) as margen_anio_actual, sum(margen_anio_anterior) as margen_anio_anterior, sum(stock_unidades_actual) as stock_unidades_actual, sum(stock_unidades_anterior) as stock_unidades_anterior, sum(stock_anio_actual) as stock_anio_actual, sum(stock_anio_anterior) as stock_anio_anterior, sum(venta_uds_ultima_semana_anio_actual) as venta_uds_ultima_semana_anio_actual, sum(venta_uds_ultima_semana_anio_anterior) as venta_uds_ultima_semana_anio_anterior from ventas_stock_stg WHERE EsTienda=1 group by branch_no,area ) a group by branch_no,(case when area in ('Complementos','Material técnico') then 'Complementos y Técnico' when area in ('Promociones','Servicios','Otros','na') then 'Otros' else area end) order by branch_no,Vta_AAct desc; drop table if exists tienda_area_subtotal; create table tienda_area_subtotal as select 'SUBTOTAL' as totales, a.mercado, a.branch_no, '' as area, cast(sum(a.venta_uds_anio_actual) as dec (10,0)) as Vta_uds_AAct, cast((sum(a.venta_uds_anio_actual)-sum(a.venta_uds_anio_anterior))/sum(a.venta_uds_anio_anterior) as dec (10,4)) as Dif_uds_perc, cast(sum(a.venta_neta_anio_actual) as dec(10,0)) as Vta_AAct, cast(sum(a.venta_neta_anio_anterior) as dec(10,0)) as Vta_AAnt, cast(sum(a.venta_neta_anio_actual)-sum(a.venta_neta_anio_anterior) as dec(10,0)) as Dif_vta, cast((sum(a.venta_neta_anio_actual)-sum(a.venta_neta_anio_anterior))/sum(a.venta_neta_anio_anterior) as dec (10,4)) as Dif_vta_per, cast(sum(a.margen_anio_actual) as dec(10,0)) as Mg_AAct, cast(sum(a.margen_anio_anterior) as dec(10,0)) as Mg_AAnt, cast(sum(a.margen_anio_actual)-sum(a.margen_anio_anterior) as dec(10,0)) as Dif_Mg, cast((sum(a.margen_anio_actual)-sum(a.margen_anio_anterior))/sum(a.margen_anio_anterior) as dec (10,4)) as Dif_Mg_per, cast(sum(a.margen_anio_actual)/sum(a.venta_neta_anio_actual) as dec (10,4)) as GR_AAct, cast(sum(a.margen_anio_anterior)/sum(a.venta_neta_anio_anterior) as dec (10,4)) as GR_AAnt, cast(sum(a.stock_unidades_actual) as dec(10,0)) as St_uds_Aact, cast(sum(a.stock_unidades_actual)-sum(a.stock_unidades_anterior) as dec(10,0)) as Dif_St_uds, cast((sum(a.stock_unidades_actual)-sum(a.stock_unidades_anterior))/sum(a.stock_unidades_anterior) as dec (10,4)) as Dif_St_uds_per, cast(sum(a.stock_anio_actual) as dec(10,0)) as St_AAct_tot, cast(sum(a.stock_anio_anterior) as dec (10,0)) as St_AAnt_tot, cast(sum(a.stock_anio_actual)-sum(a.stock_anio_anterior) as dec (10,0)) as Dif_St, cast((sum(a.stock_anio_actual)-sum(a.stock_anio_anterior))/sum(a.stock_anio_anterior) as dec (10,4)) as Dif_St_per, cast(sum(a.stock_unidades_actual)/sum(a.venta_uds_ultima_semana_anio_actual) as dec(10,2)) as cob_AAct, cast(sum(a.stock_unidades_anterior)/sum(a.venta_uds_ultima_semana_anio_anterior) as dec(10,2)) as cob_AAnt from (select mercado, branch_no, sum(venta_uds_anio_actual) as venta_uds_anio_actual, sum(venta_uds_anio_anterior) as venta_uds_anio_anterior, sum(venta_neta_anio_actual) as venta_neta_anio_actual, sum(venta_neta_anio_anterior) as venta_neta_anio_anterior, sum(margen_anio_actual) as margen_anio_actual, sum(margen_anio_anterior) as margen_anio_anterior, sum(stock_unidades_actual) as stock_unidades_actual, sum(stock_unidades_anterior) as stock_unidades_anterior, sum(stock_anio_actual) as stock_anio_actual, sum(stock_anio_anterior) as stock_anio_anterior, sum(venta_uds_ultima_semana_anio_actual) as venta_uds_ultima_semana_anio_actual, sum(venta_uds_ultima_semana_anio_anterior) as venta_uds_ultima_semana_anio_anterior from ventas_stock_stg WHERE EsTienda=1 group by branch_no ) a group by branch_no order by Vta_AAct; insert into tienda_area select * from tienda_area_subtotal; drop table if exists tienda_area_1; create table tienda_area_1 as select a.*, replace(concat(format(((a.Vta_AAct)/(b.Vta_AAct))*100,2),'%'),'.',',') as peso_1, replace(concat(format(((a.Mg_AAct)/(b.Mg_AAct))*100,2),'%'),'.',',') as peso_2, replace(concat(format(((a.St_AAct_tot)/(b.St_AAct_tot))*100,2),'%'),'.',',') as peso_3, replace(cast(((a.Vta_AAct/b.Vta_AAct)/(a.St_AAct_tot/b.St_AAct_tot)) as dec(10,2)),'.',',') as peso_4, replace(cast(((a.Mg_AAct/b.Mg_AAct)/(a.St_AAct_tot/b.St_AAct_tot)) as dec(10,2)),'.',',') as peso_5 from tienda_area a left join tienda_area_subtotal b on a.branch_no=b.branch_no order by branch_no,totales,Vta_AAct desc; drop table if exists tienda_area_total; create table tienda_area_total as select 'TOTAL' as totales, '' as mercado, '' as branch_no, '' as area, cast(sum(a.venta_uds_anio_actual) as dec (10,0)) as Vta_uds_AAct, cast((sum(a.venta_uds_anio_actual)-sum(a.venta_uds_anio_anterior))/sum(a.venta_uds_anio_anterior) as dec (10,4)) as Dif_uds_perc, cast(sum(a.venta_neta_anio_actual) as dec(10,0)) as Vta_AAct, cast(sum(a.venta_neta_anio_anterior) as dec(10,0)) as Vta_AAnt, cast(sum(a.venta_neta_anio_actual)-sum(a.venta_neta_anio_anterior) as dec(10,0)) as Dif_vta, cast((sum(a.venta_neta_anio_actual)-sum(a.venta_neta_anio_anterior))/sum(a.venta_neta_anio_anterior) as dec (10,4)) as Dif_vta_per, cast(sum(a.margen_anio_actual) as dec(10,0)) as Mg_AAct, cast(sum(a.margen_anio_anterior) as dec(10,0)) as Mg_AAnt, cast(sum(a.margen_anio_actual)-sum(a.margen_anio_anterior) as dec(10,0)) as Dif_Mg, cast((sum(a.margen_anio_actual)-sum(a.margen_anio_anterior))/sum(a.margen_anio_anterior) as dec (10,4)) as Dif_Mg_per, cast(sum(a.margen_anio_actual)/sum(a.venta_neta_anio_actual) as dec (10,4)) as GR_AAct, cast(sum(a.margen_anio_anterior)/sum(a.venta_neta_anio_anterior) as dec (10,4)) as GR_AAnt, cast(sum(a.stock_unidades_actual) as dec(10,0)) as St_uds_Aact, cast(sum(a.stock_unidades_actual)-sum(a.stock_unidades_anterior) as dec(10,0)) as Dif_St_uds, cast((sum(a.stock_unidades_actual)-sum(a.stock_unidades_anterior))/sum(a.stock_unidades_anterior) as dec (10,4)) as Dif_St_uds_per, cast(sum(a.stock_anio_actual) as dec(10,0)) as St_AAct_tot, cast(sum(a.stock_anio_anterior) as dec (10,0)) as St_AAnt_tot, cast(sum(a.stock_anio_actual)-sum(a.stock_anio_anterior) as dec (10,0)) as Dif_St, cast((sum(a.stock_anio_actual)-sum(a.stock_anio_anterior))/sum(a.stock_anio_anterior) as dec (10,4)) as Dif_St_per, cast(sum(a.stock_unidades_actual)/sum(a.venta_uds_ultima_semana_anio_actual) as dec(10,2)) as cob_AAct, cast(sum(a.stock_unidades_anterior)/sum(a.venta_uds_ultima_semana_anio_anterior) as dec(10,2)) as cob_AAnt, '' as peso_1, '' as peso_2, '' as peso_3, '' as peso_4, '' as peso_5 from ( select sum(venta_uds_anio_actual) as venta_uds_anio_actual, sum(venta_uds_anio_anterior) as venta_uds_anio_anterior, sum(venta_neta_anio_actual) as venta_neta_anio_actual, sum(venta_neta_anio_anterior) as venta_neta_anio_anterior, sum(margen_anio_actual) as margen_anio_actual, sum(margen_anio_anterior) as margen_anio_anterior, sum(stock_unidades_actual) as stock_unidades_actual, sum(stock_unidades_anterior) as stock_unidades_anterior, sum(stock_anio_actual) as stock_anio_actual, sum(stock_anio_anterior) as stock_anio_anterior, sum(venta_uds_ultima_semana_anio_actual) as venta_uds_ultima_semana_anio_actual, sum(venta_uds_ultima_semana_anio_anterior) as venta_uds_ultima_semana_anio_anterior from ventas_stock_stg WHERE EsTienda=1 ) a ; insert into tienda_area_1 select * from tienda_area_total; drop table if exists tienda_area_2; create table tienda_area_2 as select a.*, concat(a.branch_no,' ',b.nombre_tienda) as TIENDA from tienda_area_1 a left join tienda_zona_territorial_tipo_tienda b on a.branch_no=b.branch_no; drop table if exists jd_tienda_area_acumulado; create table jd_tienda_area_acumulado as select a.totales, a.mercado as Market, a.TIENDA, a.area, REPLACE(FORMAT(a.Vta_uds_AAct,0),',','.') as Vta_uds_AAct, replace(CONCAT(FORMAT(a.Dif_uds_perc*100,2),'%'),'.',',') as Dif_uds_perc, REPLACE(FORMAT(a.Vta_AAct,0),',','.') as Vta_AAct, REPLACE(FORMAT(a.Vta_AAnt, 0),',','.') as Vta_AAnt, REPLACE(FORMAT(a.Dif_vta,0),',','.') as Dif_vta, replace(CONCAT(FORMAT(a.Dif_vta_per*100,2),'%'),'.',',') as Dif_vta_per, REPLACE(FORMAT(a.Mg_AAct,0),',','.') as Mg_AAct, REPLACE(FORMAT(a.Mg_AAnt,0),',','.') as Mg_AAnt, REPLACE(FORMAT(a.Dif_Mg, 0),',','.') as Dif_Mg, replace(CONCAT(FORMAT(a.Dif_Mg_per*100,2),'%'),'.',',') as Dif_Mg_per, replace(CONCAT(FORMAT(a.GR_AAct*100,2),'%'),'.',',') as GR_AAct, replace(CONCAT(FORMAT(a.GR_AAnt*100,2),'%'),'.',',') as GR_AAnt, REPLACE(FORMAT(a.St_uds_Aact,0),',','.') as St_uds_Aact, REPLACE(FORMAT(a.Dif_St_uds,0),',','.') as Dif_St_uds, replace(CONCAT(FORMAT(a.Dif_St_uds_per*100,2),'%'),'.',',') as Dif_St_uds_per, REPLACE(FORMAT(a.St_AAct_tot,0),',','.') as St_AAct_tot, REPLACE(FORMAT(a.St_AAnt_tot,0),',','.') as St_AAnt_tot, REPLACE(FORMAT(a.Dif_St,0),',','.') as Dif_St, replace(CONCAT(FORMAT(a.Dif_St_per*100,2),'%'),'.',',') as Dif_St_per, replace(cast(a.cob_AAct as dec(10,2)),'.',',') as cob_AAct, replace(cast(a.cob_AAnt as dec(10,2)),'.',',') as cob_AAnt, (case when a.totales in ('SUBTOTAL','TOTAL') then replace(cast(((a.Vta_AAct/b.Vta_AAct)/(a.St_AAct_tot/b.St_AAct_tot)) as dec (10,2)),'.',',') else a.peso_4 end) as vta_sobre_stock, (case when a.totales in ('SUBTOTAL','TOTAL') then replace(cast(((a.Mg_AAct/b.Mg_AAct)/(a.St_AAct_tot/b.St_AAct_tot)) as dec(10,2)),'.',',') else a.peso_5 end) as mg_sobre_stock, (case when a.totales in ('SUBTOTAL','TOTAL') then replace(concat(format(((a.Vta_AAct)/(b.Vta_AAct))*100,2),'%'),'.',',') else a.peso_1 end) as Mix_vtas, (case when a.totales in ('SUBTOTAL','TOTAL') then replace(concat(format(((a.Mg_AAct)/(b.Mg_AAct))*100,2),'%'),'.',',') else a.peso_2 end) as Mix_Mg, (case when a.totales in ('SUBTOTAL','TOTAL') then replace(concat(format(((a.St_AAct_tot)/(b.St_AAct_tot))*100,2),'%'),'.',',') else a.peso_3 end) as Mix_Stock from tienda_area_2 a left join tienda_area_total b on a.totales in ('SUBTOTAL','TOTAL'); /* eliminamos tablas temporales */ drop table if exists tienda_area_1; drop table if exists tienda_area_2; drop table if exists tienda_area; drop table if exists tienda_area_subtotal; drop table if exists tienda_area_total; /* MENSUAL */ -- Creamos la tabla jd_tienda_area_mensual para la temporalidad mensual, solo si es la última semana_jd del mes_jd actual if FIN_MES_JD then drop table if exists tienda_area; create table tienda_area as select ' ' as totales, a.mercado, a.branch_no, (case when a.area in ('Complementos','Material técnico') then 'Complementos y Técnico' when a.area in ('Promociones','Servicios','Otros','na') then 'Otros' else area end) as area, cast(sum(a.venta_uds_anio_actual) as dec (10,0)) as Vta_uds_AAct, cast((sum(a.venta_uds_anio_actual)-sum(a.venta_uds_anio_anterior))/sum(a.venta_uds_anio_anterior) as dec (10,4)) as Dif_uds_perc, cast(sum(a.venta_neta_anio_actual) as dec(10,0)) as Vta_AAct, cast(sum(a.venta_neta_anio_anterior) as dec(10,0)) as Vta_AAnt, cast(sum(a.venta_neta_anio_actual)-sum(a.venta_neta_anio_anterior) as dec(10,0)) as Dif_vta, cast((sum(a.venta_neta_anio_actual)-sum(a.venta_neta_anio_anterior))/sum(a.venta_neta_anio_anterior) as dec (10,4)) as Dif_vta_per, cast(sum(a.margen_anio_actual) as dec(10,0)) as Mg_AAct, cast(sum(a.margen_anio_anterior) as dec(10,0)) as Mg_AAnt, cast(sum(a.margen_anio_actual)-sum(a.margen_anio_anterior) as dec(10,0)) as Dif_Mg, cast((sum(a.margen_anio_actual)-sum(a.margen_anio_anterior))/sum(a.margen_anio_anterior) as dec (10,4)) as Dif_Mg_per, cast(sum(a.margen_anio_actual)/sum(a.venta_neta_anio_actual) as dec (10,4)) as GR_AAct, cast(sum(a.margen_anio_anterior)/sum(a.venta_neta_anio_anterior) as dec (10,4)) as GR_AAnt, cast(sum(a.stock_unidades_actual) as dec(10,0)) as St_uds_Aact, cast(sum(a.stock_unidades_actual)-sum(a.stock_unidades_anterior) as dec(10,0)) as Dif_St_uds, cast((sum(a.stock_unidades_actual)-sum(a.stock_unidades_anterior))/sum(a.stock_unidades_anterior) as dec (10,4)) as Dif_St_uds_per, cast(sum(a.stock_anio_actual) as dec(10,0)) as St_AAct_tot, cast(sum(a.stock_anio_anterior) as dec (10,0)) as St_AAnt_tot, cast(sum(a.stock_anio_actual)-sum(a.stock_anio_anterior) as dec (10,0)) as Dif_St, cast((sum(a.stock_anio_actual)-sum(a.stock_anio_anterior))/sum(a.stock_anio_anterior) as dec (10,4)) as Dif_St_per, cast(sum(a.stock_unidades_actual)/sum(a.venta_uds_ultima_semana_anio_actual) as dec(10,2)) as cob_AAct, cast(sum(a.stock_unidades_anterior)/sum(a.venta_uds_ultima_semana_anio_anterior) as dec(10,2)) as cob_AAnt from (select mercado, branch_no, area, sum(venta_uds_anio_actual) as venta_uds_anio_actual, sum(venta_uds_anio_anterior) as venta_uds_anio_anterior, sum(venta_neta_anio_actual) as venta_neta_anio_actual, sum(venta_neta_anio_anterior) as venta_neta_anio_anterior, sum(margen_anio_actual) as margen_anio_actual, sum(margen_anio_anterior) as margen_anio_anterior, sum(stock_unidades_actual) as stock_unidades_actual, sum(stock_unidades_anterior) as stock_unidades_anterior, sum(stock_anio_actual) as stock_anio_actual, sum(stock_anio_anterior) as stock_anio_anterior, sum(venta_uds_ultima_semana_anio_actual) as venta_uds_ultima_semana_anio_actual, sum(venta_uds_ultima_semana_anio_anterior) as venta_uds_ultima_semana_anio_anterior from ventas_stock_stg WHERE EsTienda=1 and cast(semana as unsigned) between SEMANA_INICIAL_MES_JD and SEMANA_FINAL group by branch_no,area ) a group by branch_no,(case when area in ('Complementos','Material técnico') then 'Complementos y Técnico' when area in ('Promociones','Servicios','Otros','na') then 'Otros' else area end) order by branch_no,Vta_AAct desc; drop table if exists tienda_area_subtotal; create table tienda_area_subtotal as select 'SUBTOTAL' as totales, a.mercado, a.branch_no, '' as area, cast(sum(a.venta_uds_anio_actual) as dec (10,0)) as Vta_uds_AAct, cast((sum(a.venta_uds_anio_actual)-sum(a.venta_uds_anio_anterior))/sum(a.venta_uds_anio_anterior) as dec (10,4)) as Dif_uds_perc, cast(sum(a.venta_neta_anio_actual) as dec(10,0)) as Vta_AAct, cast(sum(a.venta_neta_anio_anterior) as dec(10,0)) as Vta_AAnt, cast(sum(a.venta_neta_anio_actual)-sum(a.venta_neta_anio_anterior) as dec(10,0)) as Dif_vta, cast((sum(a.venta_neta_anio_actual)-sum(a.venta_neta_anio_anterior))/sum(a.venta_neta_anio_anterior) as dec (10,4)) as Dif_vta_per, cast(sum(a.margen_anio_actual) as dec(10,0)) as Mg_AAct, cast(sum(a.margen_anio_anterior) as dec(10,0)) as Mg_AAnt, cast(sum(a.margen_anio_actual)-sum(a.margen_anio_anterior) as dec(10,0)) as Dif_Mg, cast((sum(a.margen_anio_actual)-sum(a.margen_anio_anterior))/sum(a.margen_anio_anterior) as dec (10,4)) as Dif_Mg_per, cast(sum(a.margen_anio_actual)/sum(a.venta_neta_anio_actual) as dec (10,4)) as GR_AAct, cast(sum(a.margen_anio_anterior)/sum(a.venta_neta_anio_anterior) as dec (10,4)) as GR_AAnt, cast(sum(a.stock_unidades_actual) as dec(10,0)) as St_uds_Aact, cast(sum(a.stock_unidades_actual)-sum(a.stock_unidades_anterior) as dec(10,0)) as Dif_St_uds, cast((sum(a.stock_unidades_actual)-sum(a.stock_unidades_anterior))/sum(a.stock_unidades_anterior) as dec (10,4)) as Dif_St_uds_per, cast(sum(a.stock_anio_actual) as dec(10,0)) as St_AAct_tot, cast(sum(a.stock_anio_anterior) as dec (10,0)) as St_AAnt_tot, cast(sum(a.stock_anio_actual)-sum(a.stock_anio_anterior) as dec (10,0)) as Dif_St, cast((sum(a.stock_anio_actual)-sum(a.stock_anio_anterior))/sum(a.stock_anio_anterior) as dec (10,4)) as Dif_St_per, cast(sum(a.stock_unidades_actual)/sum(a.venta_uds_ultima_semana_anio_actual) as dec(10,2)) as cob_AAct, cast(sum(a.stock_unidades_anterior)/sum(a.venta_uds_ultima_semana_anio_anterior) as dec(10,2)) as cob_AAnt from (select mercado, branch_no, sum(venta_uds_anio_actual) as venta_uds_anio_actual, sum(venta_uds_anio_anterior) as venta_uds_anio_anterior, sum(venta_neta_anio_actual) as venta_neta_anio_actual, sum(venta_neta_anio_anterior) as venta_neta_anio_anterior, sum(margen_anio_actual) as margen_anio_actual, sum(margen_anio_anterior) as margen_anio_anterior, sum(stock_unidades_actual) as stock_unidades_actual, sum(stock_unidades_anterior) as stock_unidades_anterior, sum(stock_anio_actual) as stock_anio_actual, sum(stock_anio_anterior) as stock_anio_anterior, sum(venta_uds_ultima_semana_anio_actual) as venta_uds_ultima_semana_anio_actual, sum(venta_uds_ultima_semana_anio_anterior) as venta_uds_ultima_semana_anio_anterior from ventas_stock_stg WHERE EsTienda=1 and cast(semana as unsigned) between SEMANA_INICIAL_MES_JD and SEMANA_FINAL group by branch_no ) a group by branch_no order by Vta_AAct; insert into tienda_area select * from tienda_area_subtotal; drop table if exists tienda_area_1; create table tienda_area_1 as select a.*, replace(concat(format(((a.Vta_AAct)/(b.Vta_AAct))*100,2),'%'),'.',',') as peso_1, replace(concat(format(((a.Mg_AAct)/(b.Mg_AAct))*100,2),'%'),'.',',') as peso_2, replace(concat(format(((a.St_AAct_tot)/(b.St_AAct_tot))*100,2),'%'),'.',',') as peso_3, replace(cast(((a.Vta_AAct/b.Vta_AAct)/(a.St_AAct_tot/b.St_AAct_tot)) as dec(10,2)),'.',',') as peso_4, replace(cast(((a.Mg_AAct/b.Mg_AAct)/(a.St_AAct_tot/b.St_AAct_tot)) as dec(10,2)),'.',',') as peso_5 from tienda_area a left join tienda_area_subtotal b on a.branch_no=b.branch_no order by branch_no,totales,Vta_AAct desc; drop table if exists tienda_area_total; create table tienda_area_total as select 'TOTAL' as totales, '' as mercado, '' as branch_no, '' as area, cast(sum(a.venta_uds_anio_actual) as dec (10,0)) as Vta_uds_AAct, cast((sum(a.venta_uds_anio_actual)-sum(a.venta_uds_anio_anterior))/sum(a.venta_uds_anio_anterior) as dec (10,4)) as Dif_uds_perc, cast(sum(a.venta_neta_anio_actual) as dec(10,0)) as Vta_AAct, cast(sum(a.venta_neta_anio_anterior) as dec(10,0)) as Vta_AAnt, cast(sum(a.venta_neta_anio_actual)-sum(a.venta_neta_anio_anterior) as dec(10,0)) as Dif_vta, cast((sum(a.venta_neta_anio_actual)-sum(a.venta_neta_anio_anterior))/sum(a.venta_neta_anio_anterior) as dec (10,4)) as Dif_vta_per, cast(sum(a.margen_anio_actual) as dec(10,0)) as Mg_AAct, cast(sum(a.margen_anio_anterior) as dec(10,0)) as Mg_AAnt, cast(sum(a.margen_anio_actual)-sum(a.margen_anio_anterior) as dec(10,0)) as Dif_Mg, cast((sum(a.margen_anio_actual)-sum(a.margen_anio_anterior))/sum(a.margen_anio_anterior) as dec (10,4)) as Dif_Mg_per, cast(sum(a.margen_anio_actual)/sum(a.venta_neta_anio_actual) as dec (10,4)) as GR_AAct, cast(sum(a.margen_anio_anterior)/sum(a.venta_neta_anio_anterior) as dec (10,4)) as GR_AAnt, cast(sum(a.stock_unidades_actual) as dec(10,0)) as St_uds_Aact, cast(sum(a.stock_unidades_actual)-sum(a.stock_unidades_anterior) as dec(10,0)) as Dif_St_uds, cast((sum(a.stock_unidades_actual)-sum(a.stock_unidades_anterior))/sum(a.stock_unidades_anterior) as dec (10,4)) as Dif_St_uds_per, cast(sum(a.stock_anio_actual) as dec(10,0)) as St_AAct_tot, cast(sum(a.stock_anio_anterior) as dec (10,0)) as St_AAnt_tot, cast(sum(a.stock_anio_actual)-sum(a.stock_anio_anterior) as dec (10,0)) as Dif_St, cast((sum(a.stock_anio_actual)-sum(a.stock_anio_anterior))/sum(a.stock_anio_anterior) as dec (10,4)) as Dif_St_per, cast(sum(a.stock_unidades_actual)/sum(a.venta_uds_ultima_semana_anio_actual) as dec(10,2)) as cob_AAct, cast(sum(a.stock_unidades_anterior)/sum(a.venta_uds_ultima_semana_anio_anterior) as dec(10,2)) as cob_AAnt, '' as peso_1, '' as peso_2, '' as peso_3, '' as peso_4, '' as peso_5 from ( select sum(venta_uds_anio_actual) as venta_uds_anio_actual, sum(venta_uds_anio_anterior) as venta_uds_anio_anterior, sum(venta_neta_anio_actual) as venta_neta_anio_actual, sum(venta_neta_anio_anterior) as venta_neta_anio_anterior, sum(margen_anio_actual) as margen_anio_actual, sum(margen_anio_anterior) as margen_anio_anterior, sum(stock_unidades_actual) as stock_unidades_actual, sum(stock_unidades_anterior) as stock_unidades_anterior, sum(stock_anio_actual) as stock_anio_actual, sum(stock_anio_anterior) as stock_anio_anterior, sum(venta_uds_ultima_semana_anio_actual) as venta_uds_ultima_semana_anio_actual, sum(venta_uds_ultima_semana_anio_anterior) as venta_uds_ultima_semana_anio_anterior from ventas_stock_stg WHERE EsTienda=1 and cast(semana as unsigned) between SEMANA_INICIAL_MES_JD and SEMANA_FINAL ) a ; insert into tienda_area_1 select * from tienda_area_total; drop table if exists tienda_area_2; create table tienda_area_2 as select a.*, concat(a.branch_no,' ',b.nombre_tienda) as TIENDA from tienda_area_1 a left join tienda_zona_territorial_tipo_tienda b on a.branch_no=b.branch_no; drop table if exists jd_tienda_area_mensual; create table jd_tienda_area_mensual as select a.totales, a.mercado as Market, a.TIENDA, a.area, REPLACE(FORMAT(a.Vta_uds_AAct,0),',','.') as Vta_uds_AAct, replace(CONCAT(FORMAT(a.Dif_uds_perc*100,2),'%'),'.',',') as Dif_uds_perc, REPLACE(FORMAT(a.Vta_AAct,0),',','.') as Vta_AAct, REPLACE(FORMAT(a.Vta_AAnt, 0),',','.') as Vta_AAnt, REPLACE(FORMAT(a.Dif_vta,0),',','.') as Dif_vta, replace(CONCAT(FORMAT(a.Dif_vta_per*100,2),'%'),'.',',') as Dif_vta_per, REPLACE(FORMAT(a.Mg_AAct,0),',','.') as Mg_AAct, REPLACE(FORMAT(a.Mg_AAnt,0),',','.') as Mg_AAnt, REPLACE(FORMAT(a.Dif_Mg, 0),',','.') as Dif_Mg, replace(CONCAT(FORMAT(a.Dif_Mg_per*100,2),'%'),'.',',') as Dif_Mg_per, replace(CONCAT(FORMAT(a.GR_AAct*100,2),'%'),'.',',') as GR_AAct, replace(CONCAT(FORMAT(a.GR_AAnt*100,2),'%'),'.',',') as GR_AAnt, REPLACE(FORMAT(a.St_uds_Aact,0),',','.') as St_uds_Aact, REPLACE(FORMAT(a.Dif_St_uds,0),',','.') as Dif_St_uds, replace(CONCAT(FORMAT(a.Dif_St_uds_per*100,2),'%'),'.',',') as Dif_St_uds_per, REPLACE(FORMAT(a.St_AAct_tot,0),',','.') as St_AAct_tot, REPLACE(FORMAT(a.St_AAnt_tot,0),',','.') as St_AAnt_tot, REPLACE(FORMAT(a.Dif_St,0),',','.') as Dif_St, replace(CONCAT(FORMAT(a.Dif_St_per*100,2),'%'),'.',',') as Dif_St_per, replace(cast(a.cob_AAct as dec(10,2)),'.',',') as cob_AAct, replace(cast(a.cob_AAnt as dec(10,2)),'.',',') as cob_AAnt, (case when a.totales in ('SUBTOTAL','TOTAL') then replace(cast(((a.Vta_AAct/b.Vta_AAct)/(a.St_AAct_tot/b.St_AAct_tot)) as dec (10,2)),'.',',') else a.peso_4 end) as vta_sobre_stock, (case when a.totales in ('SUBTOTAL','TOTAL') then replace(cast(((a.Mg_AAct/b.Mg_AAct)/(a.St_AAct_tot/b.St_AAct_tot)) as dec(10,2)),'.',',') else a.peso_5 end) as mg_sobre_stock, (case when a.totales in ('SUBTOTAL','TOTAL') then replace(concat(format(((a.Vta_AAct)/(b.Vta_AAct))*100,2),'%'),'.',',') else a.peso_1 end) as Mix_vtas, (case when a.totales in ('SUBTOTAL','TOTAL') then replace(concat(format(((a.Mg_AAct)/(b.Mg_AAct))*100,2),'%'),'.',',') else a.peso_2 end) as Mix_Mg, (case when a.totales in ('SUBTOTAL','TOTAL') then replace(concat(format(((a.St_AAct_tot)/(b.St_AAct_tot))*100,2),'%'),'.',',') else a.peso_3 end) as Mix_Stock from tienda_area_2 a left join tienda_area_total b on a.totales in ('SUBTOTAL','TOTAL'); /* eliminamos tablas temporales */ drop table if exists tienda_area_1; drop table if exists tienda_area_2; drop table if exists tienda_area; drop table if exists tienda_area_subtotal; drop table if exists tienda_area_total; end if; -- ---------------------------------------------------------------------------------------------------- /* TIENDA-AREA APP */ -- ---------------------------------------------------------------------------------------------------- -- Borramos la tabla tienda_area_app_stg si nos encontramos en la semana 1 del año actual jd if SEMANA_INICIAL = 1 then truncate table tienda_area_app_stg; else delete from tienda_area_app_stg where cast(semana as unsigned) between SEMANA_INICIAL and SEMANA_FINAL; end if; -- -> Crear tabla intermedia definitiva (no volatil) que guarde la información a usar por semana y/o fecha. insert into tienda_area_app_stg (semana, dia, product_code, branch_no, nombre_tienda, tipo_tienda, subzona, delegado, zona, territorial, venta_uds_anio_actual, venta_uds_anio_anterior, venta_neta_anio_actual, venta_neta_anio_anterior, venta_bruta_anio_actual, venta_bruta_anio_anterior, coste_anio_actual, coste_anio_anterior, margen_anio_actual, margen_anio_anterior, stock_anio_actual, stock_anio_anterior, stock_unidades_actual, stock_unidades_anterior, venta_uds_ultima_semana_anio_actual, venta_uds_ultima_semana_anio_anterior, venta_neta_ultima_semana_anio_actual, venta_neta_ultima_semana_anio_anterior, margen_ultima_semana_anio_actual, margen_ultima_semana_anio_anterior, venta_uds_anio_actual_lfl, venta_uds_anio_anterior_lfl, venta_neta_anio_actual_lfl, venta_bruta_anio_actual_lfl, venta_neta_anio_anterior_lfl, venta_bruta_anio_anterior_lfl, margen_anio_actual_lfl, margen_anio_anterior_lfl, stock_anio_actual_alm, stock_anio_anterior_alm, stock_uds_anio_actual_alm, stock_uds_anio_anterior_alm, descripcion, precio_original, area, serie, serie1, seccion_a, subseccion_a, seccion, familia, disenyo, marca, udp_code, udp_name, brand_type, brand_desc, Temporada, analytical_color, TEMPORALIDAD, DESCATALOGADO, EsTienda, Mercado, fare_code, mes, precio_actual) select semana, dia, product_code, branch_no, nombre_tienda, tipo_tienda, subzona, delegado, zona, territorial, venta_uds_anio_actual, venta_uds_anio_anterior, venta_neta_anio_actual, venta_neta_anio_anterior, venta_bruta_anio_actual, venta_bruta_anio_anterior, coste_anio_actual, coste_anio_anterior, margen_anio_actual, margen_anio_anterior, stock_anio_actual, stock_anio_anterior, stock_unidades_actual, stock_unidades_anterior, venta_uds_ultima_semana_anio_actual, venta_uds_ultima_semana_anio_anterior, venta_neta_ultima_semana_anio_actual, venta_neta_ultima_semana_anio_anterior, margen_ultima_semana_anio_actual, margen_ultima_semana_anio_anterior, venta_uds_anio_actual_lfl, venta_uds_anio_anterior_lfl, venta_neta_anio_actual_lfl, venta_bruta_anio_actual_lfl, venta_neta_anio_anterior_lfl, venta_bruta_anio_anterior_lfl, margen_anio_actual_lfl, margen_anio_anterior_lfl, stock_anio_actual_alm, stock_anio_anterior_alm, stock_uds_anio_actual_alm, stock_uds_anio_anterior_alm, descripcion, precio_original, area, serie, serie1, seccion_a, subseccion_a, seccion, familia, disenyo, marca, udp_code, udp_name, brand_type, brand_desc, Temporada, analytical_color, TEMPORALIDAD, DESCATALOGADO, EsTienda, Mercado, fare_code, mes, precio_actual from VENTAS_STOCK_APP; /* jd_tienda_area_app SEMANAL */ drop table if exists tienda_area_app; create table tienda_area_app as select ' ' as totales, mercado, branch_no, (case when area in ('Complementos','Material técnico') then 'Complementos y Técnico' when area in ('Promociones','Servicios','Otros','na') then 'Otros' else area end) as area, cast(sum(venta_uds_anio_actual) as dec (10,0)) as Vta_uds_AAct, cast((sum(venta_uds_anio_actual)-sum(venta_uds_anio_anterior))/sum(venta_uds_anio_anterior) as dec (10,4)) as Dif_uds_perc, cast(sum(venta_neta_anio_actual) as dec(10,0)) as Vta_AAct, cast(sum(venta_neta_anio_anterior) as dec(10,0)) as Vta_AAnt, cast(sum(venta_neta_anio_actual)-sum(venta_neta_anio_anterior) as dec(10,0)) as Dif_vta, cast((sum(venta_neta_anio_actual)-sum(venta_neta_anio_anterior))/sum(venta_neta_anio_anterior) as dec (10,4)) as Dif_vta_per, cast(sum(margen_anio_actual) as dec(10,0)) as Mg_AAct, cast(sum(margen_anio_anterior) as dec(10,0)) as Mg_AAnt, cast(sum(margen_anio_actual)-sum(margen_anio_anterior) as dec(10,0)) as Dif_Mg, cast((sum(margen_anio_actual)-sum(margen_anio_anterior))/sum(margen_anio_anterior) as dec (10,4)) as Dif_Mg_per, cast(sum(margen_anio_actual)/sum(venta_neta_anio_actual) as dec (10,4)) as GR_AAct, cast(sum(margen_anio_anterior)/sum(venta_neta_anio_anterior) as dec (10,4)) as GR_AAnt, cast(sum(stock_unidades_actual) as dec(10,0)) as St_uds_Aact, cast(sum(stock_unidades_actual)-sum(stock_unidades_anterior) as dec(10,0)) as Dif_St_uds, cast((sum(stock_unidades_actual)-sum(stock_unidades_anterior))/sum(stock_unidades_anterior) as dec (10,4)) as Dif_St_uds_per, cast(sum(stock_anio_actual) as dec(10,0)) as St_AAct_tot, cast(sum(stock_anio_anterior) as dec (10,0)) as St_AAnt_tot, cast(sum(stock_anio_actual)-sum(stock_anio_anterior) as dec (10,0)) as Dif_St, cast((sum(stock_anio_actual)-sum(stock_anio_anterior))/sum(stock_anio_anterior) as dec (10,4)) as Dif_St_per, cast(sum(stock_unidades_actual)/sum(venta_uds_ultima_semana_anio_actual) as dec(10,2)) as cob_AAct, cast(sum(stock_unidades_anterior)/sum(venta_uds_ultima_semana_anio_anterior) as dec(10,2)) as cob_AAnt from tienda_area_app_stg WHERE estienda=1 and cast(semana as unsigned) = SEMANA_FINAL group by branch_no, (case when area in ('Complementos','Material técnico') then 'Complementos y Técnico' when area in ('Promociones','Servicios','Otros','na') then 'Otros' else area end) order by branch_no,Vta_AAct desc; drop table if exists tienda_area_subtotal_app; create table tienda_area_subtotal_app as select 'SUBTOTAL' as totales, mercado, branch_no, '' as area, cast(sum(venta_uds_anio_actual) as dec (10,0)) as Vta_uds_AAct, cast((sum(venta_uds_anio_actual)-sum(venta_uds_anio_anterior))/sum(venta_uds_anio_anterior) as dec (10,4)) as Dif_uds_perc, cast(sum(venta_neta_anio_actual) as dec(10,0)) as Vta_AAct, cast(sum(venta_neta_anio_anterior) as dec(10,0)) as Vta_AAnt, cast(sum(venta_neta_anio_actual)-sum(venta_neta_anio_anterior) as dec(10,0)) as Dif_vta, cast((sum(venta_neta_anio_actual)-sum(venta_neta_anio_anterior))/sum(venta_neta_anio_anterior) as dec (10,4)) as Dif_vta_per, cast(sum(margen_anio_actual) as dec(10,0)) as Mg_AAct, cast(sum(margen_anio_anterior) as dec(10,0)) as Mg_AAnt, cast(sum(margen_anio_actual)-sum(margen_anio_anterior) as dec(10,0)) as Dif_Mg, cast((sum(margen_anio_actual)-sum(margen_anio_anterior))/sum(margen_anio_anterior) as dec (10,4)) as Dif_Mg_per, cast(sum(margen_anio_actual)/sum(venta_neta_anio_actual) as dec (10,4)) as GR_AAct, cast(sum(margen_anio_anterior)/sum(venta_neta_anio_anterior) as dec (10,4)) as GR_AAnt, cast(sum(stock_unidades_actual) as dec(10,0)) as St_uds_Aact, cast(sum(stock_unidades_actual)-sum(stock_unidades_anterior) as dec(10,0)) as Dif_St_uds, cast((sum(stock_unidades_actual)-sum(stock_unidades_anterior))/sum(stock_unidades_anterior) as dec (10,4)) as Dif_St_uds_per, cast(sum(stock_anio_actual) as dec(10,0)) as St_AAct_tot, cast(sum(stock_anio_anterior) as dec (10,0)) as St_AAnt_tot, cast(sum(stock_anio_actual)-sum(stock_anio_anterior) as dec (10,0)) as Dif_St, cast((sum(stock_anio_actual)-sum(stock_anio_anterior))/sum(stock_anio_anterior) as dec (10,4)) as Dif_St_per, cast(sum(stock_unidades_actual)/sum(venta_uds_ultima_semana_anio_actual) as dec(10,2)) as cob_AAct, cast(sum(stock_unidades_anterior)/sum(venta_uds_ultima_semana_anio_anterior) as dec(10,2)) as cob_AAnt from tienda_area_app_stg WHERE estienda=1 and cast(semana as unsigned) = SEMANA_FINAL group by branch_no order by Vta_AAct; insert into tienda_area_app select * from tienda_area_subtotal_app; drop table if exists tienda_area_1_app; create table tienda_area_1_app as select a.*, replace(concat(format(((a.Vta_AAct)/(b.Vta_AAct))*100,2),'%'),'.',',') as peso_1, replace(concat(format(((a.Mg_AAct)/(b.Mg_AAct))*100,2),'%'),'.',',') as peso_2, replace(concat(format(((a.St_AAct_tot)/(b.St_AAct_tot))*100,2),'%'),'.',',') as peso_3, replace(cast(((a.Vta_AAct/b.Vta_AAct)/(a.St_AAct_tot/b.St_AAct_tot)) as dec(10,2)),'.',',') as peso_4, replace(cast(((a.Mg_AAct/b.Mg_AAct)/(a.St_AAct_tot/b.St_AAct_tot)) as dec(10,2)),'.',',') as peso_5 from tienda_area_app a left join tienda_area_subtotal_app b on a.branch_no=b.branch_no order by branch_no,totales,Vta_AAct desc; drop table if exists tienda_area_total_app; create table tienda_area_total_app as select 'TOTAL' as totales, '' as mercado, '' as branch_no, '' as area, cast(sum(venta_uds_anio_actual) as dec (10,0)) as Vta_uds_AAct, cast((sum(venta_uds_anio_actual)-sum(venta_uds_anio_anterior))/sum(venta_uds_anio_anterior) as dec (10,4)) as Dif_uds_perc, cast(sum(venta_neta_anio_actual) as dec(10,0)) as Vta_AAct, cast(sum(venta_neta_anio_anterior) as dec(10,0)) as Vta_AAnt, cast(sum(venta_neta_anio_actual)-sum(venta_neta_anio_anterior) as dec(10,0)) as Dif_vta, cast((sum(venta_neta_anio_actual)-sum(venta_neta_anio_anterior))/sum(venta_neta_anio_anterior) as dec (10,4)) as Dif_vta_per, cast(sum(margen_anio_actual) as dec(10,0)) as Mg_AAct, cast(sum(margen_anio_anterior) as dec(10,0)) as Mg_AAnt, cast(sum(margen_anio_actual)-sum(margen_anio_anterior) as dec(10,0)) as Dif_Mg, cast((sum(margen_anio_actual)-sum(margen_anio_anterior))/sum(margen_anio_anterior) as dec (10,4)) as Dif_Mg_per, cast(sum(margen_anio_actual)/sum(venta_neta_anio_actual) as dec (10,4)) as GR_AAct, cast(sum(margen_anio_anterior)/sum(venta_neta_anio_anterior) as dec (10,4)) as GR_AAnt, cast(sum(stock_unidades_actual) as dec(10,0)) as St_uds_Aact, cast(sum(stock_unidades_actual)-sum(stock_unidades_anterior) as dec(10,0)) as Dif_St_uds, cast((sum(stock_unidades_actual)-sum(stock_unidades_anterior))/sum(stock_unidades_anterior) as dec (10,4)) as Dif_St_uds_per, cast(sum(stock_anio_actual) as dec(10,0)) as St_AAct_tot, cast(sum(stock_anio_anterior) as dec (10,0)) as St_AAnt_tot, cast(sum(stock_anio_actual)-sum(stock_anio_anterior) as dec (10,0)) as Dif_St, cast((sum(stock_anio_actual)-sum(stock_anio_anterior))/sum(stock_anio_anterior) as dec (10,4)) as Dif_St_per, cast(sum(stock_unidades_actual)/sum(venta_uds_ultima_semana_anio_actual) as dec(10,2)) as cob_AAct, cast(sum(stock_unidades_anterior)/sum(venta_uds_ultima_semana_anio_anterior) as dec(10,2)) as cob_AAnt, '' as peso_1, '' as peso_2, '' as peso_3, '' as peso_4, '' as peso_5 from tienda_area_app_stg WHERE estienda=1 and cast(semana as unsigned) = SEMANA_FINAL; insert into tienda_area_1_app select * from tienda_area_total_app; drop table if exists tienda_area_2_app; create table tienda_area_2_app as select a.*, concat(a.branch_no,' ',b.nombre_tienda) as TIENDA from tienda_area_1_app a left join tienda_zona_territorial_tipo_tienda b on a.branch_no=b.branch_no; drop table if exists jd_tienda_area_app_semanal; create table jd_tienda_area_app_semanal as select a.totales, a.mercado as Market, a.TIENDA, a.area, REPLACE(FORMAT(a.Vta_uds_AAct,0),',','.') as Vta_uds_AAct, replace(CONCAT(FORMAT(a.Dif_uds_perc*100,2),'%'),'.',',') as Dif_uds_perc, REPLACE(FORMAT(a.Vta_AAct,0),',','.') as Vta_AAct, REPLACE(FORMAT(a.Vta_AAnt, 0),',','.') as Vta_AAnt, REPLACE(FORMAT(a.Dif_vta,0),',','.') as Dif_vta, replace(CONCAT(FORMAT(a.Dif_vta_per*100,2),'%'),'.',',') as Dif_vta_per, REPLACE(FORMAT(a.Mg_AAct,0),',','.') as Mg_AAct, REPLACE(FORMAT(a.Mg_AAnt,0),',','.') as Mg_AAnt, REPLACE(FORMAT(a.Dif_Mg, 0),',','.') as Dif_Mg, replace(CONCAT(FORMAT(a.Dif_Mg_per*100,2),'%'),'.',',') as Dif_Mg_per, replace(CONCAT(FORMAT(a.GR_AAct*100,2),'%'),'.',',') as GR_AAct, replace(CONCAT(FORMAT(a.GR_AAnt*100,2),'%'),'.',',') as GR_AAnt, REPLACE(FORMAT(a.St_uds_Aact,0),',','.') as St_uds_Aact, REPLACE(FORMAT(a.Dif_St_uds,0),',','.') as Dif_St_uds, replace(CONCAT(FORMAT(a.Dif_St_uds_per*100,2),'%'),'.',',') as Dif_St_uds_per, REPLACE(FORMAT(a.St_AAct_tot,0),',','.') as St_AAct_tot, REPLACE(FORMAT(a.St_AAnt_tot,0),',','.') as St_AAnt_tot, REPLACE(FORMAT(a.Dif_St,0),',','.') as Dif_St, replace(CONCAT(FORMAT(a.Dif_St_per*100,2),'%'),'.',',') as Dif_St_per, replace(cast(a.cob_AAct as dec(10,2)),'.',',') as cob_AAct, replace(cast(a.cob_AAnt as dec(10,2)),'.',',') as cob_AAnt, (case when a.totales in ('SUBTOTAL','TOTAL') then replace(cast(((a.Vta_AAct/b.Vta_AAct)/(a.St_AAct_tot/b.St_AAct_tot)) as dec (10,2)),'.',',') else a.peso_4 end) as vta_sobre_stock, (case when a.totales in ('SUBTOTAL','TOTAL') then replace(cast(((a.Mg_AAct/b.Mg_AAct)/(a.St_AAct_tot/b.St_AAct_tot)) as dec(10,2)),'.',',') else a.peso_5 end) as mg_sobre_stock, (case when a.totales in ('SUBTOTAL','TOTAL') then replace(concat(format(((a.Vta_AAct)/(b.Vta_AAct))*100,2),'%'),'.',',') else a.peso_1 end) as Mix_vtas, (case when a.totales in ('SUBTOTAL','TOTAL') then replace(concat(format(((a.Mg_AAct)/(b.Mg_AAct))*100,2),'%'),'.',',') else a.peso_2 end) as Mix_Mg, (case when a.totales in ('SUBTOTAL','TOTAL') then replace(concat(format(((a.St_AAct_tot)/(b.St_AAct_tot))*100,2),'%'),'.',',') else a.peso_3 end) as Mix_Stock from tienda_area_2_app a left join tienda_area_total_app b on a.totales in ('SUBTOTAL','TOTAL'); /* eliminamos tablas temporales */ drop table if exists tienda_area_1_app; drop table if exists tienda_area_2_app; drop table if exists tienda_area_app; drop table if exists tienda_area_subtotal_app; drop table if exists tienda_area_total_app; /* jd_tienda_area_app ACUMULADO */ drop table if exists tienda_area_app; create table tienda_area_app as select ' ' as totales, mercado, branch_no, (case when area in ('Complementos','Material técnico') then 'Complementos y Técnico' when area in ('Promociones','Servicios','Otros','na') then 'Otros' else area end) as area, cast(sum(venta_uds_anio_actual) as dec (10,0)) as Vta_uds_AAct, cast((sum(venta_uds_anio_actual)-sum(venta_uds_anio_anterior))/sum(venta_uds_anio_anterior) as dec (10,4)) as Dif_uds_perc, cast(sum(venta_neta_anio_actual) as dec(10,0)) as Vta_AAct, cast(sum(venta_neta_anio_anterior) as dec(10,0)) as Vta_AAnt, cast(sum(venta_neta_anio_actual)-sum(venta_neta_anio_anterior) as dec(10,0)) as Dif_vta, cast((sum(venta_neta_anio_actual)-sum(venta_neta_anio_anterior))/sum(venta_neta_anio_anterior) as dec (10,4)) as Dif_vta_per, cast(sum(margen_anio_actual) as dec(10,0)) as Mg_AAct, cast(sum(margen_anio_anterior) as dec(10,0)) as Mg_AAnt, cast(sum(margen_anio_actual)-sum(margen_anio_anterior) as dec(10,0)) as Dif_Mg, cast((sum(margen_anio_actual)-sum(margen_anio_anterior))/sum(margen_anio_anterior) as dec (10,4)) as Dif_Mg_per, cast(sum(margen_anio_actual)/sum(venta_neta_anio_actual) as dec (10,4)) as GR_AAct, cast(sum(margen_anio_anterior)/sum(venta_neta_anio_anterior) as dec (10,4)) as GR_AAnt, cast(sum(stock_unidades_actual) as dec(10,0)) as St_uds_Aact, cast(sum(stock_unidades_actual)-sum(stock_unidades_anterior) as dec(10,0)) as Dif_St_uds, cast((sum(stock_unidades_actual)-sum(stock_unidades_anterior))/sum(stock_unidades_anterior) as dec (10,4)) as Dif_St_uds_per, cast(sum(stock_anio_actual) as dec(10,0)) as St_AAct_tot, cast(sum(stock_anio_anterior) as dec (10,0)) as St_AAnt_tot, cast(sum(stock_anio_actual)-sum(stock_anio_anterior) as dec (10,0)) as Dif_St, cast((sum(stock_anio_actual)-sum(stock_anio_anterior))/sum(stock_anio_anterior) as dec (10,4)) as Dif_St_per, cast(sum(stock_unidades_actual)/sum(venta_uds_ultima_semana_anio_actual) as dec(10,2)) as cob_AAct, cast(sum(stock_unidades_anterior)/sum(venta_uds_ultima_semana_anio_anterior) as dec(10,2)) as cob_AAnt from tienda_area_app_stg WHERE estienda=1 group by branch_no, (case when area in ('Complementos','Material técnico') then 'Complementos y Técnico' when area in ('Promociones','Servicios','Otros','na') then 'Otros' else area end) order by branch_no,Vta_AAct desc; drop table if exists tienda_area_subtotal_app; create table tienda_area_subtotal_app as select 'SUBTOTAL' as totales, mercado, branch_no, '' as area, cast(sum(venta_uds_anio_actual) as dec (10,0)) as Vta_uds_AAct, cast((sum(venta_uds_anio_actual)-sum(venta_uds_anio_anterior))/sum(venta_uds_anio_anterior) as dec (10,4)) as Dif_uds_perc, cast(sum(venta_neta_anio_actual) as dec(10,0)) as Vta_AAct, cast(sum(venta_neta_anio_anterior) as dec(10,0)) as Vta_AAnt, cast(sum(venta_neta_anio_actual)-sum(venta_neta_anio_anterior) as dec(10,0)) as Dif_vta, cast((sum(venta_neta_anio_actual)-sum(venta_neta_anio_anterior))/sum(venta_neta_anio_anterior) as dec (10,4)) as Dif_vta_per, cast(sum(margen_anio_actual) as dec(10,0)) as Mg_AAct, cast(sum(margen_anio_anterior) as dec(10,0)) as Mg_AAnt, cast(sum(margen_anio_actual)-sum(margen_anio_anterior) as dec(10,0)) as Dif_Mg, cast((sum(margen_anio_actual)-sum(margen_anio_anterior))/sum(margen_anio_anterior) as dec (10,4)) as Dif_Mg_per, cast(sum(margen_anio_actual)/sum(venta_neta_anio_actual) as dec (10,4)) as GR_AAct, cast(sum(margen_anio_anterior)/sum(venta_neta_anio_anterior) as dec (10,4)) as GR_AAnt, cast(sum(stock_unidades_actual) as dec(10,0)) as St_uds_Aact, cast(sum(stock_unidades_actual)-sum(stock_unidades_anterior) as dec(10,0)) as Dif_St_uds, cast((sum(stock_unidades_actual)-sum(stock_unidades_anterior))/sum(stock_unidades_anterior) as dec (10,4)) as Dif_St_uds_per, cast(sum(stock_anio_actual) as dec(10,0)) as St_AAct_tot, cast(sum(stock_anio_anterior) as dec (10,0)) as St_AAnt_tot, cast(sum(stock_anio_actual)-sum(stock_anio_anterior) as dec (10,0)) as Dif_St, cast((sum(stock_anio_actual)-sum(stock_anio_anterior))/sum(stock_anio_anterior) as dec (10,4)) as Dif_St_per, cast(sum(stock_unidades_actual)/sum(venta_uds_ultima_semana_anio_actual) as dec(10,2)) as cob_AAct, cast(sum(stock_unidades_anterior)/sum(venta_uds_ultima_semana_anio_anterior) as dec(10,2)) as cob_AAnt from tienda_area_app_stg WHERE estienda=1 group by branch_no order by Vta_AAct; insert into tienda_area_app select * from tienda_area_subtotal_app; drop table if exists tienda_area_1_app; create table tienda_area_1_app as select a.*, replace(concat(format(((a.Vta_AAct)/(b.Vta_AAct))*100,2),'%'),'.',',') as peso_1, replace(concat(format(((a.Mg_AAct)/(b.Mg_AAct))*100,2),'%'),'.',',') as peso_2, replace(concat(format(((a.St_AAct_tot)/(b.St_AAct_tot))*100,2),'%'),'.',',') as peso_3, replace(cast(((a.Vta_AAct/b.Vta_AAct)/(a.St_AAct_tot/b.St_AAct_tot)) as dec(10,2)),'.',',') as peso_4, replace(cast(((a.Mg_AAct/b.Mg_AAct)/(a.St_AAct_tot/b.St_AAct_tot)) as dec(10,2)),'.',',') as peso_5 from tienda_area_app a left join tienda_area_subtotal_app b on a.branch_no=b.branch_no order by branch_no,totales,Vta_AAct desc; drop table if exists tienda_area_total_app; create table tienda_area_total_app as select 'TOTAL' as totales, '' as mercado, '' as branch_no, '' as area, cast(sum(venta_uds_anio_actual) as dec (10,0)) as Vta_uds_AAct, cast((sum(venta_uds_anio_actual)-sum(venta_uds_anio_anterior))/sum(venta_uds_anio_anterior) as dec (10,4)) as Dif_uds_perc, cast(sum(venta_neta_anio_actual) as dec(10,0)) as Vta_AAct, cast(sum(venta_neta_anio_anterior) as dec(10,0)) as Vta_AAnt, cast(sum(venta_neta_anio_actual)-sum(venta_neta_anio_anterior) as dec(10,0)) as Dif_vta, cast((sum(venta_neta_anio_actual)-sum(venta_neta_anio_anterior))/sum(venta_neta_anio_anterior) as dec (10,4)) as Dif_vta_per, cast(sum(margen_anio_actual) as dec(10,0)) as Mg_AAct, cast(sum(margen_anio_anterior) as dec(10,0)) as Mg_AAnt, cast(sum(margen_anio_actual)-sum(margen_anio_anterior) as dec(10,0)) as Dif_Mg, cast((sum(margen_anio_actual)-sum(margen_anio_anterior))/sum(margen_anio_anterior) as dec (10,4)) as Dif_Mg_per, cast(sum(margen_anio_actual)/sum(venta_neta_anio_actual) as dec (10,4)) as GR_AAct, cast(sum(margen_anio_anterior)/sum(venta_neta_anio_anterior) as dec (10,4)) as GR_AAnt, cast(sum(stock_unidades_actual) as dec(10,0)) as St_uds_Aact, cast(sum(stock_unidades_actual)-sum(stock_unidades_anterior) as dec(10,0)) as Dif_St_uds, cast((sum(stock_unidades_actual)-sum(stock_unidades_anterior))/sum(stock_unidades_anterior) as dec (10,4)) as Dif_St_uds_per, cast(sum(stock_anio_actual) as dec(10,0)) as St_AAct_tot, cast(sum(stock_anio_anterior) as dec (10,0)) as St_AAnt_tot, cast(sum(stock_anio_actual)-sum(stock_anio_anterior) as dec (10,0)) as Dif_St, cast((sum(stock_anio_actual)-sum(stock_anio_anterior))/sum(stock_anio_anterior) as dec (10,4)) as Dif_St_per, cast(sum(stock_unidades_actual)/sum(venta_uds_ultima_semana_anio_actual) as dec(10,2)) as cob_AAct, cast(sum(stock_unidades_anterior)/sum(venta_uds_ultima_semana_anio_anterior) as dec(10,2)) as cob_AAnt, '' as peso_1, '' as peso_2, '' as peso_3, '' as peso_4, '' as peso_5 from tienda_area_app_stg WHERE estienda=1; insert into tienda_area_1_app select * from tienda_area_total_app; drop table if exists tienda_area_2_app; create table tienda_area_2_app as select a.*, concat(a.branch_no,' ',b.nombre_tienda) as TIENDA from tienda_area_1_app a left join tienda_zona_territorial_tipo_tienda b on a.branch_no=b.branch_no; drop table if exists jd_tienda_area_app_acumulado; create table jd_tienda_area_app_acumulado as select a.totales, a.mercado as Market, a.TIENDA, a.area, REPLACE(FORMAT(a.Vta_uds_AAct,0),',','.') as Vta_uds_AAct, replace(CONCAT(FORMAT(a.Dif_uds_perc*100,2),'%'),'.',',') as Dif_uds_perc, REPLACE(FORMAT(a.Vta_AAct,0),',','.') as Vta_AAct, REPLACE(FORMAT(a.Vta_AAnt, 0),',','.') as Vta_AAnt, REPLACE(FORMAT(a.Dif_vta,0),',','.') as Dif_vta, replace(CONCAT(FORMAT(a.Dif_vta_per*100,2),'%'),'.',',') as Dif_vta_per, REPLACE(FORMAT(a.Mg_AAct,0),',','.') as Mg_AAct, REPLACE(FORMAT(a.Mg_AAnt,0),',','.') as Mg_AAnt, REPLACE(FORMAT(a.Dif_Mg, 0),',','.') as Dif_Mg, replace(CONCAT(FORMAT(a.Dif_Mg_per*100,2),'%'),'.',',') as Dif_Mg_per, replace(CONCAT(FORMAT(a.GR_AAct*100,2),'%'),'.',',') as GR_AAct, replace(CONCAT(FORMAT(a.GR_AAnt*100,2),'%'),'.',',') as GR_AAnt, REPLACE(FORMAT(a.St_uds_Aact,0),',','.') as St_uds_Aact, REPLACE(FORMAT(a.Dif_St_uds,0),',','.') as Dif_St_uds, replace(CONCAT(FORMAT(a.Dif_St_uds_per*100,2),'%'),'.',',') as Dif_St_uds_per, REPLACE(FORMAT(a.St_AAct_tot,0),',','.') as St_AAct_tot, REPLACE(FORMAT(a.St_AAnt_tot,0),',','.') as St_AAnt_tot, REPLACE(FORMAT(a.Dif_St,0),',','.') as Dif_St, replace(CONCAT(FORMAT(a.Dif_St_per*100,2),'%'),'.',',') as Dif_St_per, replace(cast(a.cob_AAct as dec(10,2)),'.',',') as cob_AAct, replace(cast(a.cob_AAnt as dec(10,2)),'.',',') as cob_AAnt, (case when a.totales in ('SUBTOTAL','TOTAL') then replace(cast(((a.Vta_AAct/b.Vta_AAct)/(a.St_AAct_tot/b.St_AAct_tot)) as dec (10,2)),'.',',') else a.peso_4 end) as vta_sobre_stock, (case when a.totales in ('SUBTOTAL','TOTAL') then replace(cast(((a.Mg_AAct/b.Mg_AAct)/(a.St_AAct_tot/b.St_AAct_tot)) as dec(10,2)),'.',',') else a.peso_5 end) as mg_sobre_stock, (case when a.totales in ('SUBTOTAL','TOTAL') then replace(concat(format(((a.Vta_AAct)/(b.Vta_AAct))*100,2),'%'),'.',',') else a.peso_1 end) as Mix_vtas, (case when a.totales in ('SUBTOTAL','TOTAL') then replace(concat(format(((a.Mg_AAct)/(b.Mg_AAct))*100,2),'%'),'.',',') else a.peso_2 end) as Mix_Mg, (case when a.totales in ('SUBTOTAL','TOTAL') then replace(concat(format(((a.St_AAct_tot)/(b.St_AAct_tot))*100,2),'%'),'.',',') else a.peso_3 end) as Mix_Stock from tienda_area_2_app a left join tienda_area_total_app b on a.totales in ('SUBTOTAL','TOTAL'); /* eliminamos tablas temporales */ drop table if exists tienda_area_1_app; drop table if exists tienda_area_2_app; drop table if exists tienda_area_app; drop table if exists tienda_area_subtotal_app; drop table if exists tienda_area_total_app; /* jd_tienda_area_app MENSUAL */ if FIN_MES_JD then drop table if exists tienda_area_app; create table tienda_area_app as select ' ' as totales, mercado, branch_no, (case when area in ('Complementos','Material técnico') then 'Complementos y Técnico' when area in ('Promociones','Servicios','Otros','na') then 'Otros' else area end) as area, cast(sum(venta_uds_anio_actual) as dec (10,0)) as Vta_uds_AAct, cast((sum(venta_uds_anio_actual)-sum(venta_uds_anio_anterior))/sum(venta_uds_anio_anterior) as dec (10,4)) as Dif_uds_perc, cast(sum(venta_neta_anio_actual) as dec(10,0)) as Vta_AAct, cast(sum(venta_neta_anio_anterior) as dec(10,0)) as Vta_AAnt, cast(sum(venta_neta_anio_actual)-sum(venta_neta_anio_anterior) as dec(10,0)) as Dif_vta, cast((sum(venta_neta_anio_actual)-sum(venta_neta_anio_anterior))/sum(venta_neta_anio_anterior) as dec (10,4)) as Dif_vta_per, cast(sum(margen_anio_actual) as dec(10,0)) as Mg_AAct, cast(sum(margen_anio_anterior) as dec(10,0)) as Mg_AAnt, cast(sum(margen_anio_actual)-sum(margen_anio_anterior) as dec(10,0)) as Dif_Mg, cast((sum(margen_anio_actual)-sum(margen_anio_anterior))/sum(margen_anio_anterior) as dec (10,4)) as Dif_Mg_per, cast(sum(margen_anio_actual)/sum(venta_neta_anio_actual) as dec (10,4)) as GR_AAct, cast(sum(margen_anio_anterior)/sum(venta_neta_anio_anterior) as dec (10,4)) as GR_AAnt, cast(sum(stock_unidades_actual) as dec(10,0)) as St_uds_Aact, cast(sum(stock_unidades_actual)-sum(stock_unidades_anterior) as dec(10,0)) as Dif_St_uds, cast((sum(stock_unidades_actual)-sum(stock_unidades_anterior))/sum(stock_unidades_anterior) as dec (10,4)) as Dif_St_uds_per, cast(sum(stock_anio_actual) as dec(10,0)) as St_AAct_tot, cast(sum(stock_anio_anterior) as dec (10,0)) as St_AAnt_tot, cast(sum(stock_anio_actual)-sum(stock_anio_anterior) as dec (10,0)) as Dif_St, cast((sum(stock_anio_actual)-sum(stock_anio_anterior))/sum(stock_anio_anterior) as dec (10,4)) as Dif_St_per, cast(sum(stock_unidades_actual)/sum(venta_uds_ultima_semana_anio_actual) as dec(10,2)) as cob_AAct, cast(sum(stock_unidades_anterior)/sum(venta_uds_ultima_semana_anio_anterior) as dec(10,2)) as cob_AAnt from tienda_area_app_stg WHERE estienda=1 and cast(semana as unsigned) between SEMANA_INICIAL_MES_JD and SEMANA_FINAL group by branch_no, (case when area in ('Complementos','Material técnico') then 'Complementos y Técnico' when area in ('Promociones','Servicios','Otros','na') then 'Otros' else area end) order by branch_no,Vta_AAct desc; drop table if exists tienda_area_subtotal_app; create table tienda_area_subtotal_app as select 'SUBTOTAL' as totales, mercado, branch_no, '' as area, cast(sum(venta_uds_anio_actual) as dec (10,0)) as Vta_uds_AAct, cast((sum(venta_uds_anio_actual)-sum(venta_uds_anio_anterior))/sum(venta_uds_anio_anterior) as dec (10,4)) as Dif_uds_perc, cast(sum(venta_neta_anio_actual) as dec(10,0)) as Vta_AAct, cast(sum(venta_neta_anio_anterior) as dec(10,0)) as Vta_AAnt, cast(sum(venta_neta_anio_actual)-sum(venta_neta_anio_anterior) as dec(10,0)) as Dif_vta, cast((sum(venta_neta_anio_actual)-sum(venta_neta_anio_anterior))/sum(venta_neta_anio_anterior) as dec (10,4)) as Dif_vta_per, cast(sum(margen_anio_actual) as dec(10,0)) as Mg_AAct, cast(sum(margen_anio_anterior) as dec(10,0)) as Mg_AAnt, cast(sum(margen_anio_actual)-sum(margen_anio_anterior) as dec(10,0)) as Dif_Mg, cast((sum(margen_anio_actual)-sum(margen_anio_anterior))/sum(margen_anio_anterior) as dec (10,4)) as Dif_Mg_per, cast(sum(margen_anio_actual)/sum(venta_neta_anio_actual) as dec (10,4)) as GR_AAct, cast(sum(margen_anio_anterior)/sum(venta_neta_anio_anterior) as dec (10,4)) as GR_AAnt, cast(sum(stock_unidades_actual) as dec(10,0)) as St_uds_Aact, cast(sum(stock_unidades_actual)-sum(stock_unidades_anterior) as dec(10,0)) as Dif_St_uds, cast((sum(stock_unidades_actual)-sum(stock_unidades_anterior))/sum(stock_unidades_anterior) as dec (10,4)) as Dif_St_uds_per, cast(sum(stock_anio_actual) as dec(10,0)) as St_AAct_tot, cast(sum(stock_anio_anterior) as dec (10,0)) as St_AAnt_tot, cast(sum(stock_anio_actual)-sum(stock_anio_anterior) as dec (10,0)) as Dif_St, cast((sum(stock_anio_actual)-sum(stock_anio_anterior))/sum(stock_anio_anterior) as dec (10,4)) as Dif_St_per, cast(sum(stock_unidades_actual)/sum(venta_uds_ultima_semana_anio_actual) as dec(10,2)) as cob_AAct, cast(sum(stock_unidades_anterior)/sum(venta_uds_ultima_semana_anio_anterior) as dec(10,2)) as cob_AAnt from tienda_area_app_stg WHERE estienda=1 and cast(semana as unsigned) between SEMANA_INICIAL_MES_JD and SEMANA_FINAL group by branch_no order by Vta_AAct; insert into tienda_area_app select * from tienda_area_subtotal_app; drop table if exists tienda_area_1_app; create table tienda_area_1_app as select a.*, replace(concat(format(((a.Vta_AAct)/(b.Vta_AAct))*100,2),'%'),'.',',') as peso_1, replace(concat(format(((a.Mg_AAct)/(b.Mg_AAct))*100,2),'%'),'.',',') as peso_2, replace(concat(format(((a.St_AAct_tot)/(b.St_AAct_tot))*100,2),'%'),'.',',') as peso_3, replace(cast(((a.Vta_AAct/b.Vta_AAct)/(a.St_AAct_tot/b.St_AAct_tot)) as dec(10,2)),'.',',') as peso_4, replace(cast(((a.Mg_AAct/b.Mg_AAct)/(a.St_AAct_tot/b.St_AAct_tot)) as dec(10,2)),'.',',') as peso_5 from tienda_area_app a left join tienda_area_subtotal_app b on a.branch_no=b.branch_no order by branch_no,totales,Vta_AAct desc; drop table if exists tienda_area_total_app; create table tienda_area_total_app as select 'TOTAL' as totales, '' as mercado, '' as branch_no, '' as area, cast(sum(venta_uds_anio_actual) as dec (10,0)) as Vta_uds_AAct, cast((sum(venta_uds_anio_actual)-sum(venta_uds_anio_anterior))/sum(venta_uds_anio_anterior) as dec (10,4)) as Dif_uds_perc, cast(sum(venta_neta_anio_actual) as dec(10,0)) as Vta_AAct, cast(sum(venta_neta_anio_anterior) as dec(10,0)) as Vta_AAnt, cast(sum(venta_neta_anio_actual)-sum(venta_neta_anio_anterior) as dec(10,0)) as Dif_vta, cast((sum(venta_neta_anio_actual)-sum(venta_neta_anio_anterior))/sum(venta_neta_anio_anterior) as dec (10,4)) as Dif_vta_per, cast(sum(margen_anio_actual) as dec(10,0)) as Mg_AAct, cast(sum(margen_anio_anterior) as dec(10,0)) as Mg_AAnt, cast(sum(margen_anio_actual)-sum(margen_anio_anterior) as dec(10,0)) as Dif_Mg, cast((sum(margen_anio_actual)-sum(margen_anio_anterior))/sum(margen_anio_anterior) as dec (10,4)) as Dif_Mg_per, cast(sum(margen_anio_actual)/sum(venta_neta_anio_actual) as dec (10,4)) as GR_AAct, cast(sum(margen_anio_anterior)/sum(venta_neta_anio_anterior) as dec (10,4)) as GR_AAnt, cast(sum(stock_unidades_actual) as dec(10,0)) as St_uds_Aact, cast(sum(stock_unidades_actual)-sum(stock_unidades_anterior) as dec(10,0)) as Dif_St_uds, cast((sum(stock_unidades_actual)-sum(stock_unidades_anterior))/sum(stock_unidades_anterior) as dec (10,4)) as Dif_St_uds_per, cast(sum(stock_anio_actual) as dec(10,0)) as St_AAct_tot, cast(sum(stock_anio_anterior) as dec (10,0)) as St_AAnt_tot, cast(sum(stock_anio_actual)-sum(stock_anio_anterior) as dec (10,0)) as Dif_St, cast((sum(stock_anio_actual)-sum(stock_anio_anterior))/sum(stock_anio_anterior) as dec (10,4)) as Dif_St_per, cast(sum(stock_unidades_actual)/sum(venta_uds_ultima_semana_anio_actual) as dec(10,2)) as cob_AAct, cast(sum(stock_unidades_anterior)/sum(venta_uds_ultima_semana_anio_anterior) as dec(10,2)) as cob_AAnt, '' as peso_1, '' as peso_2, '' as peso_3, '' as peso_4, '' as peso_5 from tienda_area_app_stg WHERE estienda=1 and cast(semana as unsigned) between SEMANA_INICIAL_MES_JD and SEMANA_FINAL; insert into tienda_area_1_app select * from tienda_area_total_app; drop table if exists tienda_area_2_app; create table tienda_area_2_app as select a.*, concat(a.branch_no,' ',b.nombre_tienda) as TIENDA from tienda_area_1_app a left join tienda_zona_territorial_tipo_tienda b on a.branch_no=b.branch_no; drop table if exists jd_tienda_area_app_mensual; create table jd_tienda_area_app_mensual as select a.totales, a.mercado as Market, a.TIENDA, a.area, REPLACE(FORMAT(a.Vta_uds_AAct,0),',','.') as Vta_uds_AAct, replace(CONCAT(FORMAT(a.Dif_uds_perc*100,2),'%'),'.',',') as Dif_uds_perc, REPLACE(FORMAT(a.Vta_AAct,0),',','.') as Vta_AAct, REPLACE(FORMAT(a.Vta_AAnt, 0),',','.') as Vta_AAnt, REPLACE(FORMAT(a.Dif_vta,0),',','.') as Dif_vta, replace(CONCAT(FORMAT(a.Dif_vta_per*100,2),'%'),'.',',') as Dif_vta_per, REPLACE(FORMAT(a.Mg_AAct,0),',','.') as Mg_AAct, REPLACE(FORMAT(a.Mg_AAnt,0),',','.') as Mg_AAnt, REPLACE(FORMAT(a.Dif_Mg, 0),',','.') as Dif_Mg, replace(CONCAT(FORMAT(a.Dif_Mg_per*100,2),'%'),'.',',') as Dif_Mg_per, replace(CONCAT(FORMAT(a.GR_AAct*100,2),'%'),'.',',') as GR_AAct, replace(CONCAT(FORMAT(a.GR_AAnt*100,2),'%'),'.',',') as GR_AAnt, REPLACE(FORMAT(a.St_uds_Aact,0),',','.') as St_uds_Aact, REPLACE(FORMAT(a.Dif_St_uds,0),',','.') as Dif_St_uds, replace(CONCAT(FORMAT(a.Dif_St_uds_per*100,2),'%'),'.',',') as Dif_St_uds_per, REPLACE(FORMAT(a.St_AAct_tot,0),',','.') as St_AAct_tot, REPLACE(FORMAT(a.St_AAnt_tot,0),',','.') as St_AAnt_tot, REPLACE(FORMAT(a.Dif_St,0),',','.') as Dif_St, replace(CONCAT(FORMAT(a.Dif_St_per*100,2),'%'),'.',',') as Dif_St_per, replace(cast(a.cob_AAct as dec(10,2)),'.',',') as cob_AAct, replace(cast(a.cob_AAnt as dec(10,2)),'.',',') as cob_AAnt, (case when a.totales in ('SUBTOTAL','TOTAL') then replace(cast(((a.Vta_AAct/b.Vta_AAct)/(a.St_AAct_tot/b.St_AAct_tot)) as dec (10,2)),'.',',') else a.peso_4 end) as vta_sobre_stock, (case when a.totales in ('SUBTOTAL','TOTAL') then replace(cast(((a.Mg_AAct/b.Mg_AAct)/(a.St_AAct_tot/b.St_AAct_tot)) as dec(10,2)),'.',',') else a.peso_5 end) as mg_sobre_stock, (case when a.totales in ('SUBTOTAL','TOTAL') then replace(concat(format(((a.Vta_AAct)/(b.Vta_AAct))*100,2),'%'),'.',',') else a.peso_1 end) as Mix_vtas, (case when a.totales in ('SUBTOTAL','TOTAL') then replace(concat(format(((a.Mg_AAct)/(b.Mg_AAct))*100,2),'%'),'.',',') else a.peso_2 end) as Mix_Mg, (case when a.totales in ('SUBTOTAL','TOTAL') then replace(concat(format(((a.St_AAct_tot)/(b.St_AAct_tot))*100,2),'%'),'.',',') else a.peso_3 end) as Mix_Stock from tienda_area_2_app a left join tienda_area_total_app b on a.totales in ('SUBTOTAL','TOTAL'); /* eliminamos tablas temporales */ drop table if exists tienda_area_1_app; drop table if exists tienda_area_2_app; drop table if exists tienda_area_app; drop table if exists tienda_area_subtotal_app; drop table if exists tienda_area_total_app; end if; -- ----------------------------------------------------------------------------------------------------------------------------------------------------- /* Añadimos los datos semanales a la tabla PRODUCTO_SEMANAS. Sólo cuando estemos ejecutando el procedimiento para el informe semanal */ /* Esta tabla se genera para ir guardando un histórico de esta estructura en la base datos que pueda ser utilizado como fuente de análisis */ -- ----------------------------------------------------------------------------------------------------------------------------------------------------- /* También se añade los datos semanales de Tickets a la tabla histórica de Tickets Semanas */ -- se modifica para se ejecute siempre -- IF date_add(FECHA_INICIAL,interval 6 day)=FECHA_FINAL then -- Hemos eliminado ventas_stock2 drop table if exists ventas_stock; create table ventas_stock as select * from ventas_stock1 where cast(semana as unsigned) = SEMANA_FINAL; -- > solo la última semana /* Calculamos métricas de tiendas con venta y con stock */ drop temporary table if exists consultas.tmp_csv_prod; create temporary table consultas.tmp_csv_prod as select branch_no, mercado, semana, product_code, fare_code, (case when tipo_tienda = 'WEB' then 'WEB' else 'OFFLINE' end) channel_type, sum(stock_unidades_actual)-sum(stock_uds_anio_actual_alm) as stock, sum(venta_uds_anio_actual) as venta, sum(stock_unidades_anterior)-sum(stock_uds_anio_anterior_alm) as stock_ly, sum(venta_uds_anio_anterior) as venta_ly from consultas.ventas_stock group by branch_no, mercado, semana, product_code, fare_code, (case when tipo_tienda = 'WEB' then 'WEB' else 'OFFLINE' end); drop temporary table if exists consultas.tmp_csv_product; create temporary table consultas.tmp_csv_product as select mercado, semana, product_code, fare_code, channel_type, sum(case when stock>0 then 1 else 0 end) as Branchs_With_Stock_TY, sum(case when venta>0 then 1 else 0 end) as Branchs_With_Sales_TY, sum(case when stock_ly>0 then 1 else 0 end) as Branchs_With_Stock_LY, sum(case when venta_ly>0 then 1 else 0 end) as Branchs_With_Sales_LY from consultas.tmp_csv_prod group by mercado, semana, product_code, fare_code, channel_type; alter table consultas.tmp_csv_product add index idx(mercado, semana, product_code, fare_code, channel_type); /* PRODUCTO_SEMANAS */ DROP table if exists PRODUCTO2; create table PRODUCTO2 as select concat(ANIO_ACTUAL,'/',ANIO_ACTUAL-1,' ',semana) as AñoSem, concat(ANIO_ACTUAL,'/',ANIO_ACTUAL-1) as Year, (case when MES_FINAL_JD='12' then 'Jan' when MES_FINAL_JD='01' then 'Feb' when MES_FINAL_JD='02' then 'Mar' when MES_FINAL_JD='03' then 'Apr' when MES_FINAL_JD='04' then 'May' when MES_FINAL_JD='05' then 'Jun' when MES_FINAL_JD='06' then 'Jul' when MES_FINAL_JD='07' then 'Aug' when MES_FINAL_JD='08' then 'Sep' when MES_FINAL_JD='09' then 'Oct' when MES_FINAL_JD='10' then 'Nov' when MES_FINAL_JD='11' then 'Dec' end) as Month, semana, Mercado as Market, Fare_Code, udp_code as UDP_Code, udp_name as UDP_Name, area as Area, (case when area='Textil' then 'APPAREL' when area='Calzado' then 'FWR' when area in ('Material técnico','Complementos') then 'ACC&&HEQ' when area in ('Promociones','Otros','na','Servicios') then 'OTHER' end) as Area2, serie as Gender, (case when serie in ('UNISEX') and area in ('Textil','Calzado','Complementos','Material técnico') then 'UNISEX' when serie in ('UNISEX','na') and area in ('Promociones','Otros','Servicios','na') then 'OTHER' when serie in ('CABALLERO','XMAN') and area in ('Textil','Calzado','Complementos','Material técnico') then 'MAN' when serie in ('SEÑORA','LADY','JR. x SRA.') and area in ('Textil','Calzado','Complementos','Material técnico') then 'WOMAN' else 'JCI' end) as Gender2, seccion as Section, seccion_a as Activity, subseccion_a as Subsection_A, familia as Category, disenyo as Style, marca as Brand, brand_desc as Brand_Type, analytical_color as ColorDesc, product_code as Product_Id, descripcion as Product_Description, Temporada, Temporalidad, Descatalogado, precio_original as First_Price, precio_actual as Current_Price, sum(venta_uds_anio_actual) as Units_Sold_TY, sum(venta_uds_anio_anterior) as Units_Sold_LY, sum(venta_uds_anio_actual_lfl) as Units_Sold_LFL_TY, sum(venta_uds_anio_anterior_lfl) as Units_Sold_LFL_LY, sum(venta_neta_anio_Actual) as Net_Sales_TY, sum(venta_neta_anio_anterior) as Net_Sales_LY, sum(venta_neta_anio_Actual_lfl) as Net_Sales_LFL_TY, sum(venta_neta_anio_anterior_lfl) as Net_Sales_LFL_LY, sum(venta_bruta_anio_Actual) as Gross_Sales_TY, sum(venta_bruta_anio_anterior) as Gross_Sales_LY, sum(margen_anio_actual) as GPV_TY, sum(margen_anio_anterior) as GPV_LY, sum(margen_anio_actual_lfl) as GPV_LFL_TY, sum(margen_anio_anterior_lfl) as GPV_LFL_LY, sum(stock_anio_actual) as Total_Stk_Eur_TY, sum(stock_anio_anterior) as Total_Stk_Eur_LY, sum(stock_unidades_actual) as Total_Stk_Units_TY, sum(stock_unidades_anterior) as Total_Stk_Units_LY, sum(stock_anio_actual)-sum(stock_anio_actual_alm) as Branch_Stk_Eur_TY, sum(stock_anio_anterior)-sum(stock_anio_anterior_alm) as Branch_Stk_Eur_LY, sum(stock_unidades_actual)-sum(stock_uds_anio_actual_alm) as Branch_Stk_Units_TY, sum(stock_unidades_anterior)-sum(stock_uds_anio_anterior_alm) as Branch_Stk_Units_LY, sum(stock_anio_actual_alm) as WH_Stk_Eur_TY, sum(stock_anio_anterior_alm) as WH_Stk_Eur_LY, sum(stock_uds_anio_actual_alm) as WH_Stk_Units_TY, sum(stock_uds_anio_anterior_alm) as WH_Stk_Units_LY, case when tipo_tienda = 'WEB' then 'WEB' else 'OFFLINE' end Channel_Type, 0 as Branchs_With_Stock_TY, 0 as Branchs_With_Sales_TY, 0 as Branchs_With_Stock_LY, 0 as Branchs_With_Sales_LY from ventas_stock group by semana,mercado,Fare_Code,udp_code,udp_name,area,(case when area='Textil' then 'APPAREL' when area='Calzado' then 'FWR' when area in ('Material técnico','Complementos') then 'ACC&&HEQ' when area in ('Promociones','Otros','na','Servicios') then 'OTHER' end),serie,(case when serie in ('UNISEX') and area in ('Textil','Calzado','Complementos','Material técnico') then 'UNISEX' when serie in ('UNISEX','na') and area in ('Promociones','Otros','Servicios','na') then 'OTHER' when serie in ('CABALLERO','XMAN') and area in ('Textil','Calzado','Complementos','Material técnico') then 'MAN' when serie in ('SEÑORA','LADY','JR. x SRA.') and area in ('Textil','Calzado','Complementos','Material técnico') then 'MAN' else 'JCI' end),seccion_a,seccion,subseccion_a,familia,disenyo,marca,analytical_color,brand_desc,product_code,descripcion,Temporada, Temporalidad,Descatalogado,precio_original,precio_actual, case when tipo_tienda = 'WEB' then 'WEB' else 'OFFLINE' end; update PRODUCTO2 a LEFT OUTER JOIN tmp_csv_product t ON a.product_id = t.product_code and a.semana = t.semana and a.market = t.mercado and a.channel_type = t.channel_type and a.fare_code = t.fare_code set a.Branchs_With_Stock_TY = COALESCE(t.Branchs_With_Stock_TY,0), a.Branchs_With_Sales_TY = COALESCE(t.Branchs_With_Sales_TY,0), a.Branchs_With_Stock_LY = COALESCE(t.Branchs_With_Stock_LY,0), a.Branchs_With_Sales_LY = COALESCE(t.Branchs_With_Sales_LY,0); delete from PRODUCTO_SEMANAS_new_new where substring(Year,1,4)=ANIO_ACTUAL and cast(semana as unsigned)=SEMANA_FINAL; insert into PRODUCTO_SEMANAS_new_new select * from PRODUCTO2; drop table if exists PRODUCTO2; /* TICKET SEMANAS */ delete from TICKET_SEMANAS_NEW_NEW where substring(Año,1,4)=ANIO_ACTUAL and cast(Week as unsigned) = SEMANA_FINAL; delete from TICKET_SEMANAS_NO_SERV_NEW_NEW where substring(Año,1,4)=ANIO_ACTUAL and cast(Week as unsigned) = SEMANA_FINAL; insert into TICKET_SEMANAS_NEW_NEW select concat(ANIO_ACTUAL,'/',ANIO_ACTUAL-1,' ', a1.semana) as AñoSem, concat(ANIO_ACTUAL,'/',ANIO_ACTUAL-1) as Año, a1.branch_no as Branch_Id, a1.nombre_tienda as Branch_Name, i.mercado as Market, a1.tipo_tienda as Location, a1.subzona as Subzona, a1.delegado as Delegation_Manager, a1.zona as Region, a1.territorial as Territorial_Manager, (CASE when a1.dia=5 then 'Sat' when a1.dia=4 then 'Fri' when a1.dia=3 then 'Thu' when a1.dia=2 then 'Wed' when a1.dia=1 then 'Tue' when a1.dia=0 then 'Mon' when a1.dia=6 then 'Sun' end) as Weekday, h.Fecha as Date, a1.semana as Week, a1.mes as Month, coalesce(f.entradas_aact,0) as FF_TY, coalesce(g.entradas_aant,0) as FF_LY, (case when coalesce(f.entradas_aact,0)=0 or coalesce(g.entradas_aant,0)=0 then 0 else coalesce(f.entradas_aact,0) end) as FF_TY_LFL, (case when coalesce(g.entradas_aant,0)=0 or coalesce(f.entradas_aact,0)=0 then 0 else coalesce(g.entradas_aant,0) end) as FF_LY_LFL, coalesce(b.num_tickets_aact,0) as Transactions_TY, coalesce(c.num_tickets_aant,0) as Transactions_LY, /*(case when coalesce(f.entradas_aact,0)=0 or coalesce(g.entradas_aant,0)=0 then 0 else coalesce(b.num_tickets_aact,0) end) as Transactions_TY_LFL, (case when coalesce(g.entradas_aant,0)=0 or coalesce(f.entradas_aact,0)=0 then 0 else coalesce(c.num_tickets_aant,0) end) as Transactions_LY_LFL,*/ (case when coalesce(b.num_tickets_aact,0)=0 or coalesce(c.num_tickets_aant,0)=0 then 0 else coalesce(b.num_tickets_aact,0) end) as Transactions_TY_LFL, (case when coalesce(c.num_tickets_aant,0)=0 or coalesce(b.num_tickets_aact,0)=0 then 0 else coalesce(c.num_tickets_aant,0) end) as Transactions_LY_LFL, coalesce(a.vta_uds_Aact,0) as Unist_Sold_TY, coalesce(a.vta_uds_Aant,0) as Unist_Sold_LY, coalesce(a.vta_bruta_Aact,0) as Gross_Sales_TY, coalesce(a.vta_bruta_Aant,0) as Gross_Sales_LY, coalesce(a.vta_neta_Aact,0) as Net_Sales_TY, coalesce(a.vta_neta_Aant,0) as Net_Sales_LY, coalesce(a.vta_neta_Aact_lfl,0) as Net_Sales_TY_LFL, coalesce(a.vta_neta_Aant_lfl,0) as Net_Sales_LY_LFL, coalesce(j.Target_TY,0) as Target_TY, coalesce(j.GPV_Target_TY,0) as GPV_Target_TY, coalesce(a.mg_Aact,0) as GPV_TY, coalesce(a.mg_Aant,0) as GPV_LY, coalesce(a.mg_Aact_lfl,0) as GPV_TY_LFL, coalesce(a.mg_Aant_lfl,0) as GPV_LY_LFL, coalesce(d.num_tickets_aact_textil,0) as Transactions_TXT_TY, coalesce(e.num_tickets_aant_textil,0) as Transactions_TXT_LY, coalesce(d.num_tickets_aact_calzado,0) as Transactions_FWR_TY, coalesce(e.num_tickets_aant_calzado,0) as Transactions_FWR_LY, coalesce(d.num_tickets_aact_acc,0) as Transactions_ACC_HEQ_TY, coalesce(e.num_tickets_aant_acc,0) as Transactions_ACC_HEQ_LY, coalesce(a.stock_uds_aact,0) as Stock_Units_TY, coalesce(a.stock_uds_aant,0) as Stock_Units_LY, coalesce(a.stock_aact,0) as Stock_Euros_TY, coalesce(a.stock_aant,0) as Stock_Euros_LY from ticket_pre_a1 a1 left join ticket_pre a on a1.branch_no=a.branch_no and a1.semana=a.semana and a1.dia=a.dia left join tickets_aact b on a1.branch_no=b.branch_no and a1.semana=b.semana and a1.dia=b.dia left join tickets_aant c on a1.branch_no=c.branch_no and a1.semana=c.semana and a1.dia=c.dia left join tickets_areas_aact d on a1.branch_no=d.branch_no and a1.semana=d.semana and a1.dia=d.dia left join tickets_areas_aant e on a1.branch_no=e.branch_no and a1.semana=e.semana and a1.dia=e.dia left join footfall_aact f on a1.branch_no=f.branch_no and a1.semana=f.semana and a1.dia=f.dia left join footfall_aant g on a1.branch_no=g.branch_no and a1.semana=g.semana and a1.dia=g.dia left join md_dbg.calendario_Detalle_jd_v2 h on a1.dia=weekday(h.Fecha) and a1.semana=h.semana_jd and h.anio_jd=ANIO_ACTUAL left join tienda_zona_territorial_tipo_tienda i on a1.branch_no=i.branch_no left join objetivo_aact j on a1.branch_no=j.branch_no and a1.semana=j.semana and a1.dia=j.dia where cast(a1.semana as unsigned) = SEMANA_FINAL; insert into TICKET_SEMANAS_NO_SERV_NEW_NEW select concat(ANIO_ACTUAL,'/',ANIO_ACTUAL-1,' ', a1.semana) as AñoSem, concat(ANIO_ACTUAL,'/',ANIO_ACTUAL-1) as Año, a1.branch_no as Branch_Id, a1.nombre_tienda as Branch_Name, i.mercado as Market, a1.tipo_tienda as Location, a1.subzona as Subzona, a1.delegado as Delegation_Manager, a1.zona as Region, a1.territorial as Territorial_Manager, (CASE when a1.dia=5 then 'Sat' when a1.dia=4 then 'Fri' when a1.dia=3 then 'Thu' when a1.dia=2 then 'Wed' when a1.dia=1 then 'Tue' when a1.dia=0 then 'Mon' when a1.dia=6 then 'Sun' end) as Weekday, h.Fecha as Date, a1.semana as Week, a1.mes as Month, coalesce(f.entradas_aact,0) as FF_TY, coalesce(g.entradas_aant,0) as FF_LY, (case when coalesce(f.entradas_aact,0)=0 or coalesce(g.entradas_aant,0)=0 then 0 else coalesce(f.entradas_aact,0) end) as FF_TY_LFL, (case when coalesce(g.entradas_aant,0)=0 or coalesce(f.entradas_aact,0)=0 then 0 else coalesce(g.entradas_aant,0) end) as FF_LY_LFL, coalesce(b.num_tickets_aact,0) as Transactions_TY, coalesce(c.num_tickets_aant,0) as Transactions_LY, /*(case when coalesce(f.entradas_aact,0)=0 or coalesce(g.entradas_aant,0)=0 then 0 else coalesce(b.num_tickets_aact,0) end) as Transactions_TY_LFL, (case when coalesce(g.entradas_aant,0)=0 or coalesce(f.entradas_aact,0)=0 then 0 else coalesce(c.num_tickets_aant,0) end) as Transactions_LY_LFL,*/ (case when coalesce(b.num_tickets_aact,0)=0 or coalesce(c.num_tickets_aant,0)=0 then 0 else coalesce(b.num_tickets_aact,0) end) as Transactions_TY_LFL, (case when coalesce(c.num_tickets_aant,0)=0 or coalesce(b.num_tickets_aact,0)=0 then 0 else coalesce(c.num_tickets_aant,0) end) as Transactions_LY_LFL, coalesce(a.vta_uds_Aact,0) as Unist_Sold_TY, coalesce(a.vta_uds_Aant,0) as Unist_Sold_LY, coalesce(a.vta_bruta_Aact,0) as Gross_Sales_TY, coalesce(a.vta_bruta_Aant,0) as Gross_Sales_LY, coalesce(a.vta_neta_Aact,0) as Net_Sales_TY, coalesce(a.vta_neta_Aant,0) as Net_Sales_LY, coalesce(a.vta_neta_Aact_lfl,0) as Net_Sales_TY_LFL, coalesce(a.vta_neta_Aant_lfl,0) as Net_Sales_LY_LFL, coalesce(j.Target_TY,0) as Target_TY, coalesce(j.GPV_Target_TY,0) as GPV_Target_TY, coalesce(a.mg_Aact,0) as GPV_TY, coalesce(a.mg_Aant,0) as GPV_LY, coalesce(a.mg_Aact_lfl,0) as GPV_TY_LFL, coalesce(a.mg_Aant_lfl,0) as GPV_LY_LFL, coalesce(d.num_tickets_aact_textil,0) as Transactions_TXT_TY, coalesce(e.num_tickets_aant_textil,0) as Transactions_TXT_LY, coalesce(d.num_tickets_aact_calzado,0) as Transactions_FWR_TY, coalesce(e.num_tickets_aant_calzado,0) as Transactions_FWR_LY, coalesce(d.num_tickets_aact_acc,0) as Transactions_ACC_HEQ_TY, coalesce(e.num_tickets_aant_acc,0) as Transactions_ACC_HEQ_LY, coalesce(a.stock_uds_aact,0) as Stock_Units_TY, coalesce(a.stock_uds_aant,0) as Stock_Units_LY, coalesce(a.stock_aact,0) as Stock_Euros_TY, coalesce(a.stock_aant,0) as Stock_Euros_LY from ticket_pre_a1 a1 left join ticket_pre_NO_SERV a on a1.branch_no=a.branch_no and a1.semana=a.semana and a1.dia=a.dia left join tickets_aact_NO_SERV b on a1.branch_no=b.branch_no and a1.semana=b.semana and a1.dia=b.dia left join tickets_aant_NO_SERV c on a1.branch_no=c.branch_no and a1.semana=c.semana and a1.dia=c.dia left join tickets_areas_aact d on a1.branch_no=d.branch_no and a1.semana=d.semana and a1.dia=d.dia left join tickets_areas_aant e on a1.branch_no=e.branch_no and a1.semana=e.semana and a1.dia=e.dia left join footfall_aact f on a1.branch_no=f.branch_no and a1.semana=f.semana and a1.dia=f.dia left join footfall_aant g on a1.branch_no=g.branch_no and a1.semana=g.semana and a1.dia=g.dia left join md_dbg.calendario_Detalle_jd_v2 h on a1.dia=weekday(h.Fecha) and a1.semana=h.semana_jd and h.anio_jd=ANIO_ACTUAL left join tienda_zona_territorial_tipo_tienda i on a1.branch_no=i.branch_no left join objetivo_aact j on a1.branch_no=j.branch_no and a1.semana=j.semana and a1.dia=j.dia where cast(a1.semana as unsigned) = SEMANA_FINAL; /* TIENDA-AREA2 SEMANAS */ /* Se añaden los datos semanales de TIENDA-AREA2 a la tabla histórica de TIENDA-AREA2-SEMANAS */ delete from TIENDA_AREA2_SEMANAS_NEW_NEW where AñoSem=concat(ANIO_ACTUAL,'/',ANIO_ACTUAL-1,' ', SEMANA_FINAL_STR); insert into TIENDA_AREA2_SEMANAS_NEW_NEW select concat(ANIO_ACTUAL,'/',ANIO_ACTUAL-1,' ', SEMANA_FINAL_STR) as AñoSem, concat(ANIO_ACTUAL,'/',ANIO_ACTUAL-1) as Año, mercado as Market, branch_no AS IdTienda, nombre_tienda as Tienda, (case when area in ('Complementos','Material técnico') then 'Complementos y Técnico' when area in ('Promociones','Servicios','Otros','na') then 'Otros' else area end) as Area2, sum(venta_neta_anio_actual) as Net_Sales_TY, sum(venta_neta_anio_anterior) as Net_Sales_LY, sum(margen_anio_actual) as GPV_TY, sum(margen_anio_anterior) as GPV_LY, sum(stock_unidades_actual) as Stock_Units_TY, sum(stock_unidades_anterior) as Stock_Units_LY, sum(stock_anio_actual) as Stock_Euros_TY, sum(stock_anio_anterior) as Stock_Euros_LY from ventas_stock where EsTienda=1 and semana = SEMANA_FINAL_STR group by mercado,branch_no,(case when area in ('Complementos','Material técnico') then 'Complementos y Técnico' when area in ('Promociones','Servicios','Otros','na') then 'Otros' else area end); -- END IF; -- ----------------------------------------------------------------------------------------------------------------------------------------------------- /* Añadimos los datos semanales a la tabla PRODUCTO_SEMANAS. Sólo cuando estemos ejecutando el procedimiento para el informe semanal ACUMULADO */ /* Esta tabla se genera para ir guardando un histórico de esta estructura en la base datos que pueda ser utilizado como fuente de análisis */ -- ----------------------------------------------------------------------------------------------------------------------------------------------------- /* También se añade los datos semanales de Tickets a la tabla histórica de Tickets Semanas YTD */ /* IF FECHA_INICIAL = ( SELECT MIN(Fecha) as Inicio from md_dbg.calendario_detalle_jd_v2 where anio_jd = (Select Anio_jd from md_dbg.calendario_detalle_jd_v2 where fecha = FECHA_INICIAL) ) then */ drop table if exists ventas_stock; create table ventas_stock as select * from ventas_stock1; /*PRODUCTO_SEMANAS*/ DROP table if exists PRODUCTO_YTD; create table PRODUCTO_YTD as select concat(ANIO_ACTUAL,'/',ANIO_ACTUAL-1,' ',semana) as AñoSem, concat(ANIO_ACTUAL,'/',ANIO_ACTUAL-1) as Year, (case when MES_FINAL_JD='12' then 'Jan' when MES_FINAL_JD='01' then 'Feb' when MES_FINAL_JD='02' then 'Mar' when MES_FINAL_JD='03' then 'Apr' when MES_FINAL_JD='04' then 'May' when MES_FINAL_JD='05' then 'Jun' when MES_FINAL_JD='06' then 'Jul' when MES_FINAL_JD='07' then 'Aug' when MES_FINAL_JD='08' then 'Sep' when MES_FINAL_JD='09' then 'Oct' when MES_FINAL_JD='10' then 'Nov' when MES_FINAL_JD='11' then 'Dec' end) as Month, semana, Mercado as Market, Fare_Code, udp_code as UDP_Code, udp_name as UDP_Name, area as Area, (case when area='Textil' then 'APPAREL' when area='Calzado' then 'FWR' when area in ('Material técnico','Complementos') then 'ACC&&HEQ' when area in ('Promociones','Otros','na','Servicios') then 'OTHER' end) as Area2, serie as Gender, (case when serie in ('UNISEX') and area in ('Textil','Calzado','Complementos','Material técnico') then 'UNISEX' when serie in ('UNISEX','na') and area in ('Promociones','Otros','Servicios','na') then 'OTHER' when serie in ('CABALLERO','XMAN') and area in ('Textil','Calzado','Complementos','Material técnico') then 'MAN' when serie in ('SEÑORA','LADY','JR. x SRA.') and area in ('Textil','Calzado','Complementos','Material técnico') then 'WOMAN' else 'JCI' end) as Gender2, seccion as Section, seccion_a as Activity, subseccion_a as Subsection_A, familia as Category, disenyo as Style, marca as Brand, brand_desc as Brand_Type, analytical_color as ColorDesc, product_code as Product_Id, descripcion as Product_Description, Temporada, Temporalidad, Descatalogado, precio_original as First_Price, precio_actual as Current_Price, sum(venta_uds_anio_actual) as Units_Sold_TY, sum(venta_uds_anio_anterior) as Units_Sold_LY, sum(venta_uds_anio_actual_lfl) as Units_Sold_LFL_TY, sum(venta_uds_anio_anterior_lfl) as Units_Sold_LFL_LY, sum(venta_neta_anio_Actual) as Net_Sales_TY, sum(venta_neta_anio_anterior) as Net_Sales_LY, sum(venta_neta_anio_Actual_lfl) as Net_Sales_LFL_TY, sum(venta_neta_anio_anterior_lfl) as Net_Sales_LFL_LY, sum(venta_bruta_anio_Actual) as Gross_Sales_TY, sum(venta_bruta_anio_anterior) as Gross_Sales_LY, sum(margen_anio_actual) as GPV_TY, sum(margen_anio_anterior) as GPV_LY, sum(margen_anio_actual_lfl) as GPV_LFL_TY, sum(margen_anio_anterior_lfl) as GPV_LFL_LY, sum(stock_anio_actual) as Total_Stk_Eur_TY, sum(stock_anio_anterior) as Total_Stk_Eur_LY, sum(stock_unidades_actual) as Total_Stk_Units_TY, sum(stock_unidades_anterior) as Total_Stk_Units_LY, sum(stock_anio_actual)-sum(stock_anio_actual_alm) as Branch_Stk_Eur_TY, sum(stock_anio_anterior)-sum(stock_anio_anterior_alm) as Branch_Stk_Eur_LY, sum(stock_unidades_actual)-sum(stock_uds_anio_actual_alm) as Branch_Stk_Units_TY, sum(stock_unidades_anterior)-sum(stock_uds_anio_anterior_alm) as Branch_Stk_Units_LY, sum(stock_anio_actual_alm) as WH_Stk_Eur_TY, sum(stock_anio_anterior_alm) as WH_Stk_Eur_LY, sum(stock_uds_anio_actual_alm) as WH_Stk_Units_TY, sum(stock_uds_anio_anterior_alm) as WH_Stk_Units_LY, case when tipo_tienda = 'WEB' then 'WEB' else 'OFFLINE' end Channel_Type from ventas_stock group by semana,mercado,Fare_Code,udp_code,udp_name,area,(case when area='Textil' then 'APPAREL' when area='Calzado' then 'FWR' when area in ('Material técnico','Complementos') then 'ACC&&HEQ' when area in ('Promociones','Otros','na','Servicios') then 'OTHER' end),serie,(case when serie in ('UNISEX') and area in ('Textil','Calzado','Complementos','Material técnico') then 'UNISEX' when serie in ('UNISEX','na') and area in ('Promociones','Otros','Servicios','na') then 'OTHER' when serie in ('CABALLERO','XMAN') and area in ('Textil','Calzado','Complementos','Material técnico') then 'MAN' when serie in ('SEÑORA','LADY','JR. x SRA.') and area in ('Textil','Calzado','Complementos','Material técnico') then 'MAN' else 'JCI' end),seccion_a,seccion,subseccion_a,familia,disenyo,marca,analytical_color,brand_desc,product_code,descripcion,Temporada, Temporalidad,Descatalogado,precio_original,precio_actual, case when tipo_tienda = 'WEB' then 'WEB' else 'OFFLINE' end; -- delete from PRODUCTO_SEMANAS_YTD where substring(Year,1,4)=ANIO_ACTUAL; delete from PRODUCTO_SEMANAS_YTD where substring(Year,1,4)=ANIO_ACTUAL and cast(semana as unsigned) BETWEEN SEMANA_INICIAL AND SEMANA_FINAL; insert into PRODUCTO_SEMANAS_YTD select * from PRODUCTO_YTD; drop table if exists PRODUCTO_YTD; /* TICKET SEMANAS YTD */ delete from TICKET_SEMANAS_YTD where substring(Año,1,4)=ANIO_ACTUAL and cast(Week as unsigned) between SEMANA_INICIAL AND SEMANA_FINAL; delete from TICKET_SEMANAS_NO_SERV_YTD where substring(Año,1,4)=ANIO_ACTUAL and cast(Week as unsigned) between SEMANA_INICIAL AND SEMANA_FINAL; insert into TICKET_SEMANAS_YTD select concat(ANIO_ACTUAL,'/',ANIO_ACTUAL-1,' ',a1.semana) as AñoSem, concat(ANIO_ACTUAL,'/',ANIO_ACTUAL-1) as Año, a1.branch_no as Branch_Id, a1.nombre_tienda as Branch_Name, i.mercado as Market, a1.tipo_tienda as Location, a1.subzona as Subzona, a1.delegado as Delegation_Manager, a1.zona as Region, a1.territorial as Territorial_Manager, (CASE when a1.dia=5 then 'Sat' when a1.dia=4 then 'Fri' when a1.dia=3 then 'Thu' when a1.dia=2 then 'Wed' when a1.dia=1 then 'Tue' when a1.dia=0 then 'Mon' when a1.dia=6 then 'Sun' end) as Weekday, h.Fecha as Date, a1.semana as Week, a1.mes as Month, coalesce(f.entradas_aact,0) as FF_TY, coalesce(g.entradas_aant,0) as FF_LY, (case when coalesce(f.entradas_aact,0)=0 or coalesce(g.entradas_aant,0)=0 then 0 else coalesce(f.entradas_aact,0) end) as FF_TY_LFL, (case when coalesce(g.entradas_aant,0)=0 or coalesce(f.entradas_aact,0)=0 then 0 else coalesce(g.entradas_aant,0) end) as FF_LY_LFL, coalesce(b.num_tickets_aact,0) as Transactions_TY, coalesce(c.num_tickets_aant,0) as Transactions_LY, /*(case when coalesce(f.entradas_aact,0)=0 or coalesce(g.entradas_aant,0)=0 then 0 else coalesce(b.num_tickets_aact,0) end) as Transactions_TY_LFL, (case when coalesce(g.entradas_aant,0)=0 or coalesce(f.entradas_aact,0)=0 then 0 else coalesce(c.num_tickets_aant,0) end) as Transactions_LY_LFL,*/ (case when coalesce(b.num_tickets_aact,0)=0 or coalesce(c.num_tickets_aant,0)=0 then 0 else coalesce(b.num_tickets_aact,0) end) as Transactions_TY_LFL, (case when coalesce(c.num_tickets_aant,0)=0 or coalesce(b.num_tickets_aact,0)=0 then 0 else coalesce(c.num_tickets_aant,0) end) as Transactions_LY_LFL, coalesce(a.vta_uds_Aact,0) as Unist_Sold_TY, coalesce(a.vta_uds_Aant,0) as Unist_Sold_LY, coalesce(a.vta_bruta_Aact,0) as Gross_Sales_TY, coalesce(a.vta_bruta_Aant,0) as Gross_Sales_LY, coalesce(a.vta_neta_Aact,0) as Net_Sales_TY, coalesce(a.vta_neta_Aant,0) as Net_Sales_LY, coalesce(a.vta_neta_Aact_lfl,0) as Net_Sales_TY_LFL, coalesce(a.vta_neta_Aant_lfl,0) as Net_Sales_LY_LFL, coalesce(j.Target_TY,0) as Target_TY, coalesce(j.GPV_Target_TY,0) as GPV_Target_TY, coalesce(a.mg_Aact,0) as GPV_TY, coalesce(a.mg_Aant,0) as GPV_LY, coalesce(a.mg_Aact_lfl,0) as GPV_TY_LFL, coalesce(a.mg_Aant_lfl,0) as GPV_LY_LFL, coalesce(d.num_tickets_aact_textil,0) as Transactions_TXT_TY, coalesce(e.num_tickets_aant_textil,0) as Transactions_TXT_LY, coalesce(d.num_tickets_aact_calzado,0) as Transactions_FWR_TY, coalesce(e.num_tickets_aant_calzado,0) as Transactions_FWR_LY, coalesce(d.num_tickets_aact_acc,0) as Transactions_ACC_HEQ_TY, coalesce(e.num_tickets_aant_acc,0) as Transactions_ACC_HEQ_LY, coalesce(a.stock_uds_aact,0) as Stock_Units_TY, coalesce(a.stock_uds_aant,0) as Stock_Units_LY, coalesce(a.stock_aact,0) as Stock_Euros_TY, coalesce(a.stock_aant,0) as Stock_Euros_LY from ticket_pre_a1 a1 left join ticket_pre a on a1.branch_no=a.branch_no and a1.semana=a.semana and a1.dia=a.dia left join tickets_aact b on a1.branch_no=b.branch_no and a1.semana=b.semana and a1.dia=b.dia left join tickets_aant c on a1.branch_no=c.branch_no and a1.semana=c.semana and a1.dia=c.dia left join tickets_areas_aact d on a1.branch_no=d.branch_no and a1.semana=d.semana and a1.dia=d.dia left join tickets_areas_aant e on a1.branch_no=e.branch_no and a1.semana=e.semana and a1.dia=e.dia left join footfall_aact f on a1.branch_no=f.branch_no and a1.semana=f.semana and a1.dia=f.dia left join footfall_aant g on a1.branch_no=g.branch_no and a1.semana=g.semana and a1.dia=g.dia left join md_dbg.calendario_Detalle_jd_v2 h on a1.dia=weekday(h.Fecha) and a1.semana=h.semana_jd and h.anio_jd=ANIO_ACTUAL left join tienda_zona_territorial_tipo_tienda i on a1.branch_no=i.branch_no left join objetivo_aact j on a1.branch_no=j.branch_no and a1.semana=j.semana and a1.dia=j.dia; /*where cast(a1.semana as unsigned) between SEMANA_INICIAL and SEMANA_FINAL*/ insert into TICKET_SEMANAS_NO_SERV_YTD select concat(ANIO_ACTUAL,'/',ANIO_ACTUAL-1,' ',a1.semana) as AñoSem, concat(ANIO_ACTUAL,'/',ANIO_ACTUAL-1) as Año, a1.branch_no as Branch_Id, a1.nombre_tienda as Branch_Name, i.mercado as Market, a1.tipo_tienda as Location, a1.subzona as Subzona, a1.delegado as Delegation_Manager, a1.zona as Region, a1.territorial as Territorial_Manager, (CASE when a1.dia=5 then 'Sat' when a1.dia=4 then 'Fri' when a1.dia=3 then 'Thu' when a1.dia=2 then 'Wed' when a1.dia=1 then 'Tue' when a1.dia=0 then 'Mon' when a1.dia=6 then 'Sun' end) as Weekday, h.Fecha as Date, a1.semana as Week, a1.mes as Month, coalesce(f.entradas_aact,0) as FF_TY, coalesce(g.entradas_aant,0) as FF_LY, (case when coalesce(f.entradas_aact,0)=0 or coalesce(g.entradas_aant,0)=0 then 0 else coalesce(f.entradas_aact,0) end) as FF_TY_LFL, (case when coalesce(g.entradas_aant,0)=0 or coalesce(f.entradas_aact,0)=0 then 0 else coalesce(g.entradas_aant,0) end) as FF_LY_LFL, coalesce(b.num_tickets_aact,0) as Transactions_TY, coalesce(c.num_tickets_aant,0) as Transactions_LY, /*(case when coalesce(f.entradas_aact,0)=0 or coalesce(g.entradas_aant,0)=0 then 0 else coalesce(b.num_tickets_aact,0) end) as Transactions_TY_LFL, (case when coalesce(g.entradas_aant,0)=0 or coalesce(f.entradas_aact,0)=0 then 0 else coalesce(c.num_tickets_aant,0) end) as Transactions_LY_LFL,*/ (case when coalesce(b.num_tickets_aact,0)=0 or coalesce(c.num_tickets_aant,0)=0 then 0 else coalesce(b.num_tickets_aact,0) end) as Transactions_TY_LFL, (case when coalesce(c.num_tickets_aant,0)=0 or coalesce(b.num_tickets_aact,0)=0 then 0 else coalesce(c.num_tickets_aant,0) end) as Transactions_LY_LFL, coalesce(a.vta_uds_Aact,0) as Unist_Sold_TY, coalesce(a.vta_uds_Aant,0) as Unist_Sold_LY, coalesce(a.vta_bruta_Aact,0) as Gross_Sales_TY, coalesce(a.vta_bruta_Aant,0) as Gross_Sales_LY, coalesce(a.vta_neta_Aact,0) as Net_Sales_TY, coalesce(a.vta_neta_Aant,0) as Net_Sales_LY, coalesce(a.vta_neta_Aact_lfl,0) as Net_Sales_TY_LFL, coalesce(a.vta_neta_Aant_lfl,0) as Net_Sales_LY_LFL, coalesce(j.Target_TY,0) as Target_TY, coalesce(j.GPV_Target_TY,0) as GPV_Target_TY, coalesce(a.mg_Aact,0) as GPV_TY, coalesce(a.mg_Aant,0) as GPV_LY, coalesce(a.mg_Aact_lfl,0) as GPV_TY_LFL, coalesce(a.mg_Aant_lfl,0) as GPV_LY_LFL, coalesce(d.num_tickets_aact_textil,0) as Transactions_TXT_TY, coalesce(e.num_tickets_aant_textil,0) as Transactions_TXT_LY, coalesce(d.num_tickets_aact_calzado,0) as Transactions_FWR_TY, coalesce(e.num_tickets_aant_calzado,0) as Transactions_FWR_LY, coalesce(d.num_tickets_aact_acc,0) as Transactions_ACC_HEQ_TY, coalesce(e.num_tickets_aant_acc,0) as Transactions_ACC_HEQ_LY, coalesce(a.stock_uds_aact,0) as Stock_Units_TY, coalesce(a.stock_uds_aant,0) as Stock_Units_LY, coalesce(a.stock_aact,0) as Stock_Euros_TY, coalesce(a.stock_aant,0) as Stock_Euros_LY from ticket_pre_a1 a1 left join ticket_pre_NO_SERV a on a1.branch_no=a.branch_no and a1.semana=a.semana and a1.dia=a.dia left join tickets_aact_NO_SERV b on a1.branch_no=b.branch_no and a1.semana=b.semana and a1.dia=b.dia left join tickets_aant_NO_SERV c on a1.branch_no=c.branch_no and a1.semana=c.semana and a1.dia=c.dia left join tickets_areas_aact d on a1.branch_no=d.branch_no and a1.semana=d.semana and a1.dia=d.dia left join tickets_areas_aant e on a1.branch_no=e.branch_no and a1.semana=e.semana and a1.dia=e.dia left join footfall_aact f on a1.branch_no=f.branch_no and a1.semana=f.semana and a1.dia=f.dia left join footfall_aant g on a1.branch_no=g.branch_no and a1.semana=g.semana and a1.dia=g.dia left join md_dbg.calendario_Detalle_jd_v2 h on a1.dia=weekday(h.Fecha) and a1.semana=h.semana_jd and h.anio_jd=ANIO_ACTUAL left join tienda_zona_territorial_tipo_tienda i on a1.branch_no=i.branch_no left join objetivo_aact j on a1.branch_no=j.branch_no and a1.semana=j.semana and a1.dia=j.dia; /*where cast(a1.semana as unsigned) between SEMANA_INICIAL and SEMANA_FINAL */ /* TIENDA-AREA2 SEMANAS YTD */ /* Se añaden los datos semanales de TIENDA-AREA2 a la tabla histórica de TIENDA-AREA2-SEMANAS-YTD */ -- delete from TIENDA_AREA2_SEMANAS_YTD where substring(Año,1,4)=ANIO_ACTUAL; delete from TIENDA_AREA2_SEMANAS_YTD where AñoSem between concat(ANIO_ACTUAL,'/',ANIO_ACTUAL-1,' ', SEMANA_ACTUAL) and concat(ANIO_ACTUAL,'/',ANIO_ACTUAL-1,' ', SEMANA_FINAL_STR); insert into TIENDA_AREA2_SEMANAS_YTD select concat(ANIO_ACTUAL,'/',ANIO_ACTUAL-1,' ',semana) as AñoSem, concat(ANIO_ACTUAL,'/',ANIO_ACTUAL-1) as Año, mercado as Market, branch_no AS IdTienda, nombre_tienda as Tienda, (case when area in ('Complementos','Material técnico') then 'Complementos y Técnico' when area in ('Promociones','Servicios','Otros','na') then 'Otros' else area end) as Area2, sum(venta_neta_anio_actual) as Net_Sales_TY, sum(venta_neta_anio_anterior) as Net_Sales_LY, sum(margen_anio_actual) as GPV_TY, sum(margen_anio_anterior) as GPV_LY, sum(stock_unidades_actual) as Stock_Units_TY, sum(stock_unidades_anterior) as Stock_Units_LY, sum(stock_anio_actual) as Stock_Euros_TY, sum(stock_anio_anterior) as Stock_Euros_LY from ventas_stock WHERE EsTienda=1 group by concat(ANIO_ACTUAL,'/',ANIO_ACTUAL-1,' ',semana), mercado,branch_no,(case when area in ('Complementos','Material técnico') then 'Complementos y Técnico' when area in ('Promociones','Servicios','Otros','na') then 'Otros' else area end); -- END IF; drop table if exists objetivo_aact; drop table if exists objetivo_aant; -- ----------------------------------------------------------------------------------------------------------------------------------------------------- /* INFORME VENTAS MULTICANAL Y WEB ESPAÑA*/ -- ----------------------------------------------------------------------------------------------------------------------------------------------------- drop table if exists multi_web_pre; create table multi_web_pre as select c.udp_code, c.udp_name, d.mercado, a.channel_code, e.channel_desc, b.anio_jd as anio, b.mes_jd as mes, b.semana_jd as semana, c.area, c.serie, c.seccion_a as seccion, c.familia, c.marca, a.product_code, c.descripcion, c.original_price as pvp_ini, sum(a.qty) as vta_uds, d.fare_code from consultas.rep_pr_sales_detail a left join md_dbg.calendario_Detalle_jd_v2 b on a.sales_Date=b.fecha left join consultas.articulos c on a.product_code=c.product_code inner join tiendas_ica d on a.branch_no=d.branch_no left join md_dbg.channels e on e.channel_code=a.channel_code where a.sales_Date>=date_add(FECHA_INICIAL_SEMANA_FINAL_JD,interval -7*12 day) and a.sales_date<=FECHA_FINAL and a.channel_code<>8 group by d.fare_code,d.mercado,a.channel_code,b.anio_jd,b.mes_jd,b.semana_jd,a.product_code; drop table if exists multi_web_precios; create table multi_web_precios as select c.product_code, c.fare_code, c.anio_jd, c.semana_jd, d.price_after as pvp_actual from (select a.product_code, a.fare_code, b.anio_jd, b.semana_jd, max(a.pvp_Date) as pvp_Date from md_Dbg.pvps_fares a left join md_Dbg.calendario_detalle_jd_v2 b on a.pvp_date=b.fecha where pvp_date<=FECHA_FINAL group by a.fare_code,a.product_code,b.anio_jd,b.semana_jd) c left join md_dbg.pvps_fares d on c.pvp_date=d.pvp_date and c.product_code=d.product_code and c.fare_code=d.fare_code; alter table multi_web_precios add index (product_code,anio_jd,semana_jd,fare_code); alter table multi_web_pre add index (product_code,anio,semana,fare_code); drop table if exists multi_web; create table multi_web as select c.udp_code, c.udp_name, c.mercado, c.fare_code as tarifa, c.channel_code, c.channel_desc, c.anio, c.mes, c.semana, c.area, c.serie, c.seccion, c.familia, c.marca, c.product_code, c.descripcion, c.pvp_ini, coalesce(d.pvp_actual,c.pvp_ini) as pvp_actual, c.vta_uds from (select a.udp_code, a.udp_name, a.mercado, a.fare_code, a.channel_code, a.channel_desc, a.anio, a.mes, a.semana, a.area, a.serie, a.seccion, a.familia, a.marca, a.product_code, a.descripcion, a.pvp_ini, a.vta_uds, max(concat(b.anio_jd,b.semana_jd)) as anio_semana_jd from multi_web_pre a left join multi_web_precios b on a.product_code=b.product_code and concat(a.anio,a.semana)>=concat(b.anio_jd,b.semana_jd) and a.fare_code=b.fare_code group by mercado,fare_code,channel_code,product_code,anio,semana) c left join multi_web_precios d on c.product_code=d.product_code and c.anio_semana_jd=concat(d.anio_jd,d.semana_jd) and c.fare_code=d.fare_code; /*---------------------------------------------------------------------*/ /* INFORME VENTAS MULTICANAL Y WEB ESPAÑA POR TALLA (LGDS-350 20211007)*/ drop table if exists multi_web_pre2; create table multi_web_pre2 as select d.mercado, a.channel_code, e.channel_desc, b.anio_jd as anio, b.mes_jd as mes, b.semana_jd as semana, c.area, c.serie, c.seccion_a as seccion, c.familia, c.marca, a.product_code, a.size_pos, c.descripcion, c.original_price as pvp_ini, sum(a.qty) as vta_uds, d.fare_code, concat(a.season_code,"_",a.size_pos) as partida from consultas.rep_pr_sales_detail a left join md_dbg.calendario_Detalle_jd_v2 b on a.sales_Date=b.fecha left join consultas.articulos c on a.product_code=c.product_code inner join tiendas_ica d on a.branch_no=d.branch_no left join md_dbg.channels e on e.channel_code=a.channel_code where a.sales_Date>=date_add(FECHA_INICIAL_SEMANA_FINAL_JD,interval -7*12 day) and a.sales_date<=FECHA_FINAL and a.channel_code<>8 group by d.fare_code,d.mercado,a.channel_code,b.anio_jd,b.mes_jd,b.semana_jd,a.product_code,a.size_pos; alter table multi_web_pre2 add index (product_code,anio,semana,fare_code); drop table if exists multi_web2; create table multi_web2 as select c.mercado, c.fare_code as tarifa, c.channel_code, c.channel_desc, c.anio, c.mes, c.semana, c.area, c.serie, c.seccion, c.familia, c.marca, c.product_code, c.descripcion, c.partida, c.Size_pos, c.talla, c.pvp_ini, coalesce(d.pvp_actual,c.pvp_ini) as pvp_actual, c.vta_uds from (select a.mercado, a.fare_code, a.channel_code, a.channel_desc, a.anio, a.mes, a.semana, a.area, a.serie, a.seccion, a.familia, a.marca, a.product_code, a.descripcion, a.Size_pos, a.pvp_ini, a.vta_uds as vta_uds, a.partida, sz.size_desc as talla, max(concat(b.anio_jd,b.semana_jd)) as anio_semana_jd from multi_web_pre2 a left join multi_web_precios b on a.product_code=b.product_code and concat(a.anio,a.semana)>=concat(b.anio_jd,b.semana_jd) and a.fare_code=b.fare_code left join md_dbg.products p on a.product_code=p.product_code left join md_dbg.size sz on p.size_code=sz.size_code and a.size_pos=sz.size_pos group by a.mercado, a.fare_code, a.channel_code, a.channel_desc, a.anio, a.mes, a.semana, a.area, a.serie, a.seccion, a.familia, a.marca, a.product_code, a.descripcion, a.Size_pos, a.pvp_ini, a.partida, sz.size_desc) c left join multi_web_precios d on c.product_code=d.product_code and c.anio_semana_jd=concat(d.anio_jd,d.semana_jd) and c.fare_code=d.fare_code; DROP TABLE IF exists multi_web_pre2; /* INFORME VENTAS MULTICANAL Y WEB ESPAÑA POR TALLA (LGDS-350 20211007)*/ /*---------------------------------------------------------------------*/ DROP TABLE IF exists multi_web_pre; DROP TABLE IF exists multi_web_precios; drop table if exists tmp_iva_aact; drop table if exists tmp_iva_aact_branch; drop table if exists tmp_iva_aant; drop table if exists tmp_iva_aant_branch; drop table if exists iva_aact; drop table if exists iva_aant; drop table if exists ticket_pre_a; drop table if exists ticket_pre_a1; END
Stuck with a problem? Got Error? Ask ChatGPT!
Copy Clear