Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.62 KB | None | 0 0
  1. SELECT oc.id,
  2. oc.project_id,
  3. oc.state_id,
  4. to_char(timezone((bpd.time_zone_name)::text, oc.state_arrive_ts), 'YYYY-MM-DD'::text) AS arrive_date,
  5. to_char(timezone((bpd.time_zone_name)::text, oc.state_arrive_ts), 'HH24:MI:SS'::text) AS arrive_time,
  6. timezone((bpd.time_zone_name)::text, oc.state_arrive_ts) AS arrive_ts,
  7. to_char(timezone((bpd.time_zone_name)::text, inc.update_date), 'YYYY-MM-DD'::text) AS up_price_date,
  8. to_char(timezone((bpd.time_zone_name)::text, inc.update_date), 'HH24:MI:SS'::text) AS up_price_time,
  9. timezone((bpd.time_zone_name)::text, inc.update_date) AS up_price_ts,
  10. CASE
  11. WHEN (inc.update_date IS NOT NULL and oc.payment_sub_price != oc.payment_trip_price) THEN true
  12. ELSE false
  13. END AS is_up_price,
  14. to_char(timezone((bpd.time_zone_name)::text, oc.state_taken_ts), 'YYYY-MM-DD'::text) AS take_date,
  15. to_char(timezone((bpd.time_zone_name)::text, oc.state_taken_ts), 'HH24:MI:SS'::text) AS take_time,
  16. to_char(timezone((bpd.time_zone_name)::text, ohsn.date), 'HH24:MI:SS'::text) AS wait_time,
  17. to_char(timezone((bpd.time_zone_name)::text, ohs.date), 'HH24:MI:SS'::text) AS wait_time_end,
  18. to_char(timezone((bpd.time_zone_name)::text, oc.state_complete_ts), 'YYYY-MM-DD'::text) AS complete_date,
  19. to_char(timezone((bpd.time_zone_name)::text, oc.state_complete_ts), 'HH24:MI:SS'::text) AS complete_time,
  20. timezone((bpd.time_zone_name)::text, oc.state_complete_ts) AS complete_ts,
  21. bz_f.name AS zone_from,
  22. bz_t.name AS zone_to,
  23. oc.payment_trip_price,
  24. oc.payment_sub_price,
  25. oc.payment_avg_price,
  26. (oc.payment_trip_price - oc.payment_sub_price) AS diff_price,
  27. CASE
  28. WHEN (inc.update_type = 1 and oc.payment_sub_price != oc.payment_trip_price) THEN 'Да'::text
  29. ELSE ''::text
  30. END AS client_update,
  31. CASE
  32. WHEN (inc.update_type = 2 and oc.payment_sub_price != oc.payment_trip_price) THEN 'Да'::text
  33. ELSE ''::text
  34. END AS driver_update,
  35. oc.driver_id,
  36. (((dc.name)::text || ' '::text) || (dc.lastname)::text) AS driver_names,
  37. dc.phone AS driver_phone,
  38. CASE
  39. WHEN (oc.state_id = 601) THEN 'Да'::text
  40. ELSE ''::text
  41. END AS partner,
  42. cc.nickname AS client_nickname,
  43. cc.user_id,
  44. CASE
  45. WHEN (oc.contact_phone IS NULL) THEN cc.phone_additional
  46. ELSE oc.contact_phone
  47. END AS client_phone,
  48. CASE
  49. WHEN (oc.operator_id IS NULL) THEN 'Приложение'::text
  50. ELSE (oc.operator_id)::text
  51. END AS operator,
  52. oc.operator_id,
  53. CASE
  54. WHEN (oc.payment_method_id = 3) THEN comp.name_short
  55. ELSE ''::character varying
  56. END AS company_name,
  57. oc.comment,
  58. dst.description AS state,
  59. dbp.display_name AS billing_type,
  60. oc.billing_type AS billing_type_id,
  61. CASE
  62. WHEN (oc.billing_type = 1) THEN dt.name
  63. WHEN (oc.billing_type = 2) THEN dp.name
  64. WHEN (oc.billing_type = 3) THEN ds.name
  65. ELSE NULL::character varying
  66. END AS billing_name,
  67. ((((((oc.route -> 0) ->> 'town'::text) || ' '::text) || ((oc.route -> 0) ->> 'street'::text)) ||
  68. ' '::text) ||
  69. ((oc.route -> 0) ->> 'house'::text)) AS route_from,
  70. CASE
  71. WHEN ((oc.route -> 2) IS NOT NULL) THEN (
  72. (((((oc.route -> 1) ->> 'town'::text) || ' '::text) || ((oc.route -> 1) ->> 'street'::text)) ||
  73. ' '::text) || ((oc.route -> 1) ->> 'house'::text))
  74. ELSE ''::text
  75. END AS route_lap_first,
  76. CASE
  77. WHEN ((oc.route -> 3) IS NOT NULL) THEN (
  78. (((((oc.route -> 2) ->> 'town'::text) || ' '::text) || ((oc.route -> 2) ->> 'street'::text)) ||
  79. ' '::text) || ((oc.route -> 2) ->> 'house'::text))
  80. ELSE ''::text
  81. END AS route_lap_second,
  82. CASE
  83. WHEN ((oc.route -> 4) IS NOT NULL) THEN (
  84. (((((oc.route -> 3) ->> 'town'::text) || ' '::text) || ((oc.route -> 3) ->> 'street'::text)) ||
  85. ' '::text) || ((oc.route -> 3) ->> 'house'::text))
  86. ELSE ''::text
  87. END AS route_lap_third,
  88. ((((((oc.route -> '-1'::integer) ->> 'town'::text) || ' '::text) ||
  89. ((oc.route -> '-1'::integer) ->> 'street'::text)) || ' '::text) ||
  90. ((oc.route -> '-1'::integer) ->> 'house'::text)) AS route_to,
  91. (st_di.distanses ->> '202'::text) AS ds_to_client_wait,
  92. (st_di.distanses ->> '301'::text) AS ds_to_client_complete,
  93. (st_di.distanses_to_end ->> '301'::text) AS ds_to_end_complete,
  94. oc.route_distance,
  95. (st_di.distanses ->> '201'::text) AS ds_to_client_found,
  96. (st_di.distanses ->> '401'::text) AS ds_to_client_declined,
  97. (st_di.distanses_to_end ->> '401'::text) AS ds_to_end_declined,
  98. (st_di.distanses ->> '403'::text) AS ds_to_client_declined_cl,
  99. (st_di.distanses_to_end ->> '403'::text) AS ds_to_end_declined_cl,
  100. state.destination
  101. FROM (((((((((((((((orders.common oc
  102. LEFT JOIN (SELECT h_payment.id,
  103. h_payment.order_id,
  104. h_payment.update_date,
  105. h_payment.d_payment_update_type_id AS update_type,
  106. row_number() OVER (PARTITION BY h_payment.order_id ORDER BY h_payment.id DESC) AS rownum
  107. FROM orders.h_payment
  108. WHERE (h_payment.d_payment_update_type_id = ANY (ARRAY [1, 2]))) inc ON (((inc.rownum = 1) AND (inc.order_id = oc.id))))
  109. LEFT JOIN base.zones bz_f ON ((bz_f.id = oc.route_zone_from_id)))
  110. LEFT JOIN base.zones bz_t ON ((bz_t.id = oc.route_zone_to_id)))
  111. LEFT JOIN base.projects_data bpd ON ((bpd.id = oc.project_id)))
  112. LEFT JOIN application.driver_card dc ON ((dc.user_id = oc.driver_id)))
  113. LEFT JOIN application.client_card cc ON ((cc.user_id = oc.client_id)))
  114. LEFT JOIN (select min(update_date) as date, order_id
  115. from orders.h_state
  116. where state_id = 203
  117. group by state_id, order_id) ohs on ohs.order_id = oc.id
  118. LEFT JOIN (select update_date as date, order_id
  119. from orders.h_state
  120. where state_id = 202) ohsn on ohsn.order_id = oc.id
  121. LEFT JOIN contract.clients cont ON ((cont.client_id = oc.client_id)))
  122. LEFT JOIN contract.company_card comp ON ((cont.company_id = comp.id)))
  123. LEFT JOIN orders.d_states dst ON ((dst.id = oc.state_id)))
  124. LEFT JOIN billing.d_billing_types dbp ON ((dbp.id = oc.billing_type)))
  125. LEFT JOIN billing.d_tariffs dt ON ((dt.id = oc.billing_id)))
  126. LEFT JOIN billing.d_packages dp ON ((dp.id = oc.billing_id)))
  127. LEFT JOIN billing.d_sessions ds ON ((ds.id = oc.billing_id)))
  128. LEFT JOIN (SELECT h_state.order_id,
  129. h_state.driver_id,
  130. json_object(array_agg((h_state.state_id)::text),
  131. array_agg((h_state.distance)::text)) AS distanses,
  132. json_object(array_agg((h_state.state_id)::text),
  133. array_agg((h_state.distance_to_end)::text)) AS distanses_to_end
  134. FROM orders.h_state
  135. GROUP BY h_state.order_id, h_state.driver_id) st_di ON (((st_di.order_id = oc.id) AND (oc.driver_id = st_di.driver_id))))
  136. LEFT JOIN (SELECT res.order_id,
  137. array_to_string(
  138. array_agg(((((((res.driver_id || ' '::text) || res.status) || ' '::text) ||
  139. (res.phone)::text) || ' '::text) || res.update_date)),
  140. chr(10)) AS destination
  141. FROM (SELECT hs.order_id,
  142. CASE
  143. WHEN (hs.state_id = 201) THEN 'Назначен'::text
  144. ELSE (('Снялся по причине "'::text || (tr.description)::text) || '"'::text)
  145. END AS status,
  146. hs.driver_id,
  147. dc_1.phone,
  148. hs.update_date
  149. FROM (((orders.h_state hs
  150. LEFT JOIN orders.d_states ds_1 ON ((ds_1.id = hs.state_id)))
  151. LEFT JOIN application.driver_card dc_1 ON ((dc_1.user_id = hs.driver_id)))
  152. LEFT JOIN orders.d_tookoff_reasons tr
  153. ON ((tr.id = (substr(reverse((hs.comment)::text), 1, 1))::integer)))
  154. WHERE ((hs.driver_id IS NOT NULL) AND (hs.state_id = ANY (ARRAY [201, 102])))) res
  155. GROUP BY res.order_id) state ON ((state.order_id = oc.id)));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement