Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. WITH RECURSIVE date_ranges(range_from, range_to) AS (
  2. VALUES (
  3. '2018-11-19T05:00:00+09:00'::TIMESTAMP WITH TIME ZONE,
  4. '2018-12-19T04:59:00+09:00'::TIMESTAMP WITH TIME ZONE
  5. )
  6. UNION
  7. SELECT
  8. range_from + interval '1 day',
  9. range_to + interval '1 day'
  10. FROM date_ranges
  11. WHERE range_from < '2018-12-20T04:59:00+09:00'
  12. )
  13. SELECT
  14. TO_CHAR(date_ranges.range_from at time zone 'Asia/Tokyo', 'YYYY/MM/DD') AS date_resv,
  15. COALESCE(SUM(number_resv_by_call), 0) AS number_resv_by_call,
  16. COALESCE(SUM(number_resv_on_web), 0) AS number_resv_on_web,
  17. COALESCE(SUM(number_resv_using_cash), 0) AS number_resv_using_cash,
  18. COALESCE(SUM(number_resv_using_point), 0) AS number_resv_using_point,
  19. COALESCE(SUM(revenue_resv_by_phone), 0) AS revenue_resv_by_phone,
  20. COALESCE(SUM(revenue_resv_on_web + delay_option_total_cash + delay_option_total_points), 0) AS revenue_resv_on_web,
  21. COALESCE(SUM(revenue_resv_using_cash + delay_option_total_cash), 0) AS revenue_resv_using_cash,
  22. COALESCE(SUM(revenue_resv_using_point + delay_option_total_points), 0) AS revenue_resv_using_point,
  23. COALESCE(SUM(total_revenue + delay_option_total_cash + delay_option_total_points), 0) AS total_revenue,
  24. COALESCE(SUM(total_web_cash_service_fee), 0) AS total_web_cash_service_fee,
  25. COALESCE(SUM(revenue_resv_cancelled), 0) AS revenue_resv_cancelled,
  26. COALESCE(SUM(total_cancel_fee), 0) AS total_cancel_fee,
  27. deleted_at
  28. FROM date_ranges
  29. LEFT JOIN (
  30. SELECT
  31. store_id,
  32. deleted_at,
  33. confirm_start_at AS time_at,
  34. (CASE WHEN created_by='store' THEN 1 ELSE 0 END) AS number_resv_by_call,
  35. (CASE WHEN created_by='store' THEN 0 ELSE 1 END) AS number_resv_on_web,
  36. (CASE WHEN payment_method='cash' THEN 1 ELSE 0 END) AS number_resv_using_cash,
  37. (CASE WHEN payment_method='point' THEN 1 ELSE 0 END) AS number_resv_using_point,
  38. (CASE WHEN created_by='store' THEN (total_cash + total_points - delay_option_total_cash - delay_option_total_points) ELSE 0 END) AS revenue_resv_by_phone,
  39. (CASE WHEN created_by='store' THEN 0 ELSE (total_cash + total_points - delay_option_total_cash - delay_option_total_points) END) AS revenue_resv_on_web,
  40. (CASE WHEN payment_method='cash' THEN (total_cash - delay_option_total_cash) ELSE 0 END) AS revenue_resv_using_cash,
  41. (CASE WHEN payment_method='point' THEN (total_points - delay_option_total_points) ELSE 0 END) AS revenue_resv_using_point,
  42. (total_cash + total_points - delay_option_total_cash - delay_option_total_points) AS total_revenue,
  43. (CASE WHEN payment_method='cash' AND created_by!='store' THEN web_service_fee ELSE 0 END) AS total_web_cash_service_fee,
  44. 0 AS revenue_resv_cancelled,
  45. 0 AS total_cancel_fee,
  46. 0 AS delay_option_total_cash,
  47. 0 AS delay_option_total_points
  48. FROM reservations
  49. WHERE
  50. confirm_start_at IS NOT NULL
  51. AND store_id = 56854
  52. UNION
  53. SELECT
  54. reservations.store_id,
  55. reservations.deleted_at,
  56. reservation_delay_options.approved_at AS time_at,
  57. 0 AS number_resv_by_call,
  58. 0 AS number_resv_on_web,
  59. 0 AS number_resv_using_cash,
  60. 0 AS number_resv_using_point,
  61. 0 AS revenue_resv_by_phone,
  62. 0 AS revenue_resv_on_web,
  63. 0 AS revenue_resv_using_cash,
  64. 0 AS revenue_resv_using_point,
  65. 0 AS total_revenue,
  66. 0 AS total_web_cash_service_fee,
  67. 0 AS revenue_resv_cancelled,
  68. 0 AS total_cancel_fee,
  69. (CASE WHEN reservation_delay_options.payment_method='cash' THEN reservation_delay_options.total_price ELSE 0 END) AS delay_option_total_cash,
  70. (CASE WHEN reservation_delay_options.payment_method='point' THEN reservation_delay_options.total_price ELSE 0 END) AS delay_option_total_points
  71. FROM reservations
  72. LEFT JOIN reservation_delay_options
  73. ON reservations.id = reservation_delay_options.reservation_id
  74. AND reservation_delay_options.deleted_at IS NULL
  75. AND reservation_delay_options.status = 'approved'
  76. AND reservation_delay_options.approved_at IS NOT NULL
  77. WHERE
  78. reservations.confirm_start_at IS NOT NULL
  79. AND reservations.cancelled_at IS NULL
  80. AND reservations.store_id = 56854
  81. UNION
  82. SELECT
  83. store_id,
  84. deleted_at,
  85. cancelled_at AS time_at,
  86. 0 AS number_resv_by_call,
  87. 0 AS number_resv_on_web,
  88. 0 AS number_resv_using_cash,
  89. 0 AS number_resv_using_point,
  90. 0 AS revenue_resv_by_phone,
  91. 0 AS revenue_resv_on_web,
  92. 0 AS revenue_resv_using_cash,
  93. 0 AS revenue_resv_using_point,
  94. 0 AS total_revenue,
  95. 0 AS total_web_cash_service_fee,
  96. (CASE WHEN cancelled_at IS NOT NULL THEN total_cash + total_points ELSE 0 END) AS revenue_resv_cancelled,
  97. (CASE WHEN cancelled_at IS NOT NULL THEN cancel_fee ELSE 0 END) AS total_cancel_fee,
  98. 0 AS delay_option_total_cash,
  99. 0 AS delay_option_total_points
  100. FROM reservations
  101. WHERE
  102. reservations.cancel_fee > 0
  103. AND reservations.cancelled_at IS NOT NULL
  104. AND store_id = 56854
  105. ) AS reservations
  106. ON
  107. to_timestamp(time_at) AT TIME ZONE 'Asia/Tokyo' >= date_ranges.range_from
  108. AND to_timestamp(time_at) AT TIME ZONE 'Asia/Tokyo' < date_ranges.range_to
  109. GROUP BY date_ranges.range_from, reservations.deleted_at
  110. ORDER BY date_ranges.range_from
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement