Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- with Daily(subscription_date, refund) as
- (select d, avg(payment_money) from
- (select S.Id, cast(S.datetime as date) as d, P.payment_money from Subscribtion S
- outer apply
- (select sum(P.money) as payment_money from Payment P
- where P.UserId = S.UserId and P.datetime between S.datetime and dateadd(day, 1, S.datetime) ) P
- ) PerSubsription
- group by d)
- select D1.subscription_date, avg(D2.refund) refund_past_week from Daily D1
- left join Daily D2 on DateDiff(day, D2.subscription_date, D1.subscription_date) between 0 and 6
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement