1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
SELECT compte_client,
SUM(CASE WHEN strftime('%Y', date_facturation) = p1 THEN montant_facture ELSE 0 END) AS p1,
SUM(CASE WHEN strftime('%Y', date_facturation) = p2 THEN montant_facture ELSE 0 END) AS p2,
SUM(CASE WHEN strftime('%Y', date_facturation) = p3 THEN montant_facture ELSE 0 END) AS p3,
SUM(CASE WHEN strftime('%Y', date_facturation) = p4 THEN montant_facture ELSE 0 END) AS p4,
SUM(CASE WHEN strftime('%Y', date_facturation) <= p5 THEN montant_facture ELSE 0 END) AS p5,
round(sum(montant_facture),2) as montant_total
FROM tmp_creances_clients,
(SELECT strftime('%Y',current_date) -1 AS p1,
strftime('%Y',current_date) -2 AS p2,
strftime('%Y',current_date) -3 AS p3,
strftime('%Y',current_date) -4 AS p4,
strftime('%Y',current_date) -5 AS p5) AS an
GROUP BY 1
ORDER BY 1 |
Partager