Created
July 21, 2020 21:22
-
-
Save joegaudet/8cb9013eb77916f8b1bc69e71316d085 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with formated_order_items as ( | |
select oi.id, | |
o.id as order_id, | |
oi.quantity, | |
(string_agg(oi.quantity || ' - ' || mi.name, CHR(10))) as description, | |
string_agg(mog.verb || ' ' || moi.name, ',') as pickles, | |
string_agg(dt.name, ',') as dietary_tags | |
from order_items oi | |
join menu_items mi on oi.menu_item_id = mi.id | |
join order_item_menu_option_items oimoi on oi.id = oimoi.order_item_id | |
join menu_option_items moi on oimoi.menu_option_item_id = moi.id | |
join menu_option_groups mog on moi.menu_option_group_id = mog.id | |
join dietary_tags_menu_items dtmi on mi.id = dtmi.menu_item_id | |
join dietary_tags dt on dtmi.dietary_tag_id = dt.id | |
join orders o on oi.order_id = o.id | |
where o.driver_id = 49149 | |
and o.state not in ('cancelled') | |
and o.pickup_at >= now() | |
group by o.id, oi.id, mi.name | |
), | |
formatted_orders as ( | |
select foi.order_id, | |
sum(foi.quantity) as quantity, | |
string_agg(foi.description || ' ' || foi.pickles || ' ' || foi.dietary_tags, ', ') | |
from formated_order_items foi | |
group by foi.order_id | |
) | |
select * from formatted_orders; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment