Código SQL:
Ver originalSELECT SUM(t1.valor factura rango], t1.entidad
FROM
(
SELECT C.strNombre_CLIE [ENTIDAD],SUM(D.dcmBruto_DETA) [VALOR FACTURA RANGO],(@TotalFactura)[TOTAL CARTERA],
'RANGO DE FECHAS' = CASE
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'
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 '
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'
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'
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'
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
FROM tblFACTura F INNER JOIN tblCLIEnte C ON F.intCodigo_CLIE=C.intCodigo_CLIE INNER JOIN tblDetalle D ON
F.intCodigo_FACT=D.intCodigo_FACT
WHERE F.strNitEmpresa=@Nit_EMPR
GROUP BY C.strNombre_CLIE,dtmFechaVenc_FACT
DECLARE TOTALCARTERA CURSOR FOR
SELECT C.strNombre_CLIE [ENTIDAD],SUM(D.dcmBruto_DETA) [VALOR FACTURA],(@TotalFactura)[TOTAL CARTERA]
FROM tblFACTura F INNER JOIN tblCLIEnte C ON F.intCodigo_CLIE=C.intCodigo_CLIE INNER JOIN tblDetalle D ON
F.intCodigo_FACT=D.intCodigo_FACT
WHERE F.strNitEmpresa=@Nit_EMPR
GROUP BY C.strNombre_CLIE,dtmFechaVenc_FACT
) AS t1 GROUP BY t1.nombre
Seria algo asi :)
Saludos!