Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. select T1.month, T1.order_count,
  2.     100 * (T1.order_count - lag(T1.order_count, 1) over (order by T1.month)) / lag(T1.order_count, 1) over (order by T1.month) ||'%' as order_growth,
  3.     T1.order_count/T2.hour_count as drh,
  4.     T1.restaurants,
  5.     100 * (T1.restaurants - lag(T1.restaurants, 1) over (order by T1.month)) / lag(T1.restaurants, 1) over (order by T1.month) ||'%' as restaurant_growth,
  6.     T1.runners,
  7.     100 * (T1.runners - lag(T1.runners, 1) over (order by T1.month)) / lag(T1.runners, 1) over (order by T1.month) ||'%' as runner_growth,
  8.     T1.GR,
  9.     to_char((100 * (T1.GR::numeric - lag(T1.GR::numeric, 1) over (order by T1.month)) / lag(T1.GR::numeric, 1) over (order by T1.month)), '999D99%') as GR_growth,
  10.     cast(((T1.order_count * 1.50) + (4 * T2.hour_count))::numeric as money) as COGS,
  11.     to_char((100 * (((T1.order_count * 1.50) + (4 * T2.hour_count)) - lag(((T1.order_count * 1.50) + (4 * T2.hour_count)), 1) over (order by T1.month))
  12.         / lag(((T1.order_count * 1.50) + (4 * T2.hour_count)), 1) over (order by T1.month)), '999D99%') as COGS_growth,
  13.     to_char((((T1.GR::numeric::float8 - ((T1.order_count * 1.50) + (4 * T2.hour_count))) / T1.GR::numeric::float8)*100.0), '999D99%') as GM,
  14.     to_char((100 * ((((T1.GR::numeric::float8 - ((T1.order_count * 1.50) + (4 * T2.hour_count))) / T1.GR::numeric::float8)*100.0)
  15.         - lag((((T1.GR::numeric::float8 - ((T1.order_count * 1.50) + (4 * T2.hour_count))) / T1.GR::numeric::float8)*100), 1) over (order by T1.month))
  16.         / lag((((T1.GR::numeric::float8 - ((T1.order_count * 1.50) + (4 * T2.hour_count))) / T1.GR::numeric::float8)*100.0), 1) over (order by T1.month))
  17.         , '999D99%') as GM_Growth,
  18.     cast(((T1.GR::numeric::float8 - ((T1.order_count * 1.50) + (4 * T2.hour_count)))/T1.restaurants)::numeric as money) as GPPR,
  19.     to_char((100 * (((T1.GR::numeric::float8 - ((T1.order_count * 1.50) + (4 * T2.hour_count)))/T1.restaurants)
  20.         - lag(((T1.GR::numeric::float8 - ((T1.order_count * 1.50) + (4 * T2.hour_count)))/T1.restaurants), 1) over (order by T1.month))
  21.         / lag(((T1.GR::numeric::float8 - ((T1.order_count * 1.50) + (4 * T2.hour_count)))/T1.restaurants), 1) over (order by T1.month))
  22.         , '999D99%') as GPPR_Growth
  23. from
  24. (
  25.     select
  26.         to_char(transactions.delivered_at_est, 'YYYY-MM') || ' (' || trim(trailing ' ' from to_char(time_requested, 'Month')) || ')' as month,
  27.         count(distinct transactions.id) as order_count,
  28.         count(distinct transactions.seller_id) as restaurants,
  29.         count(distinct transactions.runner_id) as runners,
  30.         cast(SUM(daas_rate)::numeric as money) as GR
  31.     from transactions
  32.     join territories on transactions.territory_id = territories.id
  33.     join businessprofiles on transactions.seller_id = businessprofiles.id
  34.     where
  35.         method = 'Delivery' and
  36.         time_requested between ((now() + interval '-720 day')::date + interval '3 hours') and ((now()::date + interval '4 hours')) and
  37.         status = 'completed' and
  38.         transactions.is_test = false and
  39.         transactions.company_name != 'Wawa' and transactions.company_name != 'TEST VENDOR'
  40.     group by month
  41.     order by month
  42. ) T1
  43.  
  44. join
  45. (
  46.     select
  47.         to_char(shifts.start, 'YYYY-MM') || ' (' || trim(trailing ' ' from to_char(shifts.start, 'Month')) || ')' as month,
  48.         sum(length_hours) as hour_count
  49.     from shifts
  50.     join retreat_points on shifts.retreat_point = retreat_points.id
  51.     join territories on territories.id = retreat_points.territory_id
  52.     where shifts.start between ((now() + interval '-720 day')::date + interval '3 hours') and ((now()::date + interval '4 hours')) and
  53.         shifts.stop between ((now() + interval '-720 day')::date + interval '3 hours') and ((now()::date + interval '4 hours')) and
  54.         shifts.role != 'dispatch' and
  55.         shifts.user_id is not null
  56.     group by month
  57.     order by month
  58. ) T2 on T1.month = T2.month
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement