Skip to content

Commit

Permalink
Update C_Ingredient_Optimization.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Mylinear authored Sep 20, 2024
1 parent 5204f1a commit 54ca6b7
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions Case_Study_2_Pizza_Runner/C_Ingredient_Optimization.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,55 @@ FROM pizza_recipes c
yine ayni şekilde çıkan sonucu int cast ediyorum. Böylece başka tablolar ile joninleme işlemi yaptığımda hata almayacağım.

output:

![image](https://github.com/user-attachments/assets/b53979d2-028c-4fcf-830b-a2d73cc6791c)


Stage 2
Bu oluşturmuş olduğum tabloyu `pizza_names` ve `pizza_toppings` tabloları ile joinliyorum. Böylece pizza isimlerini ve topping isimlerini birlikte görebiliyoruz.

```sql
WITH toppings_cte AS (
SELECT
pizza_id,
REGEXP_SPLIT_TO_TABLE(toppings, '[,\s]+')::INTEGER AS top_id
FROM pizza_recipes
)
SELECT pizza_name,
tcte.pizza_id,
top_id,
topping_name
from toppings_cte tcte
join pizza_names pn on pn.pizza_id = tcte.pizza_id
join pizza_toppings pt on pt.topping_id = tcte.top_id
```

Stage 3

Son aşamada ise pizza isimlerine göre gruplayıp topping isimlerini string_agg fonksiyonu ile birleştiriyorum.

```sql
WITH toppings_cte AS (
SELECT
pizza_id,
REGEXP_SPLIT_TO_TABLE(toppings, '[,\s]+')::INTEGER AS top_id
FROM pizza_recipes
),
joined_table as (
SELECT pizza_name,
tcte.pizza_id,
top_id,
topping_name
from toppings_cte tcte
join pizza_names pn on pn.pizza_id = tcte.pizza_id
join pizza_toppings pt on pt.topping_id = tcte.top_id
)
SELECT
pizza_name,
STRING_AGG(topping_name, ', ') AS toppings_list
from joined_table
group by pizza_name
```
output

![image](https://github.com/user-attachments/assets/60eb45be-5bac-4fc2-b6ba-d02508f05017)

0 comments on commit 54ca6b7

Please sign in to comment.