LovelessIsma

pa_reporteConsoliadoRO

Jan 27th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 14.73 KB | None | 0 0
  1. USE [SNSMART_ANALISIS]
  2. GO
  3. /****** Object:  StoredProcedure [SN].[pa_reporteConsoliadoRO]    Script Date: 27/01/2017 10:00:38 ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8.  
  9. ALTER procedure [SN].[pa_reporteConsoliadoRO] (
  10.     @fechaini datetime,
  11.     @fechafin datetime
  12. )
  13. AS
  14. BEGIN
  15.  
  16.  
  17.    
  18.    
  19.            
  20.                    
  21. SELECT case MONTH(@fechaini)when 1 then 'ENERO'
  22.                 when 2 then 'FEBRERO'
  23.                 when 3 then 'MARZO'
  24.                 when 4 then 'ABRIL'
  25.                 when 5 then 'MAYO'
  26.                 when 6 then 'JUNIO'
  27.                 when 7 then 'JULIO'
  28.                 when 8 then 'AGOSTO'
  29.                 when 9 then 'SEPTIEMBRE'
  30.                 when 10 then 'OCTUBRE'
  31.                 when 11 then 'NOVIEMBRE'
  32.                 else 'DICIEMBRE' end as MES, COUNT(nroescritura), SUM(MontoOperacion) AS Total FROM (
  33. select distinct * from (
  34.  
  35.             select '2' as tipoesc,
  36.              convert(int, SUBSTRING(a.Tm_NumeroEscritura, 6, 5)) nroescritura,
  37.                                 SN.fc_formatearFecha('03', a.tm_fechafirmaesc) as fechaNum,
  38.                 s.tm_codigoROF + ': ' + s.tm_nombreROF as Moneda,
  39.                 p.Td_ValorExpVeh as MontoOperacion
  40.                 --,
  41.                 --              convert(varchar(max), round(100 / j.CountPersona, 2)) as porcentaje,
  42.                 --                  j.CountPersona ,
  43.                 --                  b.Td_idexpedienteacto,
  44.                 --                  i.Ta_DefTipoInterv
  45.                                 from SN.Tm_Escritura  as a
  46.                                     inner join SN.Td_Expediente_Acto as b on a.Tm_IdExpediente = b.Tm_idexpediente
  47.                                     inner join SN.Tp_Actos as c on b.Tp_idacto = c.Tp_IdActo
  48.                                     inner join SN.Td_Expediente_Interv as f on b.Td_idexpedienteacto = f.Td_IdExpedienteActo
  49.                                     inner join SN.vstInterviniente2 as h on f.Tp_IdPersona = h.Tp_idpersona
  50.                                     inner join SN.Tp_TipoInterv as i on f.Tp_IdTipointerv = i.Tp_IdTipoInterv
  51.                                     inner join SYSMART.dbo.ta_tabla as l on h.Ta_TipoIdentifica = l.ta_codigo and l.ta_campo = 'Ta_TipoIdentifica'
  52.                                     left join SYSMART.dbo.ta_tabla as m on h.Ta_Nacionalidad = m.ta_codigo and m.ta_campo = 'Ta_Nacionalidad'
  53.                                     left join SYSMART.dbo.ta_tabla as n on h.Ta_EstCivil = n.ta_codigo and n.ta_campo = 'Ta_EstCivil'
  54.                                    
  55.                                     inner join SYSMART.dbo.tm_ubigeo as oDist on h.tm_idubigeo = oDist.tm_idubigeo
  56.                                     inner join SYSMART.dbo.tm_ubigeo as oProv2 on oDist.tm_refcodigo_ubi = oProv2.tm_idubigeo
  57.                                     inner join SYSMART.dbo.tm_ubigeo as oDep1 on oProv2.tm_refcodigo_ubi = oDep1.tm_idubigeo
  58.                                    
  59.                                     inner join SN.Td_Expediente_Vehiculo as p on b.Td_idexpedienteacto = p.Td_IdExpedienteActo
  60.                                     inner join SYSMART.dbo.tm_forma_pago as q on p.Tm_Idformapago = q.tm_idformapago
  61.                                    
  62.                                     inner join SYSMART.dbo.ta_tabla as r on p.Ta_MedioPago = r.ta_codigo and r.ta_campo = 'Ta_MedioPago'
  63.                                    
  64.                                     inner join SYSMART.dbo.Tm_moneda as s on p.Tm_IdMoneda = s.tm_idmoneda
  65.                                     inner join SN.Tp_Vehiculos as t on p.Tp_IdVehiculo = t.Tp_IdVehiculo
  66.                                 where  a.Tm_FechaGenEsc >= @fechaini and a.Tm_FechaGenEsc <= @fechafin
  67.                                     and i.Ta_DefTipoInterv in ('00', '01')
  68.                                     and f.Tp_IdTipointerv <> 15
  69.                                     and a.Tm_idRegistro = 9
  70.                                     and not a.Tm_idescritura in (4717, 4744, 5696, 6354, 6476, 6482, 6541, 7126, 7152, 7380, 8085, 8147, 8151, 8154, 8163, 8198, 8244, 8304, 8489, 8516)  --- ESCRITURAS EXCLUIDAS
  71.                                     and not c.Ta_NatActo is null
  72.                                 and not c.Tp_IdActo in (17, 24, 33, 104, 34, 147, 47, 125, 148, 119,
  73.             40,
  74.             39,
  75.             38,
  76.             37,
  77.             36,
  78.             35)
  79.  
  80.             and case s.tm_codigoROF when 'PEN' then (p.Td_ValorExpVeh / 2.821) when 'USD' then p.Td_ValorExpVeh else (p.Td_ValorExpVeh / 3.521) end > 2500
  81. union all
  82.    
  83.             select
  84.             '1' as tipoesc,
  85.             convert(int, SUBSTRING(Tm_NumeroEscritura, 6, 5)) nroescritura,
  86.             SN.fc_formatearFecha('03', Tm_FechaGenEsc) as fechaNum,
  87.                 Moneda,
  88.                 MontoOperacion from (
  89.            
  90.             select a.Tm_NumeroEscritura,
  91.                                 a.Tm_FechaGenEsc,
  92.                 s.tm_codigoROF + ': ' + s.tm_nombreROF as Moneda,
  93.                 p.Td_ValorExpTrans as MontoOperacion
  94.                 --,
  95.                 --              convert(varchar(max), round(100 / j.CountPersona, 2)) as porcentaje,
  96.                 --                  j.CountPersona ,
  97.                 --                  b.Td_idexpedienteacto,
  98.                 --                  i.Ta_DefTipoInterv
  99.                                 from SN.Tm_Escritura  as a
  100.                                     inner join SN.Td_Expediente_Acto as b on a.Tm_IdExpediente = b.Tm_idexpediente
  101.                                     inner join SN.Tp_Actos as c on b.Tp_idacto = c.Tp_IdActo
  102.                                     inner join SN.Td_Expediente_Interv as f on b.Td_idexpedienteacto = f.Td_IdExpedienteActo
  103.                                     inner join SN.vstInterviniente2 as h on f.Tp_IdPersona = h.Tp_idpersona
  104.                                     inner join SN.Tp_TipoInterv as i on f.Tp_IdTipointerv = i.Tp_IdTipoInterv
  105.                                     inner join SYSMART.dbo.ta_tabla as l on h.Ta_TipoIdentifica = l.ta_codigo and l.ta_campo = 'Ta_TipoIdentifica'
  106.                                     left join SYSMART.dbo.ta_tabla as m on h.Ta_Nacionalidad = m.ta_codigo and m.ta_campo = 'Ta_Nacionalidad'
  107.                                     left join SYSMART.dbo.ta_tabla as n on h.Ta_EstCivil = n.ta_codigo and n.ta_campo = 'Ta_EstCivil'
  108.                                    
  109.                                     inner join SYSMART.dbo.tm_ubigeo as oDist on h.tm_idubigeo = oDist.tm_idubigeo
  110.                                     inner join SYSMART.dbo.tm_ubigeo as oProv2 on oDist.tm_refcodigo_ubi = oProv2.tm_idubigeo
  111.                                     inner join SYSMART.dbo.tm_ubigeo as oDep1 on oProv2.tm_refcodigo_ubi = oDep1.tm_idubigeo
  112.                                    
  113.                                     inner join SN.Td_Expediente_Transferencia as p on b.Td_idexpedienteacto = p.Td_IdExpedienteActo
  114.                                     inner join SYSMART.dbo.tm_forma_pago as q on p.Tm_Idformapago = q.tm_idformapago
  115.                                    
  116.                                     inner join SYSMART.dbo.ta_tabla as r on p.Ta_MedioPago = r.ta_codigo and r.ta_campo = 'Ta_MedioPago'
  117.                                    
  118.                                     inner join SYSMART.dbo.Tm_moneda as s on p.Tm_IdMoneda = s.tm_idmoneda
  119.                                     inner join SN.Tp_Predios as t on p.Tp_IdBien = t.Tp_IdPredio
  120.                                     inner join SN.Tp_OficinaReg as u on t.Tp_IdOficinaReg = u.Tp_IdOficinaReg
  121.                                 where  a.Tm_FechaGenEsc >= @fechaini and a.Tm_FechaGenEsc <= @fechafin
  122.                                     and i.Ta_DefTipoInterv in ('00', '01')
  123.                                     and f.Tp_IdTipointerv <> 15
  124.                                     and a.Tm_idRegistro = 6
  125.                                     and not a.Tm_idescritura in (4717, 4744, 5696, 6354, 6476, 6482, 6541, 7126, 7152, 7380, 8085, 8147, 8151, 8154, 8163, 8198, 8244, 8304, 8489, 8516)  --- ESCRITURAS EXCLUIDAS
  126.                                                                 and not c.Ta_NatActo is null
  127.                             and b.Tp_idacto not in (35, 36, 37, 38, 39, 40)
  128.                             and case s.tm_codigoROF when 'PEN' then (p.Td_ValorExpTrans / 2.821) when 'USD' then p.Td_ValorExpTrans else (p.Td_ValorExpTrans / 3.521) end > 2500
  129.        
  130.         UNION ALL
  131.         --- CONSTITUCIONES CON PREDIOS
  132.        
  133.             select
  134.            
  135.              a.Tm_NumeroEscritura,
  136.                                 a.Tm_FechaGenEsc,
  137.                 s.tm_codigoROF + ': ' + s.tm_nombreROF as Moneda,
  138.                 d.Td_CapitalSocial as MontoOperacion
  139.             from SN.Tm_Escritura  as a
  140.                     inner join SN.Td_Expediente_Acto as b on a.Tm_IdExpediente = b.Tm_idexpediente
  141.                     inner join SN.Tp_Actos as c on b.Tp_idacto = c.Tp_IdActo
  142.                     inner join SN.Td_Expediente_Interv as f on b.Td_idexpedienteacto = f.Td_IdExpedienteActo
  143.                     inner join SN.vstInterviniente2 as h on f.Tp_IdPersona = h.Tp_idpersona
  144.                     inner join SN.Tp_TipoInterv as i on f.Tp_IdTipointerv = i.Tp_IdTipoInterv
  145.                    
  146.                     inner join SN.Td_Expediente_Sociedades as d on b.Td_idexpedienteacto = d.Td_IdExpedienteActo
  147.                     left join SN.Td_Expediente_SocAportes as e on d.Td_IdExpSociedades = e.Td_IdExpSociedades
  148.                     left join SN.Td_Expediente_DetAportes as g on e.Td_IdExpSocAportes = g.Td_IdExpSocAportes and g.Ta_TipoBien = '01'
  149.                    
  150.                     inner join SN.Tp_Predios as k on g.Tp_IdBien = k.Tp_IdPredio
  151.                     inner join SN.Tp_OficinaReg as u on k.Tp_IdOficinaReg = u.Tp_IdOficinaReg
  152.                    
  153.                     inner join SYSMART.dbo.ta_tabla as l on h.Ta_TipoIdentifica = l.ta_codigo and l.ta_campo = 'Ta_TipoIdentifica'
  154.                     left join SYSMART.dbo.ta_tabla as m on h.Ta_Nacionalidad = m.ta_codigo and m.ta_campo = 'Ta_Nacionalidad'
  155.                     left join SYSMART.dbo.ta_tabla as n on h.Ta_EstCivil = n.ta_codigo and n.ta_campo = 'Ta_EstCivil'
  156.                    
  157.                     inner join SYSMART.dbo.tm_ubigeo as oDist on h.tm_idubigeo = oDist.tm_idubigeo
  158.                     inner join SYSMART.dbo.tm_ubigeo as oProv2 on oDist.tm_refcodigo_ubi = oProv2.tm_idubigeo
  159.                     inner join SYSMART.dbo.tm_ubigeo as oDep1 on oProv2.tm_refcodigo_ubi = oDep1.tm_idubigeo
  160.                    
  161.                     inner join SYSMART.dbo.Tm_moneda as s on d.Tm_IdMoneda = s.tm_idmoneda
  162.                    
  163.                 where  a.Tm_FechaGenEsc >= @fechaini and a.Tm_FechaGenEsc <= @fechafin
  164.                     and i.Ta_DefTipoInterv in ('00', '01')
  165.                     and f.Tp_IdTipointerv <> 15
  166.                     and a.Tm_idRegistro = 6
  167.                     and not a.Tm_idescritura in (4717, 4744, 5696, 6354, 6476, 6482, 6541, 7126, 7152, 7380, 8085, 8147, 8151, 8154, 8163, 8198, 8244, 8304, 8489, 8516)  --- ESCRITURAS EXCLUIDAS
  168.                     and not c.Ta_NatActo is null
  169.                     and b.Tp_idacto in (35, 36, 37, 38, 39, 40)
  170.                    
  171.                    
  172.             UNION ALL
  173.             --- CONSTITUCIONES CON VEHICULOS
  174.            
  175.             select
  176.            
  177.             a.Tm_NumeroEscritura,
  178.                                 a.Tm_FechaGenEsc,
  179.                 s.tm_codigoROF + ': ' + s.tm_nombreROF as Moneda,
  180.                  d.Td_CapitalSocial as MontoOperacion
  181.             from SN.Tm_Escritura  as a
  182.                     inner join SN.Td_Expediente_Acto as b on a.Tm_IdExpediente = b.Tm_idexpediente
  183.                     inner join SN.Tp_Actos as c on b.Tp_idacto = c.Tp_IdActo
  184.                     inner join SN.Td_Expediente_Interv as f on b.Td_idexpedienteacto = f.Td_IdExpedienteActo
  185.                     inner join SN.vstInterviniente2 as h on f.Tp_IdPersona = h.Tp_idpersona
  186.                     inner join SN.Tp_TipoInterv as i on f.Tp_IdTipointerv = i.Tp_IdTipoInterv
  187.                    
  188.                     inner join SN.Td_Expediente_Sociedades as d on b.Td_idexpedienteacto = d.Td_IdExpedienteActo
  189.                     left join SN.Td_Expediente_SocAportes as e on d.Td_IdExpSociedades = e.Td_IdExpSociedades
  190.                     left join SN.Td_Expediente_DetAportes as g on e.Td_IdExpSocAportes = g.Td_IdExpSocAportes  and g.Ta_TipoBien = '00'
  191.                    
  192.                     inner join SN.Tp_Vehiculos as k on g.Tp_IdBien = k.Tp_IdVehiculo
  193.                    
  194.                    
  195.                     inner join SYSMART.dbo.ta_tabla as l on h.Ta_TipoIdentifica = l.ta_codigo and l.ta_campo = 'Ta_TipoIdentifica'
  196.                     left join SYSMART.dbo.ta_tabla as m on h.Ta_Nacionalidad = m.ta_codigo and m.ta_campo = 'Ta_Nacionalidad'
  197.                     left join SYSMART.dbo.ta_tabla as n on h.Ta_EstCivil = n.ta_codigo and n.ta_campo = 'Ta_EstCivil'
  198.                    
  199.                     inner join SYSMART.dbo.tm_ubigeo as oDist on h.tm_idubigeo = oDist.tm_idubigeo
  200.                     inner join SYSMART.dbo.tm_ubigeo as oProv2 on oDist.tm_refcodigo_ubi = oProv2.tm_idubigeo
  201.                     inner join SYSMART.dbo.tm_ubigeo as oDep1 on oProv2.tm_refcodigo_ubi = oDep1.tm_idubigeo
  202.                    
  203.                     inner join SYSMART.dbo.Tm_moneda as s on d.Tm_IdMoneda = s.tm_idmoneda
  204.                    
  205.                 where  a.Tm_FechaGenEsc >= @fechaini and a.Tm_FechaGenEsc <= @fechafin
  206.                     and i.Ta_DefTipoInterv in ('00', '01')
  207.                     and f.Tp_IdTipointerv <> 15
  208.                     and a.Tm_idRegistro = 6
  209.                     and not a.Tm_idescritura in (4717, 4744, 5696, 6354, 6476, 6482, 6541, 7126, 7152, 7380, 8085, 8147, 8151, 8154, 8163, 8198, 8244, 8304, 8489, 8516)  --- ESCRITURAS EXCLUIDAS
  210.                     and not c.Ta_NatActo is null
  211.                     and b.Tp_idacto in (35, 36, 37, 38, 39, 40)
  212.            
  213.             UNION ALL
  214.            
  215.             -- CONSTITUCIONES CON OTROS BIENES
  216.        
  217.             select
  218.            
  219.             a.Tm_NumeroEscritura,
  220.                                 a.Tm_FechaGenEsc,
  221.            
  222.                 s.tm_codigoROF + ': ' + s.tm_nombreROF as Moneda,
  223.                  d.Td_CapitalSocial as MontoOperacion
  224.             from SN.Tm_Escritura  as a
  225.                     inner join SN.Td_Expediente_Acto as b on a.Tm_IdExpediente = b.Tm_idexpediente
  226.                     inner join SN.Tp_Actos as c on b.Tp_idacto = c.Tp_IdActo
  227.                     inner join SN.Td_Expediente_Interv as f on b.Td_idexpedienteacto = f.Td_IdExpedienteActo
  228.                     inner join SN.vstInterviniente2 as h on f.Tp_IdPersona = h.Tp_idpersona
  229.                     inner join SN.Tp_TipoInterv as i on f.Tp_IdTipointerv = i.Tp_IdTipoInterv
  230.                    
  231.                     inner join SN.Td_Expediente_Sociedades as d on b.Td_idexpedienteacto = d.Td_IdExpedienteActo
  232.                     left join SN.Td_Expediente_SocAportes as e on d.Td_IdExpSociedades = e.Td_IdExpSociedades
  233.                     left join SN.Td_Expediente_DetAportes as g on e.Td_IdExpSocAportes = g.Td_IdExpSocAportes and g.Ta_TipoBien = '02'
  234.                    
  235.        
  236.                     inner join SYSMART.dbo.ta_tabla as l on h.Ta_TipoIdentifica = l.ta_codigo and l.ta_campo = 'Ta_TipoIdentifica'
  237.                     left join SYSMART.dbo.ta_tabla as m on h.Ta_Nacionalidad = m.ta_codigo and m.ta_campo = 'Ta_Nacionalidad'
  238.                     left join SYSMART.dbo.ta_tabla as n on h.Ta_EstCivil = n.ta_codigo and n.ta_campo = 'Ta_EstCivil'
  239.                    
  240.                     inner join SYSMART.dbo.tm_ubigeo as oDist on h.tm_idubigeo = oDist.tm_idubigeo
  241.                     inner join SYSMART.dbo.tm_ubigeo as oProv2 on oDist.tm_refcodigo_ubi = oProv2.tm_idubigeo
  242.                     inner join SYSMART.dbo.tm_ubigeo as oDep1 on oProv2.tm_refcodigo_ubi = oDep1.tm_idubigeo
  243.                    
  244.                     inner join SYSMART.dbo.Tm_moneda as s on d.Tm_IdMoneda = s.tm_idmoneda
  245.                    
  246.                 where  a.Tm_FechaGenEsc >= @fechaini and a.Tm_FechaGenEsc <= @fechafin
  247.                     and i.Ta_DefTipoInterv in ('00', '01')
  248.                     and f.Tp_IdTipointerv <> 15
  249.                     and a.Tm_idRegistro = 6
  250.                     and not a.Tm_idescritura in (4717, 4744, 5696, 6354, 6476, 6482, 6541, 7126, 7152, 7380, 8085, 8147, 8151, 8154, 8163, 8198, 8244, 8304, 8489, 8516)  --- ESCRITURAS EXCLUIDAS
  251.                     and not c.Ta_NatActo is null
  252.                     and b.Tp_idacto in (35, 36, 37, 38, 39, 40)
  253.                    
  254.                    
  255.     ) as x
  256.     union all
  257.             select
  258.             --ROW_NUMBER() OVER(ORDER BY a.Tm_NumeroEscritura ASC) AS Item,
  259.         '5' as tipoesc,
  260.              convert(int, SUBSTRING(a.Tm_NumeroEscritura, 6, 5)) nroescritura,
  261.                                 SN.fc_formatearFecha('03', a.tm_fechafirmaesc) as fechaNum,
  262.                 s.tm_codigoROF + ': ' + s.tm_nombreROF as Moneda,
  263.                 p.Td_MontoGravamen as MontoOperacion
  264.        
  265.                                 from SN.Tm_Escritura  as a
  266.                                     inner join SN.Td_Expediente_Acto as b on a.Tm_IdExpediente = b.Tm_idexpediente
  267.                                     inner join SN.Tp_Actos as c on b.Tp_idacto = c.Tp_IdActo
  268.                                     inner join SN.Td_Expediente_Interv as f on b.Td_idexpedienteacto = f.Td_IdExpedienteActo
  269.                                     inner join SN.vstInterviniente2 as h on f.Tp_IdPersona = h.Tp_idpersona
  270.                                     inner join SN.Tp_TipoInterv as i on f.Tp_IdTipointerv = i.Tp_IdTipoInterv
  271.                                     inner join SYSMART.dbo.ta_tabla as l on h.Ta_TipoIdentifica = l.ta_codigo and l.ta_campo = 'Ta_TipoIdentifica'
  272.                                     left join SYSMART.dbo.ta_tabla as m on h.Ta_Nacionalidad = m.ta_codigo and m.ta_campo = 'Ta_Nacionalidad'
  273.                                     left join SYSMART.dbo.ta_tabla as n on h.Ta_EstCivil = n.ta_codigo and n.ta_campo = 'Ta_EstCivil'
  274.                                    
  275.                                     inner join SYSMART.dbo.tm_ubigeo as oDist on h.tm_idubigeo = oDist.tm_idubigeo
  276.                                     inner join SYSMART.dbo.tm_ubigeo as oProv2 on oDist.tm_refcodigo_ubi = oProv2.tm_idubigeo
  277.                                     inner join SYSMART.dbo.tm_ubigeo as oDep1 on oProv2.tm_refcodigo_ubi = oDep1.tm_idubigeo
  278.                                    
  279.                                     inner join SN.Td_Expediente_Gravamen as p on b.Td_idexpedienteacto = p.Td_IdExpedienteActo
  280.                                    
  281.                                     inner join SYSMART.dbo.Tm_moneda as s on p.Tm_IdMoneda = s.tm_idmoneda
  282.                                     inner join SN.Tp_Vehiculos as t on p.Tp_idbien = t.Tp_IdVehiculo
  283.                                 where  a.Tm_FechaGenEsc >= @fechaini and a.Tm_FechaGenEsc <= @fechafin
  284.                                     and i.Ta_DefTipoInterv in ('00', '01')
  285.                                     and f.Tp_IdTipointerv <> 15
  286.                                     and a.Tm_idRegistro = 11
  287. ) as DISTINTO
  288. ) AS PETERETE
  289. end
Add Comment
Please, Sign In to add comment