Advertisement
nsv54

Untitled

Nov 6th, 2020
1,030
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- drop-ы
  2. alter table rotax_team.price_modifiers
  3. drop column tips_type;
  4.  
  5. alter table rotax_team.price_modifiers
  6. drop column tips_value;
  7.  
  8. alter table rotax_team.price_modifiers
  9. drop column discount_coef;
  10.  
  11. alter table rotax_team.price_modifiers
  12. drop column coupon_value;
  13.  
  14. alter table rotax_team.price_modifiers
  15. drop column coupon_applied;
  16.  
  17. alter table rotax_team.orders
  18. drop column total_paid;
  19.  
  20. alter table rotax_team.waiting_statuses
  21. rename column riding_time to transporting_time;
  22.  
  23. alter table rotax_team.waiting_statuses
  24. rename column riding_status to transporting_status;
  25.  
  26. -- price_modifiers.tips_value
  27. WITH orders AS (
  28.     SELECT
  29.         doc::json AS dj
  30.     FROM common.order_proc_rotax
  31.     LIMIT 100000
  32. ),
  33. parsed_orders AS (
  34.     SELECT
  35.         dj ->> '_id' as id,
  36.         dj -> 'order' -> 'creditcard' -> 'tips' ->> 'type' as tips_type,
  37.         (dj -> 'order' -> 'creditcard' -> 'tips' ->> 'value')::decimal as tips_value,
  38.         (dj -> 'order' ->> 'cost')::DECIMAL AS cost
  39.     FROM orders
  40. )
  41. SELECT
  42.        id,
  43.        case when tips_type='percent' then cost * tips_value / 100
  44.             when tips_type='flat' then tips_value
  45.             else null
  46.        end as tips_value
  47. FROM parsed_orders
  48. where tips_value is not null;
  49.  
  50. -- price_modifiers.discount_value, price_modifiers.coupon_value
  51. WITH orders AS (
  52.     SELECT
  53.         doc::json AS dj
  54.     FROM common.order_proc_rotax
  55.     LIMIT 100000
  56. ),
  57. parsed_orders AS (
  58.     SELECT
  59.         dj ->> '_id' as id,
  60.         (dj -> 'order' ->> 'cost')::DECIMAL AS cost,
  61.         dj -> 'order' ->> 'taxi_status' as taxi_status,
  62.         (dj -> 'order' -> 'discount' ->> 'price')::decimal as orig_price,
  63.         (dj -> 'order' -> 'discount' ->> 'value')::decimal as discount_coef,
  64.         (dj -> 'order' -> 'coupon' ->> 'value')::decimal as coupon_value,
  65.         (dj -> 'order' -> 'coupon' ->> 'was_used')::boolean as coupon_used
  66.     FROM orders
  67. ),
  68. revenues as (
  69.     select
  70.            id,
  71.            taxi_status,
  72.            coalesce(orig_price, cost) as orig_price,
  73.            coalesce(discount_coef, 0) as discount_coef,
  74.            coalesce(coupon_value, 0) as coupon_value,
  75.            coalesce(coupon_used, FALSE) as coupon_used
  76.     from parsed_orders
  77. )
  78. SELECT
  79.     id,
  80.     orig_price * discount_coef as discount_value,
  81.     case when coupon_used=True and coupon_value >= orig_price then orig_price
  82.          when coupon_used=True and coupon_value < orig_price then coupon_value
  83.          else 0
  84.     end as coupon_value
  85. FROM
  86.     revenues
  87. WHERE
  88.      taxi_status = 'complete' and (discount_coef != 0 or coupon_used = True);
  89.  
  90.  
  91. -- orders.original_price
  92. WITH orders AS (
  93.     SELECT
  94.         doc::json AS dj
  95.     FROM common.order_proc_rotax
  96.     LIMIT 100000
  97. ),
  98. parsed_orders AS (
  99.     SELECT
  100.         dj ->> '_id' as id,
  101.         (dj -> 'order' ->> 'cost')::DECIMAL AS cost,
  102.         (dj -> 'order' -> 'discount' ->> 'price')::decimal as orig_price
  103.     FROM orders
  104. )
  105. SELECT
  106.     id,
  107.     coalesce(orig_price, cost) as original_price
  108. FROM
  109.     parsed_orders;
  110.  
  111.  
  112.  
  113. -- waiting_statuses.waiting_status, waiting_statuses.waiting_time
  114. WITH orders AS (
  115.     SELECT
  116.         doc::json AS dj
  117.     FROM common.order_proc_rotax
  118.     LIMIT 10000
  119. ),
  120. parsed_orders AS (
  121.     SELECT
  122.         dj ->> '_id' as id,
  123.         (dj ->> 'created')::TIMESTAMP AS created_dttm,
  124.         (dj -> 'order_info' -> 'statistics' -> 'status_updates' -> 0 ->> 'c')::timestamp as cr_time,
  125.         (dj -> 'order_info' -> 'statistics' -> 'status_updates' -> -3 ->> 'c')::timestamp as waiting_time1,
  126.         (dj -> 'order_info' -> 'statistics' -> 'status_updates' -> -3 ->> 't') as waiting_status1,
  127.         (dj -> 'order_info' -> 'statistics' -> 'status_updates' -> -2 ->> 'c')::timestamp as waiting_time2,
  128.         (dj -> 'order_info' -> 'statistics' -> 'status_updates' -> -2 ->> 't') as waiting_status2,
  129.         dj -> 'order' ->> 'city' as city
  130.     FROM orders
  131. )
  132. select
  133.     id,
  134.     case when waiting_status1 = 'waiting' then waiting_status1
  135.          when waiting_status2 = 'waiting' then waiting_status2
  136.          else null
  137.     end as waiting_status,
  138.     case when waiting_status1 = 'waiting' then waiting_time1
  139.          when waiting_status2 = 'waiting' then waiting_time2
  140.          else null
  141.     end as waiting_time
  142. from parsed_orders;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement