Advertisement
ahmedrahil786

TTT -Mr.Hoon - Outline Dashboard Requirement

Dec 8th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. select
  2. A.month,
  3. ca.CarsApproved as cars,
  4. B.CompletedBookings as Bookings,
  5. A.Signups,
  6. C.MFTs as first_rent,
  7. B.Active as active,
  8. B.comp_gross as GMV,
  9. AV.ten,
  10. AV.eleven,
  11. H.Active_hosts as active_Cars
  12. from
  13. (select
  14. extract('month' from u.created_at) as month,
  15. count(distinct u.id) as Signups,
  16. count(distinct l.user_id) as Hosts,
  17. count(distinct l.id) as Cars,
  18. count(distinct case when l.status = 'approved' then l.id end) as CarsApproved,
  19. count(distinct ld.listing_id) as CarswithDocUploaded
  20. from users u
  21. left outer join listings l
  22. on l.user_id = u.id
  23. left outer join listing_documents ld
  24. on ld.listing_id = l.id
  25. group by 1) A
  26. join (
  27. select
  28. extract('month' from b.created_at) as month,
  29. count(distinct case when b.status = 'completed' then b.user_id end) as Active,
  30. count(distinct case when b.status != 'cancelled' then b.id end) as NonCancelledBookings,
  31. count(distinct case when b.status = 'accepted' then b.id end) as AcceptedBookings,
  32. count(distinct case when b.status = 'completed' then b.id end) as CompletedBookings,
  33. sum(case when b.status not IN('cancelled', 'payment_failed', 'withdrawn', 'rejected', 'accepted') then b.gross_amount end) as comp_gross
  34. from bookings b
  35. group by 1) B
  36. on B.month = A.month
  37. left join
  38. (
  39. select A.month as month, count(distinct case when B.first_res = A.bookingid then A.userid end) as MFTs from
  40. ((select distinct b.id as bookingid, extract('month' from b.created_at) as month, b.user_id userid
  41. from bookings b left join users u on u.id = b.user_id
  42. where b.status = 'completed'
  43. group by b.id
  44. order by b.id desc) A
  45. left join
  46. (select b.user_id as userid , min(b.id) as first_res
  47. from bookings b where b.status ='completed'
  48. group by userid
  49. order by 2 asc) B
  50. on B.userid = A.userid)
  51. group by A.month) C
  52. on C.month = A.month
  53. left join
  54. (select
  55. extract('month' from uk.created_at) as month,
  56. count(distinct case when uk.status = 'approved' then uk.id end) as doc_approved
  57. from user_kyc uk
  58. group by 1) D
  59. on D.month = A.month
  60. left join
  61. (select
  62. extract('month' from l.created_at) as month,
  63. count(distinct case when l.status = 'approved' then l.id end) as CarsApproved
  64. from listings l
  65. group by month) ca
  66. on ca.month = A.month
  67. left join
  68. (select A.month as month,
  69. count(distinct case when A.month = '10' then A.lid end) as ten,
  70. count(distinct case when A.month = '11' then A.lid end) as eleven,
  71. count(distinct case when A.month = '12' then A.lid end) as twelve
  72. from
  73. (SELECT distinct la.listing_id as lid, extract('month' from la.start_date) as month
  74. FROM listing_availability la
  75. order by 1 asc) A
  76. group by 1) AV
  77. on AV.month = A.month
  78. left join
  79. (select
  80. extract('month' from b.created_at) as Amonth,
  81. count(distinct case when b.status in ('accepted','completed') then b.listing_id end) as Active_hosts
  82. from bookings b
  83. group by 1) H
  84. on H.Amonth = A.month
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement