Advertisement
hoewarden

Untitled

Sep 14th, 2019
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.53 KB | None | 0 0
  1. with Daily(subscription_date, refund) as
  2. (select d, avg(payment_money) from
  3. (select S.Id, cast(S.datetime as date) as d, P.payment_money from Subscribtion S
  4. outer apply
  5. (select sum(P.money) as payment_money from Payment P
  6. where P.UserId = S.UserId and P.datetime between S.datetime and dateadd(day, 1, S.datetime) ) P
  7. ) PerSubsription
  8. group by d)
  9. select D1.subscription_date, avg(D2.refund) refund_past_week from Daily D1
  10. 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