I have to obtain a list with the clients of the company to which we have invoiced something during the current year, indicating how much we have invoiced each month.
I have two tables: the customer table (customer) and the invoice table (invoice). The relationship is obvious: a customer can have many bills, and each invoice is related to a single customer.
The only thing that has occurred to me is the following:
select id,
name as "Nombre",
identity_number as "CIF",
(select sum(total)
from invoice
where payment_date between '2017-01-01' and '2017-02-01' and customer_id=c.id group by customer_id) as "Enero",
(select sum(total)
from invoice
where payment_date between '2017-02-01' and '2017-03-01' and customer_id=c.id group by customer_id) as "Febrero",
(select sum(total)
from invoice
where payment_date between '2017-03-01' and '2017-04-01' and customer_id=c.id group by customer_id) as "Marzo",
(select sum(total)
from invoice
where payment_date between '2017-04-01' and '2017-05-01' and customer_id=c.id group by customer_id) as "Abril",
(select sum(total)
from invoice
where payment_date between '2017-05-01' and '2017-06-01' and customer_id=c.id group by customer_id) as "Mayo",
(select sum(total)
from invoice
where payment_date between '2017-06-01' and '2017-07-01' and customer_id=c.id group by customer_id) as "Junio",
(select sum(total)
from invoice
where payment_date between '2017-07-01' and '2017-08-01' and customer_id=c.id group by customer_id) as "Julio",
(select sum(total)
from invoice
where payment_date between '2017-08-01' and '2017-09-01' and customer_id=c.id group by customer_id) as "Agosto",
(select sum(total)
from invoice
where payment_date between '2017-09-01' and '2017-10-01' and customer_id=c.id group by customer_id) as "Septiembre",
(select sum(total)
from invoice
where payment_date between '2017-10-01' and '2017-11-01' and customer_id=c.id group by customer_id) as "Octubre",
(select sum(total)
from invoice
where payment_date between '2017-11-01' and '2017-12-01' and customer_id=c.id group by customer_id) as "Noviembre",
(select sum(total)
from invoice
where payment_date between '2017-12-01' and '2017-12-31' and customer_id=c.id group by customer_id) as "Diciembre"
from customer c
where c.branding_id=1 and (select sum(total)
from invoice
where payment_date between '2017-01-01' and '2017-12-31' and customer_id=c.id group by customer_id) > 0
This works, but I find it extremely inefficient (there are 13 subqueries, if we include the WHERE one). Is there a way to simplify this query or is it the best way to get the data?