Skip to content

Instantly share code, notes, and snippets.

@joegaudet
Created November 22, 2019 15:17
Show Gist options
  • Select an option

  • Save joegaudet/3b65de9b1392c0d4298bcf72e5fb523e to your computer and use it in GitHub Desktop.

Select an option

Save joegaudet/3b65de9b1392c0d4298bcf72e5fb523e to your computer and use it in GitHub Desktop.
with menu_item_order_counts as (
select o.id as order_id,
o.menu_id,
oi.menu_item_id,
sum(oi.quantity) as quantity
from orders o
join order_items oi on o.id = oi.order_id
join menu_items mi on oi.menu_item_id = mi.id
join menu_groups mg on mi.menu_group_id = mg.id
where o.menu_id is not null
and o.state = 'closed'
and not mg.is_cutlery
group by o.id, o.menu_id, oi.menu_item_id
order by o.id
),
menu_item_ordering_rates as (
select *,
sum(mioc.quantity) over (partition by order_id) as order_total,
mioc.quantity / sum(mioc.quantity) over (partition by order_id) as rate
from menu_item_order_counts mioc
)
select menu_item_id, avg(rate)
from menu_item_ordering_rates
group by menu_item_id
menu_item_id avg_rate
208374 0.08495670995670995671
21090 0.17174389396611618834
398539 0.10536699587332498725
5413 0.19259259259259259259
425085 0.07692307692307692308
372249 0.1493250339404185558
134102 0.17468253968253968254
93742 0.04347826086956521739
19119 0.12820512820512820513
350955 0.17726522401279502091
113815 0.14019607843137254902
184422 0.16666666666666666667
69449 0.00465116279069767442
209238 0.10115272525124056869
366869 0.11371596762325239147
383853 0.11111111111111111111
175495 0.17718352214717332804
67689 0.1451048951048951049
6597 0.10489536846679703823
353020 0.01724137931034482759
164922 0.06818181818181818182
47518 0.23809523809523809524
370766 0.09365079365079365079
158099 0.17866479925303454715
347059 0.16577540106951871658
46322 0.2761183997628526873
order_id menu_id menu_item_id quantity order_total rate
91276 304 5106 3 6 0.5
91276 304 5109 2 6 0.33333333333333333333
91276 304 30529 1 6 0.16666666666666666667
91277 889 30252 45 46 0.9782608695652173913
91277 889 30459 1 46 0.0217391304347826087
91278 727 22576 8 28 0.28571428571428571429
91278 727 22587 2 28 0.07142857142857142857
91278 727 22594 5 28 0.17857142857142857143
91278 727 22598 5 28 0.17857142857142857143
91278 727 22603 8 28 0.28571428571428571429
91279 476 10818 1 17 0.05882352941176470588
91279 476 10819 1 17 0.05882352941176470588
91279 476 10820 1 17 0.05882352941176470588
91279 476 10829 4 17 0.23529411764705882353
91279 476 10831 4 17 0.23529411764705882353
91279 476 10832 3 17 0.17647058823529411765
91279 476 10834 3 17 0.17647058823529411765
with menu_item_order_counts as (
select o.id as order_id,
o.menu_id,
oi.menu_item_id,
sum(oi.quantity) as quantity
from orders o
join order_items oi on o.id = oi.order_id
join menu_items mi on oi.menu_item_id = mi.id
join menu_groups mg on mi.menu_group_id = mg.id
where o.menu_id is not null
and o.state = 'closed'
and not mg.is_cutlery
group by o.id, o.menu_id, oi.menu_item_id
order by o.id
),
menu_item_ordering_rates as (
select *,
sum(mioc.quantity) over (partition by order_id) as order_total,
mioc.quantity / sum(mioc.quantity) over (partition by order_id) as rate
from menu_item_order_counts mioc
)
select *
from menu_item_ordering_rates;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment