Ver Mensaje Individual
  #5  
Antiguo 18-06-2012
abelg abelg is offline
Miembro
 
Registrado: jul 2004
Posts: 50
Reputación: 20
abelg Va por buen camino
Hola espero esto te ayude, la tabla temporal es solo para simular los datos

Código SQL [-]
create table #encfactura (
  CodigoCliente int,
  fechafactura datetime,
  totalfactura money
)

insert into #encfactura Values(392, '2012-05-22', 47500.00)
insert into #encfactura Values(392, '2012-05-13', 1425.00)
insert into #encfactura Values(392, '2012-02-14', 79520.00)

Declare @fecha datetime = '2012-06-13'

select CodigoCliente, sum(totalfactura) TotalFactura,
  SUM(case when datediff(d,fechafactura,@fecha) BETWEEN  0 and 30 then TotalFactura end) [1-30],
  SUM(case when datediff(d,fechafactura,@fecha) BETWEEN 31 and 60 then TotalFactura end) [31-60],
  SUM(case when datediff(d,fechafactura,@fecha) BETWEEN 61 and 90 then TotalFactura end) [61-90],
  SUM(case when datediff(d,fechafactura,@fecha) BETWEEN 91 and 9999 then TotalFactura end) [91 o Mas]
from #encfactura
where year(fechafactura)= 2012
group by CodigoCliente
order by CodigoCliente

drop table #encfactura
salu2
Responder Con Cita