dbo.obtener_comex

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

1
Name Data Type Length Description
@p_Cuit varchar 20

SQL Script

Copy
SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO

--exec obtener_comex '30714532215'

CREATE OR ALTER PROCEDURE dbo.obtener_comex 
@p_Cuit varchar(20)
AS


BEGIN
       SET NOCOUNT ON;
/*
select comercioexterior.mes,ComercioExterior.anio, 0 as consumo,0 as temporal,0 as deposito,0 as zona_franca,0 as total
from comercioexterior inner join comercioexterior_operacion on ComercioExterior.id_operacion = ComercioExterior_Operacion.id_operacion 
where comercioexterior.cuit = @p_Cuit
and procesado > GETDATE() - 770
group by mes, anio
order by anio desc, mes desc
*/

/*
select distinct top 24 ComercioExterior.mes,ComercioExterior.anio,isnull(importe,0) as consumo,isnull(pTemporal.temporal,0) as temporal,ComercioExterior.procesado,GETDATE() as date,GETDATE()-770 as Date,isnull(pdeposito.deposito,0) as deposito ,isnull(pZF.zonafranca,0) as zonafranca,
(isnull(importe,0) + isnull(pTemporal.temporal,0) + isnull(pdeposito.deposito,0) + isnull(pZF.zonafranca,0))  as total
from ComercioExterior left join (select cuit,anio,mes,importe as temporal from ComercioExterior 
where id_operacion = 83 and cuit = @p_Cuit) ptemporal on ptemporal.cuit = ComercioExterior.cuit and ptemporal.anio = ComercioExterior.anio


and ptemporal.mes = ComercioExterior.mes 
left join (select cuit,anio,mes,importe as deposito from ComercioExterior 
where id_operacion = 89 and cuit = @p_Cuit) pdeposito on pdeposito.cuit = ComercioExterior.cuit and pdeposito.anio = ComercioExterior.anio
and pdeposito.mes = ComercioExterior.mes 

left join (select cuit,anio,mes,importe as zonafranca from ComercioExterior 
where id_operacion = 21 and cuit = @p_Cuit) pZF on pZF.cuit = ComercioExterior.cuit and pZF.anio = ComercioExterior.anio
and pZF.mes = ComercioExterior.mes 
where id_operacion = 12 and ComercioExterior.cuit = @p_Cuit
and ComercioExterior.anio >= DATEPART(year, DATEADD(month, -1, GETDATE())) -2
order by  ComercioExterior.anio desc ,ComercioExterior.mes desc
*/


/*
declare @max_periodo date
set @max_periodo = (select fecha_informacion from fuentes_procesadas where fuente='comercio_exterior' and visible =1)
select periodo as date,consumo,deposito,zona_franca as zonafranca,temporal,
(isnull(consumo,0)+isnull(deposito,0)+isnull(zona_franca,0)+isnull(temporal,0)) as total, 
format(MONTH(periodo),'00') as mes ,
YEAR(PERIODO) as anio 
from comercio_exterior where
periodo between  DATEADD(MONTH, -24, @max_periodo )  and @max_periodo and cuit = @p_cuit
order by  anio desc ,mes desc

*/


  declare @max_periodo date, @min_periodo date
  declare @max_periodo_uno date,  @max_periodo_ultimo date, @min_periodo_uno date
  set @max_periodo  = (select max(fecha_informacion) as fecha_informacion from fuentes_procesadas where fuente='comercio_exterior' and visible = 1)
  set @max_periodo_uno = convert(date,SUBSTRING(convert(char,@max_periodo),0,8)+'-01')
  set @max_periodo_ultimo = DATEADD(day, -1, (DATEADD(month, +1, @max_periodo_uno)))
  set @min_periodo  = (select DATEADD(month, -24, max(fecha_informacion)) as fecha_informacion from fuentes_procesadas where fuente='comercio_exterior' and visible =1)
  set @min_periodo_uno = convert(date,SUBSTRING(convert(char,@min_periodo),0,8)+'-01')

/* select @min_periodo_uno, @max_periodo_ultimo*/

 select distinct periodo as date,
   consumo,deposito, 
   zona_franca as zonafranca,
   temporal,
   (isnull(consumo,0)+isnull(deposito,0)+isnull(zona_franca,0)+isnull(temporal,0)) as total, 
   format(MONTH(periodo),'00') as mes ,
   YEAR(PERIODO) as anio 
    from comercio_exterior 
   where periodo between @min_periodo_uno and @max_periodo_ultimo
     and cuit = @p_cuit
 order by  anio desc ,mes desc
END

GO

Used By

No items found