Skip to content

Commit

Permalink
Add tourless item migration
Browse files Browse the repository at this point in the history
  • Loading branch information
uhfrb committed Jul 5, 2024
1 parent a644d43 commit b39c06a
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions foodlib/migrations/20240705134012_add_tourless_items_to_list.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
-- Add migration script here
CREATE OR REPLACE VIEW public.shopping_list AS
SELECT full_weight.event_id,
full_weight.event_name,
full_weight.ingredient_id,
full_weight.ingredient,
sum(full_weight.weight) AS weight,
round(sum(full_weight.weight) * ingredients.energy * 1000::numeric, 2) AS energy,
sum(full_weight.weight)::double precision * COALESCE(price_per_ingredient_weight.price, '-1,00 €'::money) AS price,
full_weight.tour_id,
category
FROM ( SELECT prefetch.event_id,
prefetch.tour_id,
prefetch.event_name,
prefetch.ingredient_id,
prefetch.ingredient,
prefetch.store_id,
prefetch.buy_by,
round(GREATEST(prefetch.weight::double precision - COALESCE(event_storage.amount::double precision, 0::double precision), 0::double precision)::numeric, 2) AS weight
FROM ( SELECT shopping_tour_ingredients.event_id,
shopping_tour_ingredients.tour_id,
shopping_tour_ingredients.event AS event_name,
shopping_tour_ingredients.ingredient_id,
shopping_tour_ingredients.ingredient,
shopping_tour_ingredients.store_id,
shopping_tour_ingredients.buy_by,
sum(shopping_tour_ingredients.weight) AS weight
FROM shopping_tour_ingredients
GROUP BY shopping_tour_ingredients.event_id, shopping_tour_ingredients.tour_id, shopping_tour_ingredients.event, shopping_tour_ingredients.ingredient_id, shopping_tour_ingredients.ingredient, shopping_tour_ingredients.buy_by, shopping_tour_ingredients.store_id) prefetch
LEFT JOIN event_storage USING (event_id, ingredient_id)) full_weight
LEFT JOIN best_event_ingredient_sources price_per_ingredient_weight USING (ingredient_id, event_id)
LEFT JOIN metro_categories USING (ingredient_source_id)
LEFT JOIN ingredients USING (ingredient_id)
GROUP BY full_weight.event_id, full_weight.tour_id, full_weight.event_name, full_weight.ingredient_id, full_weight.ingredient, ingredients.energy, price_per_ingredient_weight.price, category;

0 comments on commit b39c06a

Please sign in to comment.