I have this query and it does not allow me to use an aggregation function within another aggregation function and the fact is that I want the fields not to be repeated and I have already tried with distinct and nothing.
this is the error:
Can not perform an aggregate function on an expression containing an aggregate or a subquery.
select distinct CodigoDeAlquiler,
case datepart(weekday,FechaDelAlquiler)
when 1 then 'Domingo' when 2 then 'Lunes'
when 3 then 'Martes' when 4 then 'Miercoles'
when 5 then 'Jueves' when 6 then 'Viernes'
when 7 then 'Sabado' end as Dia,
FechaRealDevolucion,
l.Titulo,
cl.NumeroDeCopia,
c.NombresDelCliente + ' ' + c.ApellidosDelCliente as Cliente,
sum(count(alq.CodigoDeAlquiler)) as Total
from AlquileresDelLibro alq
inner join Clientes c
on alq.ClienteId = c.Id
inner join CopiasDelLibro cl
on alq.CopiaDelLibroId = cl.Id
inner join Libros l
on cl.LibroId = l.Id
where datepart(week,FechaDelAlquiler) = datepart(week,getdate())
group by CodigoDeAlquiler, datepart(weekday,FechaDelAlquiler), FechaRealDevolucion, l.Titulo, cl.NumeroDeCopia, c.NombresDelCliente, c.ApellidosDelCliente
having count(*) > 0
order by Dia asc