Ver Mensaje Individual
  #2 (permalink)  
Antiguo 20/04/2011, 10:09
Avatar de Libras
Libras
Colaborador
 
Fecha de Ingreso: agosto-2006
Ubicación: En la hermosa perla de occidente
Mensajes: 7.412
Antigüedad: 18 años, 3 meses
Puntos: 774
Respuesta: cursor para obtener cartera de credito

Código SQL:
Ver original
  1. SELECT SUM(t1.valor factura rango], t1.entidad
  2. FROM
  3.  
  4. (
  5. SELECT C.strNombre_CLIE [ENTIDAD],SUM(D.dcmBruto_DETA) [VALOR FACTURA RANGO],(@TotalFactura)[TOTAL CARTERA],
  6. 'RANGO DE FECHAS' = CASE
  7. WHEN (DATEDIFF(DAY,CONVERT(VARCHAR,dtmFechaVenc_FACT,10 3),CAST(GETDATE() AS DATETIME))>0 AND DATEDIFF(DAY,CONVERT(VARCHAR,dtmFechaVenc_FACT,103 ),CAST(GETDATE() AS DATETIME))<=30) THEN 'FACTURAS VENCIDAS ENTRE 0 Y 30 DIAS'
  8. WHEN DATEDIFF(DAY,CONVERT(VARCHAR,dtmFechaVenc_FACT,103 ),CAST(GETDATE() AS DATETIME))>31 AND DATEDIFF(DAY,CONVERT(VARCHAR,dtmFechaVenc_FACT,103 ),CAST(GETDATE() AS DATETIME))<=60 THEN 'FACTURAS VENCIDAS ENTRE 31 Y 60 DIAS '
  9. WHEN DATEDIFF(DAY,CONVERT(VARCHAR,dtmFechaVenc_FACT,103 ),CAST(GETDATE() AS DATETIME))>61 AND DATEDIFF(DAY,CONVERT(VARCHAR,dtmFechaVenc_FACT,103 ),CAST(GETDATE() AS DATETIME))<=90 THEN 'FACTURAS VENCIDAS ENTRE 61 Y 90 DIAS'
  10. WHEN DATEDIFF(DAY,CONVERT(VARCHAR,dtmFechaVenc_FACT,103 ),CAST(GETDATE()AS DATETIME))>91 AND DATEDIFF(DAY,CONVERT(VARCHAR,dtmFechaVenc_FACT,103 ),CAST(GETDATE() AS DATETIME))<=180 THEN 'FACTURAS VENCIDAS ENTRE 91 Y 180 DIAS'
  11. WHEN DATEDIFF(DAY,CONVERT(VARCHAR,dtmFechaVenc_FACT,103 ),CAST(GETDATE() AS DATETIME))>181 AND DATEDIFF(DAY,CONVERT(VARCHAR,dtmFechaVenc_FACT,103 ),CAST(GETDATE() AS DATETIME))<=360 THEN 'FACTURAS VENCIDAS ENTRE 181 Y 360 DIAS'
  12. WHEN DATEDIFF(DAY,CONVERT(VARCHAR,dtmFechaVenc_FACT,103 ),CAST(GETDATE() AS DATETIME))>361 THEN 'FACTURAS VENCIDAS MAYORES DE 360 DIAS' ELSE 'FACTURAS PROXIMAS A VENCER' END
  13.  
  14. FROM tblFACTura F INNER JOIN tblCLIEnte C ON F.intCodigo_CLIE=C.intCodigo_CLIE INNER JOIN tblDetalle D ON
  15. F.intCodigo_FACT=D.intCodigo_FACT
  16. WHERE F.strNitEmpresa=@Nit_EMPR
  17. GROUP BY C.strNombre_CLIE,dtmFechaVenc_FACT
  18. DECLARE TOTALCARTERA CURSOR FOR
  19. SELECT C.strNombre_CLIE [ENTIDAD],SUM(D.dcmBruto_DETA) [VALOR FACTURA],(@TotalFactura)[TOTAL CARTERA]
  20. FROM tblFACTura F INNER JOIN tblCLIEnte C ON F.intCodigo_CLIE=C.intCodigo_CLIE INNER JOIN tblDetalle D ON
  21. F.intCodigo_FACT=D.intCodigo_FACT
  22. WHERE F.strNitEmpresa=@Nit_EMPR
  23. GROUP BY C.strNombre_CLIE,dtmFechaVenc_FACT
  24.  
  25. ) AS t1 GROUP BY t1.nombre


Seria algo asi :)

Saludos!
__________________
What does an execution plan say to t-sql query? Go f**k yourself, if you are not happy with me