dbo.obtener_comex_totales

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

2
Name Data Type Length Description
@cuit varchar 50
@meses int 4

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
/*
EXEC dbo.obtener_comex_totales '30500548041',24
exec [obtener_comex_totales] '30502874353',12

exec obtener_comex_totales '30590360763'
*/

CREATE OR ALTER PROCEDURE dbo.obtener_comex_totales 
@cuit varchar(50), 
@meses int = 24

AS
BEGIN
 
    SET NOCOUNT ON;
 DECLARE @total_impo decimal(15,2)
 DECLARE @total_expo decimal(15,2)
 set @meses = @meses * (-1)
 declare @max_periodo date,@min_periodo date
 set @max_periodo  = (select max(fecha_informacion) as fecha_informacion from fuentes_procesadas where fuente='comex_pais_rubro' and visible =1)
 set @min_periodo  = (select DATEADD(month, @meses, max(fecha_informacion)) as fecha_informacion from fuentes_procesadas where fuente='comex_pais_rubro' and visible =1)

    begin TRY
        set @total_impo =  ( select sum(ROUND (replace(fobusd,',','.'),0)) from comex_importaciones where cuit = @cuit  and  fecha between cast(@min_periodo  as varchar) and cast(@max_periodo  as varchar))
  set @total_expo =  ( select sum(ROUND (replace(fobusd,',','.'),0))  
        --from comex_exportaciones 
        /* 24/01/2025: Porque hay muchos registros que se repiten muchas veces */
        from (select distinct cuit, procedencia, fobusd, posicion, fecha
         from comex_exportaciones 
         where cuit = @cuit  and  fecha  between cast(@min_periodo  as varchar) and cast(@max_periodo  as varchar)
        ) ce 
        /**/
        where cuit = @cuit  and  fecha  between cast(@min_periodo  as varchar) and cast(@max_periodo  as varchar))
 end TRY
    begin CATCH
    
    end CATCH




 select @total_impo as total_impo , @total_expo as total_expo
END

GO