dbo.obtener_comex_pais

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

1
Name Data Type Length Description
@cuit varchar 13

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
--exec obtener_comex_pais '30500548041'

CREATE OR ALTER PROCEDURE dbo.obtener_comex_pais
@cuit varchar(13)

AS
BEGIN
 
SET NOCOUNT ON;

/* 10/03/2021 agb agrega el join con rubros porque sino las cuentas no dan lo mismo que en el store por rubros. Hay posiciones arancelarias que no tenemos registradas */
declare @max_periodo date,@min_periodo date
set @max_periodo  = (select max(fecha_informacion) from fuentes_procesadas where fuente='comex_pais_rubro' and visible =1)
set @min_periodo  = (select DATEADD(month, -24, max(@max_periodo)) as fecha_informacion)

SELECT 'IMPO' as tipo_operacion, nombre,sum(ROUND (replace(fobusd,',','.'),0)) as fob FROM comex_importaciones 
inner join comex_paises on procedencia = comex_paises.codigo
inner join comex_rubros on comex_rubros.rubro = substring(comex_importaciones.posicion,1,4)
WHERE CUIT = @cuit and fecha between @min_periodo and @max_periodo
group by nombre 

union all

SELECT 'EXPO' as tipo_operacion,nombre,sum(ROUND (replace(fobusd,',','.'),0)) as fob 
--FROM comex_exportaciones ce 
/* 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 @min_periodo and @max_periodo
   ) ce 
/**/
inner join comex_paises on procedencia = comex_paises.codigo
inner join comex_rubros on comex_rubros.rubro = substring(ce.posicion,1,4)
WHERE CUIT = @cuit and fecha between @min_periodo and @max_periodo
group by nombre order by tipo_operacion,fob desc


END

GO

Used By

No items found