1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| create table Facturation
( IdFacturation integer
, DtFacturation datetime
, montant decimal(10,2)
, cesure1 smallint
, cesure2 smallint
, cesure3 smallint
, cesure4 smallint
)
go
insert into Facturation values
(1, '2018-04-10 09:41:19', 1500, 50, 25, 25, 0),
(2, '2018-04-10 09:41:19', 2000, 50, 50, 0, 0),
(3, '2017-04-10 09:41:19', 650, 0, 25, 75, 0),
(4, '2017-04-10 09:41:19', 15000, 20, 25, 20, 35),
(5, '2017-04-10 09:41:19', 4850, 100, 0, 0, 0)
go
select year(DtFacturation)
+ case cesure
when 'cesure1' then 0
when 'cesure2' then 1
when 'cesure3' then 2
when 'cesure4' then 3
end as annee
, sum(montant * pct) / 100.00 as montant
, count(case when pct > 0 then 1 end) as nbr
from Facturation
unpivot (pct for cesure in (cesure1, cesure2, cesure3, cesure4)) as t
group by year(DtFacturation)
+ case cesure
when 'cesure1' then 0
when 'cesure2' then 1
when 'cesure3' then 2
when 'cesure4' then 3
end
order by annee asc
go
annee montant nbr
----- ------- ---
2017 7850 2
2018 5662 4
2019 4862 4
2020 5625 2
2021 0 0 |
Partager