dbo.obtener_comex_rubros

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 20

SQL Script

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

CREATE OR ALTER PROCEDURE dbo.obtener_comex_rubros
@cuit varchar(20)

AS
BEGIN
 
SET NOCOUNT ON;
 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, comex_rubros.descripcion,sum(ROUND (replace(fobusd,',','.'),0)) as fob,posicion FROM comex_importaciones 
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 descripcion,posicion
union 
SELECT 'EXPO' as tipo_operacion,descripcion,sum(ROUND (replace(fobusd,',','.'),0)) as fob,posicion 
--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_rubros on comex_rubros.rubro = substring(ce.posicion,1,4)
WHERE CUIT = @cuit and fecha between @min_periodo and @max_periodo
group by descripcion,posicion order by tipo_operacion,fob desc

END

GO

Used By

No items found