ahmedrahil786

Amount spent by sofam - TIM

Jul 22nd, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. (select distinct r.member_id ,m.display_name as name,
  2. count(r.id) as reservations,
  3. sum(ra.mileage) as Distance,
  4. round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as Duration,
  5. round((timestampdiff(day, min(r.start_at), max(r.start_at)))) as Days_Active_on_socar,
  6. round(sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('rent','oneway','d2d','mileage')),0)),2) as Total_Charges,
  7. round(sum(ifnull((select sum(amount) from payments where state='normal' and reservation_id=r.id and paid_type ='coupon'),0)),2) as coupons_spent,
  8. round(sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('rent','oneway','d2d','mileage')),0)
  9. - ifnull((select sum(amount) from payments where state='normal' and reservation_id=r.id and paid_type ='coupon'),0)),2) as Net_paid_to_Socar,
  10. Round(round(sum(ifnull((select sum(amount) from payments where state='normal' and reservation_id=r.id and paid_type ='coupon'),0)),2)/round(sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('rent','oneway','d2d','mileage')),0)),2),2) as coupon_spent_PCT,
  11. round(round(sum(ifnull((select sum(amount) from charges where state='normal' and r.id=reservation_id and kind in ('rent','oneway','d2d','mileage')),0)
  12. - ifnull((select sum(amount) from payments where state='normal' and reservation_id=r.id and paid_type ='coupon'),0)),2)/count(r.id) ,2) as Average_paid_spent_per_resv,
  13. round((sum(ra.mileage)/round((timestampdiff(day, min(r.start_at), max(r.start_at))))),2) as Ratio_Dis_to_Daysactive
  14. from reservations r, zones z, members m, reservation_appendixes ra
  15. Where
  16. r.start_zone_id = z.id
  17. and r.member_id = m.id
  18. and r.id = ra.reservation_id
  19. and (m.imaginary in ('sofam') or m.id IN (84450,75140))
  20. and r.state in ('completed')
  21. group by r.member_id
  22. order by duration desc)
Advertisement
Add Comment
Please, Sign In to add comment