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
| with cte_recur (dticket, day_rent, stop_rent, uninvoiced_net) as
(
select dticket
, start_rent
, stop_rent
, uninvoiced_net
from MaTable
union all
select dticket
, dateadd(day, 1, day_rent)
, stop_rent
, uninvoiced_net
from cte_recur
where day_rent < stop_rent
)
select dticket
, max(day_rent) as jour_facture
, sum(case when datename(weekday, day_rent) in ('Saturday', 'Sunday') then 0 else 1 end) as ouvre
, sum(case when datename(weekday, day_rent) in ('Saturday', 'Sunday') then 0.00 else 1.00 end)
/ sum(sum(case when datename(weekday, day_rent) in ('Saturday', 'Sunday') then 0.00 else 1.00 end)) over(partition by dticket)
* uninvoiced_net as montant_facture
from cte_recur
group by dticket
, datepart(month, day_rent)
, uninvoiced_net
order by 1, 2
| dticket | jour_facture | ouvre | montant_facture |
|---------|--------------|-------|-----------------|
| 1 | 2020-09-30 | 4 | 111.111 |
| 1 | 2020-10-31 | 22 | 611.111 |
| 1 | 2020-11-15 | 10 | 277.777 |
| 2 | 2020-09-27 | 1 | 50 | |
Partager