ahmedrahil786

Coupons usage per Day - Rahil

Jan 10th, 2020
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. set @startdate1 = date_sub(CURDATE(), interval 60 day);
  2. set @startdate2 = date_add(CURDATE(), interval 10 day);
  3. use socar_malaysia;
  4. select I.return_Date as date, I.coupon_name as Coupon_name, count(I.rid) as resv, round(Sum(I.gross_rev)/count(I.rid),2) as Gros_BS,round(Sum(I.net_rev)/count(I.rid),2) as Net_BS, Sum(I.mft) as MFTs from
  5. (select A.return_date as return_date, A.rid2 as rid, A.coupon_name as coupon_name, A.dur as dur, A.ad_sdur as ad_sdur, IFNULL(E.charges,0) as gross_rev, if(rid2=firstRes,1,0) as mft,
  6. IFNULL(F.coupon_Spent,0) as Coupon_Spent,
  7. round((IFNULL(E.charges,0) - IFNULL(F.coupon_Spent,0)),2) as net_rev from
  8. (select
  9. distinct r.id as rid2, r.member_id as mid, c.comment as coupon_name,
  10. round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur,
  11. round(sum(floor(timestampdiff(minute, r.start_at, r.end_at)/60/24)*12+if(mod(timestampdiff(hour, r.start_at, r.end_at),24)>=12,12,mod(timestampdiff(minute, r.start_at, r.end_at)/60,24))),2) as ad_sdur,
  12. ifnull((select min(id) from reservations where state='completed' and r.member_id=member_id group by member_id),0) as firstRes,
  13. Date(r.return_at + interval '8' hour) as return_Date
  14. from reservations r left join members m on r.member_id = m.id
  15. left join reservation_appendixes ra on ra.reservation_id = r.id
  16. left join coupons c on c.reservation_id = r.id
  17. where r.state in ('completed')
  18. and r.return_at + interval 8 hour >= @startdate1
  19. and r.return_at + interval 8 hour <= @startdate2
  20. and m.imaginary in ('sofam', 'normal')
  21. and r.member_id not in ('125', '127')
  22. group by rid2) A
  23. left join
  24. (select c.reservation_id as rid, sum(c.amount) as charges from charges c
  25. where c.state='normal' and c.kind in ('rent','oneway','d2d','mileage','insurance')
  26. group by rid) E on A.rid2 = E.rid
  27. left join
  28. (select p.reservation_id as rid, month(p.created_at + interval '8' hour) as month,Year(p.created_at + interval '8' hour) as year, IFNULL(p.amount,0) as coupon_Spent from payments p
  29. where p.state = 'normal' and p.paid_type = 'coupon'
  30. group by p.reservation_id) F on A.rid2 = F.rid ) I
  31. group by 1,2
  32. order by 1 desc
Advertisement
Add Comment
Please, Sign In to add comment