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
|
with tab as
(
SELECT '01' AS mois, 2 AS montant FROM dual union ALL
SELECT '01', 1 FROM dual union ALL
SELECT '02', 6 FROM dual union ALL
SELECT '03', 4 FROM dual union ALL
SELECT '03', 2 FROM dual union ALL
SELECT '04', 2 FROM dual union ALL
SELECT '05', 5 FROM dual
),
pivot as
(
select level as x from dual connect by level <= 3
),
data as
(
select mois, montant, case when mois = '01' and x = 1 then 1
when (mois = '02' and x = 1) or (mois = '01' and x = 2) then 2
when (mois = '03' and x = 1) or (mois = '02' and x = 2) or (mois = '01' and x = 3) then 3
when (mois = '04' and x = 1) or (mois = '03' and x = 2) or (mois = '02' and x = 3) then 4
when (mois = '05' and x = 1) or (mois = '04' and x = 2) or (mois = '03' and x = 3) then 5
end grp
from tab, pivot
)
select max(mois), sum(montant)
from data
where grp is not null
group by grp |
Partager