ahmedrahil786

City level dur > 24 hours - Rahil

Jun 2nd, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. set @startdate1 = date_sub(CURDATE(), interval 15 month);
  2. set @startdate2 = date_add(CURDATE(), interval 10 day);
  3. use socar_malaysia;
  4. select month(A.return_date) as month, year(A.return_date) as year,
  5. count(distinct case when A.city like 'KL' then A.rid end) as KL_res,
  6. count(distinct case when A.city like 'SL' then A.rid end) as SL_res,
  7. count(distinct case when A.city like 'PG' then A.rid end) as PG_res,
  8. count(distinct case when A.city like 'JB' then A.rid end) as JB_res,
  9. count(distinct case when A.dur >= 24 and A.city like 'KL' then A.rid end) as KL_24_plus,
  10. count(distinct case when A.dur >= 24 and A.city like 'SL' then A.rid end) as SL_24_plus,
  11. count(distinct case when A.dur >= 24 and A.city like 'PG' then A.rid end) as PG_24_plus,
  12. count(distinct case when A.dur >= 24 and A.city like 'JB' then A.rid end) as JB_24_plus,
  13. ROUND((count(distinct case when A.dur >= 24 and A.city like 'KL' then A.rid end)/count(distinct case when A.city like 'KL' then A.rid end))*100,2) as 24_plus_pct_out_of_total_KL,
  14. ROUND((count(distinct case when A.dur >= 24 and A.city like 'SL' then A.rid end)/count(distinct case when A.city like 'SL' then A.rid end))*100,2) as 24_plus_pct_out_of_total_SL,
  15. ROUND((count(distinct case when A.dur >= 24 and A.city like 'PG' then A.rid end)/count(distinct case when A.city like 'PG' then A.rid end))*100,2) as 24_plus_pct_out_of_total_PG,
  16. ROUND((count(distinct case when A.dur >= 24 and A.city like 'JB' then A.rid end)/count(distinct case when A.city like 'JB' then A.rid end))*100,2) as 24_plus_pct_out_of_total_JB
  17. from
  18. (select
  19. distinct r.id as rid, r.member_id as mid, z.city as city,
  20. round(sum(timestampdiff(minute, r.start_at, r.end_at)/60),2) as dur,
  21. 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,
  22. Date(r.return_at + interval '8' hour) as return_Date
  23. from reservations r left join members m on r.member_id = m.id
  24. left join zones z on z.id = r.start_zone_id
  25. where r.state in ('completed')
  26. and r.return_at + interval 8 hour >= @startdate1
  27. and r.return_at + interval 8 hour <= @startdate2
  28. and m.imaginary in ('sofam', 'normal')
  29. and r.member_id not in ('125', '127')
  30. group by rid) A
  31. group by 1,2
  32. order by 2 asc , 1 asc
Advertisement
Add Comment
Please, Sign In to add comment