Advertisement
nsv54

Untitled

Nov 5th, 2020 (edited)
797
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. WITH orders AS (
  2.     SELECT
  3.         doc::json AS dj
  4.     FROM common.order_proc_rotax
  5.     LIMIT 100000
  6. ),
  7. parsed_orders AS (
  8.     SELECT
  9.         dj -> 'order' ->> 'application' AS app,
  10.         (dj -> 'order' ->> 'cost')::DECIMAL AS cost,
  11.         (dj ->> 'created')::TIMESTAMP AS created_dttm,
  12.         dj -> 'order' ->> 'taxi_status' as taxi_status,
  13.         dj -> 'order' ->> 'discount' as discount
  14.     FROM orders
  15. )
  16.  
  17. SELECT date_trunc('day', created_dttm) AS day,
  18.        avg(cost) AS mean_cost,
  19.        COUNT(*) AS total_rides
  20. FROM parsed_orders
  21. WHERE taxi_status = 'complete' and discount is not null
  22. GROUP BY day
  23. ORDER BY day;
  24.  
  25. -- discount
  26. WITH orders AS (
  27.     SELECT
  28.         doc::json AS dj
  29.     FROM common.order_proc_rotax
  30.     LIMIT 100000
  31. ),
  32. parsed_orders AS (
  33.     SELECT
  34.         dj -> 'order' ->> 'application' AS app,
  35.         (dj -> 'order' ->> 'cost')::DECIMAL AS cost,
  36.         (dj ->> 'created')::TIMESTAMP AS created_dttm,
  37.         dj -> 'order' ->> 'taxi_status' as taxi_status,
  38.         (dj -> 'order' -> 'discount' ->> 'price')::decimal as orig_price,
  39.         (dj -> 'order' -> 'discount' ->> 'value')::decimal as discount_coef
  40.     FROM orders
  41. )
  42. SELECT date_trunc('day', created_dttm) AS day,
  43.        sum(orig_price * discount_coef),
  44.     sum(cost) as cost_sum,
  45.     sum(orig_price) as sum_or
  46. FROM parsed_orders
  47. WHERE taxi_status = 'complete' and orig_price is not null
  48. group by day
  49.  
  50.  
  51.  
  52. --------------------------------------------------
  53. ----- revenue ------------------------------------
  54. WITH orders AS (
  55.     SELECT
  56.         doc::json AS dj
  57.     FROM common.order_proc_rotax
  58.     LIMIT 10000
  59. ),
  60. parsed_orders AS (
  61.     SELECT
  62.         (dj -> 'order' ->> 'cost')::DECIMAL AS cost,
  63.         (dj ->> 'created')::TIMESTAMP AS created_dttm,
  64.         dj -> 'order' ->> 'taxi_status' as taxi_status,
  65.         (dj -> 'order' -> 'discount' ->> 'price')::decimal as orig_price,
  66.         (dj -> 'order' -> 'discount' ->> 'value')::decimal as discount_coef,
  67.         (dj -> 'order' -> 'coupon' ->> 'value')::decimal as coupon_value,
  68.         (dj -> 'order' -> 'coupon' ->> 'was_used')::boolean as coupon_used
  69.     FROM orders
  70. ),
  71. revenues as (
  72.     select
  73.            cost,
  74.            created_dttm,
  75.            taxi_status,
  76.            coalesce(orig_price, 0) as orig_price,
  77.            coalesce(discount_coef, 0) as discount_coef,
  78.            coalesce(coupon_value, 0) as coupon_value,
  79.            coalesce(coupon_used, FALSE) as coupon_used
  80.     from parsed_orders
  81. )
  82. SELECT
  83.      date_trunc('day', created_dttm) AS day,
  84.      sum(orig_price * discount_coef
  85.              + cast(coupon_used as int) * (cast(coupon_value >= cost as int) * cost
  86.                                     + cast(coupon_value < cost as int) * coupon_value)) as discount_sum,
  87.       sum(cost + orig_price * discount_coef) as revenue_sum,
  88.      sum(
  89.                  cast(cost < 100 as int) *
  90.                  (cast(orig_price > 0 as int) * orig_price * 0.9 + cast(orig_price = 0 as int) * cost * 0.9) +
  91.                  cast(cost > 100 and cost < 200 as int) *
  92.                  (cast(orig_price > 0 as int) * orig_price * 0.874 + cast(orig_price = 0 as int) * cost * 0.874) +
  93.                  cast(cost > 200 and cost < 300 as int) *
  94.                  (cast(orig_price > 0 as int) * orig_price * 0.863 + cast(orig_price = 0 as int) * cost * 0.863) +
  95.                  cast(cost > 300 and cost < 400 as int) *
  96.                  (cast(orig_price > 0 as int) * orig_price * 0.857 + cast(orig_price = 0 as int) * cost * 0.857) +
  97.                  cast(cost > 400 and cost < 500 as int) *
  98.                  (cast(orig_price > 0 as int) * orig_price * 0.854 + cast(orig_price = 0 as int) * cost * 0.854) +
  99.                  cast(cost > 500 and cost < 600 as int) *
  100.                  (cast(orig_price > 0 as int) * orig_price * 0.851 + cast(orig_price = 0 as int) * cost * 0.851) +
  101.                  cast(cost > 600 and cost < 700 as int) *
  102.                  (cast(orig_price > 0 as int) * orig_price * 0.85 + cast(orig_price = 0 as int) * cost * 0.85) +
  103.                  cast(cost > 700 and cost < 800 as int) *
  104.                  (cast(orig_price > 0 as int) * orig_price * 0.848 + cast(orig_price = 0 as int) * cost * 0.848) +
  105.                  cast(cost > 800 and cost < 900 as int) *
  106.                  (cast(orig_price > 0 as int) * orig_price * 0.847 + cast(orig_price = 0 as int) * cost * 0.847) +
  107.                  cast(cost > 900 and cost < 1000 as int) *
  108.                  (cast(orig_price > 0 as int) * orig_price * 0.846 + cast(orig_price = 0 as int) * cost * 0.846) +
  109.                  cast(cost > 1000 as int) *
  110.                  (cast(orig_price > 0 as int) * orig_price * 0.844 + cast(orig_price = 0 as int) * cost * 0.844)
  111.          ) as taxi_comission_sum
  112. FROM
  113.     revenues
  114. WHERE
  115.      taxi_status = 'complete'
  116.  group by
  117.      day, taxi_status
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement