ahmedrahil786

SU to MFT - Rahil

Jun 9th, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. set @startDate = "2019-3-01 00:00:00";
  2. #set @monthInterval = 12;
  3.  
  4. select L.*, B.signups from
  5.  
  6. (select
  7. LEFT(YEARWEEK(created_at,3),4) AS Year,
  8. weekofyear(created_at) as Week,
  9. count(distinct mid) as TotMFTs,
  10. count(distinct case when SUtoMFT <= 1 then mid end) AS "<=1",
  11. count(distinct case when SUtoMFT > 1 and SUtoMFT <= 14 then mid end) AS "1-14",
  12. count(distinct case when SUtoMFT > 14 and SUtoMFT <= 20 then mid end) AS "15-20",
  13. count(distinct case when SUtoMFT > 20 and SUtoMFT <= 30 then mid end) AS "21-30",
  14. count(distinct case when SUtoMFT > 30 and SUtoMFT <= 60 then mid end) AS "31-60",
  15. count(distinct case when SUtoMFT > 60 and SUtoMFT <= 120 then mid end) AS "61-120",
  16. count(distinct case when SUtoMFT > 120 and SUtoMFT <= 180 then mid end) AS "121-180",
  17. count(distinct case when SUtoMFT > 180 and SUtoMFT <= 365 then mid end) AS "181-365",
  18. count(distinct case when SUtoMFT > 365 and SUtoMFT <= 730 then mid end) AS "366-730",
  19. count(distinct case when SUtoMFT > 730 then mid end) AS ">730"
  20. from(
  21. select
  22. r.id as rid,
  23. convert_tz(r.created_at, '+00:00', '+8:00') as created_at,
  24. #convert_tz(r.start_at, '+00:00', '+8:00') as start_date,
  25. #convert_tz(r.return_at, '+00:00', '+8:00') as return_at,
  26. #timestampdiff(minute, r.start_at, r.return_at)/60 as dur,
  27. ifnull((select min(id) from reservations where r.member_id=member_id group by member_id),0) as firstRes,
  28. m.id as mid,
  29. convert_tz(m.created_at, '+00:00', '+8:00') as signUpDate,
  30. timestampdiff(hour, m.created_at, r.created_at)/24 as SUtoMFT
  31. #str_to_date(m.birthday, '%Y%m%d') as birthdate,
  32. #(year(convert_tz(r.created_at, '+00:00', '+8:00'))-year(m.birthday)) as age,
  33. #r.way as way
  34. from cars c, reservations r, members m, zones z
  35. where c.id = r.car_id
  36. and m.id = r.member_id
  37. and z.id = r.start_zone_id
  38. and z.id not in (2,3,101)
  39. #and z.city in ('JB')
  40. and m.imaginary in ('normal')
  41. and convert_tz(r.created_at, '+00:00', '+8:00') >= @startDate
  42. #and convert_tz(r.created_at, '+00:00', '+8:00') < date_add(@startDate, interval @monthInterval month)
  43. having rid=firstRes) A
  44. group by Year, Week
  45. order by Year desc, Week desc) L
  46. left join
  47. (select
  48. weekofyear(m.created_at + interval '8' hour) as week,
  49. year(m.created_at + interval '8' hour) as year,
  50. count(distinct m.id) as signups
  51. from members m
  52. left outer join reservations r on
  53. r.member_id = m.id
  54. where m.created_at + interval '8' hour >= @startDate
  55. and m.imaginary = 'normal'
  56. group by 1,2) B
  57. on L.week = B.week and L.year = B.year
  58.  
  59. group by Year, Week
  60. order by Year desc, Week desc
Advertisement
Add Comment
Please, Sign In to add comment