Aggiunta where su idBdg in RG_AnalisiVendite_KPI

This commit is contained in:
2024-06-11 15:16:08 +02:00
parent 6a39ca8972
commit 917cf529d8

View File

@@ -0,0 +1,117 @@
package it.integry.ems.migration.model;
import it.integry.ems.migration._base.BaseMigration;
import it.integry.ems.migration._base.IntegryCustomerDB;
import it.integry.ems.migration._base.MigrationModelInterface;
public class Migration_20240611120635 extends BaseMigration implements MigrationModelInterface {
@Override
public void up() throws Exception {
if (isHistoryDB())
return;
if (!isCustomerDb(IntegryCustomerDB.RossoGargano_RossoGargano))
return;
createOrUpdateFunction("RG_AnalisiVendite_KPI", "CREATE FUNCTION [dbo].[RG_AnalisiVendite_KPI](@idBdg BIGINT, @formato VARCHAR(40) = NULL, @famiglia VARCHAR(40) = NULL)\n" +
" RETURNS TABLE AS\n" +
" RETURN(WITH detailTab AS\n" +
" (SELECT YEAR(vtb_bdgt.budget_da) AS AnnoFiscale,\n" +
" ISNULL(art.cod_mart_Stat, art.cod_mart) AS CodArticolo,\n" +
" ISNULL(art_Stat.descrizione, art.descrizione) AS Articolo,\n" +
" mtb_sgrp.descrizione AS famiglia,\n" +
" mtb_tipi.descrizione AS formato,\n" +
" ms.descrizione AS sottoformato,\n" +
" ROUND(SUM(car.CAR_QTA * art.qta_cnf), 0) AS QtaProdotta,\n" +
" 0 AS QtaVenduta,\n" +
" 0 AS QtaBudget,\n" +
" 0 AS Giacenza\n" +
"\n" +
" FROM RossoGarganoExchange.dbo.WCARICHI_TAB Car\n" +
" INNER JOIN (SELECT DISTINCT SCAR_ANNO_C, SCAR_TIPO_C, SCAR_NUM_C\n" +
" FROM RossoGarganoExchange.dbo.WSCARICHI_TAB) scar\n" +
" ON car.CAR_TIPO = scar.SCAR_TIPO_C AND\n" +
" car.CAR_ANNO = scar.SCAR_ANNO_C AND\n" +
" car.CAR_NUMERO = scar.SCAR_NUM_C\n" +
" INNER JOIN rossoGargano.dbo.mtb_aart art ON Car.CAR_ART = art.cod_mart\n" +
" LEFT OUTER JOIN RossoGargano.dbo.mtb_aart art_stat\n" +
" ON art.cod_mart_stat = art_stat.cod_mart\n" +
" INNER JOIN mtb_grup ON art.cod_mgrp = mtb_grup.cod_mgrp\n" +
" INNER JOIN mtb_sgrp\n" +
" ON art.cod_mgrp = mtb_sgrp.cod_mgrp AND art.cod_msgr = mtb_sgrp.cod_msgr\n" +
" LEFT OUTER JOIN mtb_tipi ON art.cod_mtip = mtb_tipi.cod_mtip\n" +
" LEFT OUTER JOIN mtb_stip ms\n" +
" ON art_stat.cod_mtip = ms.cod_mtip AND art_stat.cod_mstp = ms.cod_mstp\n" +
" INNER JOIN vtb_bdgt ON id_bdg = @idBdg\n" +
" INNER JOIN RossoGarganoExchange.dbo.mtb_partitamag_prefisso\n" +
" ON mtb_partitamag_prefisso.Anno =\n" +
" YEAR(rossogargano.dbo.vtb_bdgt.budget_da)\n" +
" WHERE CAR_DTINS >= CONVERT(VARCHAR(4), mtb_partitamag_prefisso.anno) + '/01/01'\n" +
" AND mtb_grup.tipo_mgrp = 'SL'\n" +
" AND car_lotto LIKE mtb_partitamag_prefisso.prefisso + '%'\n" +
" GROUP BY YEAR(vtb_bdgt.budget_da), art.cod_mart, art.descrizione, mtb_sgrp.descrizione,\n" +
" mtb_tipi.descrizione, ms.descrizione, art.cod_mart_stat, art_stat.descrizione\n" +
"\n" +
" UNION ALL\n" +
" SELECT anno_c AS AnnoFiscale,\n" +
" cod_mart AS codArticolo,\n" +
" descrizione AS Articolo,\n" +
" famiglia,\n" +
" formato,\n" +
" sottoformato,\n" +
" 0 AS qta_prodotta,\n" +
" ROUND(SUM(qta_vend_anno_c), 0) AS qta_venduta,\n" +
" 0 AS budget,\n" +
" 0 AS giacenza\n" +
" FROM dbo.[RG_AnalisiVenditeAnno](@idBdg, 0)\n" +
" GROUP BY anno_c, cod_mart, descrizione, formato, famiglia, sottoformato\n" +
"\n" +
" UNION ALL\n" +
" SELECT YEAR(vtb_bdgt.budget_da) AS anno_fisc,\n" +
" art.cod_mart,\n" +
" art.descrizione,\n" +
" mtb_sgrp.descrizione AS famiglia,\n" +
" mtb_tipi.descrizione AS formato,\n" +
" ms.descrizione AS sottoformato,\n" +
" 0 AS qta_prodotta,\n" +
" 0 AS qta_venduta,\n" +
" ROUND(SUM(vtb_bdgr.qta_budget), 0) AS qtaBudget,\n" +
" 0 AS giacenza\n" +
" FROM vtb_bdgt\n" +
" INNER JOIN vtb_bdgr ON vtb_bdgt.id_bdg = vtb_bdgr.id_bdg\n" +
" INNER JOIN mtb_aart art ON vtb_bdgr.cod_mart = art.cod_mart\n" +
" INNER JOIN mtb_sgrp\n" +
" ON art.cod_mgrp = mtb_sgrp.cod_mgrp AND art.cod_msgr = mtb_sgrp.cod_msgr\n" +
" INNER JOIN mtb_tipi ON art.cod_mtip = mtb_tipi.cod_mtip\n" +
" LEFT OUTER JOIN mtb_stip ms\n" +
" ON art.cod_mtip = ms.cod_mtip AND art.cod_mstp = ms.cod_mstp\n" +
" AND vtb_bdgt.id_bdg = @idBdg\n" +
" WHERE vtb_bdgt.id_bdg = @idBdg\n" +
" GROUP BY YEAR(vtb_bdgt.budget_da),\n" +
" art.cod_mart,\n" +
" art.descrizione,\n" +
" mtb_sgrp.descrizione,\n" +
" mtb_tipi.descrizione,\n" +
" ms.descrizione)\n" +
" SELECT AnnoFiscale,\n" +
" CodArticolo,\n" +
" Articolo,\n" +
" famiglia,\n" +
" formato,\n" +
" sottoformato,\n" +
" SUM(qtaProdotta) AS qtaProdotta,\n" +
" SUM(qtaVenduta) AS qtaVenduta,\n" +
" SUM(qtaBudget) AS qtaBudget\n" +
" FROM detailTab\n" +
" WHERE (@formato IS NULL OR formato = @formato)\n" +
" AND (@famiglia IS NULL OR famiglia = @famiglia)\n" +
" GROUP BY AnnoFiscale, CodArticolo, Articolo, famiglia, formato, sottoformato)");
}
@Override
public void down() throws Exception {
}
}