我正在解决 Danny Ma 的 Pizza Runner SQL 案例研究。在“定价和评级”一节中,我陷入了两个问题,无法找到正确的解决方案。 案例研究链接:https://8weeksqlchallenge.com/case-study-2/
[cleaned_customer_orders table](https://i.stack.imgur.com/fEgXb.png)
[cleaned_runner_orders table](https://i.stack.imgur.com/sl6kd.png)
[extras table](https://i.stack.imgur.com/Ggm0B.png)
Q1。查询
select
sum(case when cco.pizza_id = 1 then 12 else 10 end) as pizza_cost
from cleaned_customer_orders cco
join pizza_names pn on pn.pizza_id = cco.pizza_id
join cleaned_runner_orders cro on cro.order_id = cco.order_id
where cro.cancellation is null
Q2。查询
with cte_extras_cost as (select
sum(case when pn.pizza_id = 1 then 12 else 10 end) as pizza_cost,
count (e.topping_id)*1 as extras_cost
from cleaned_customer_orders cco
join pizza_names pn on pn.pizza_id = cco.pizza_id
left join extras e on e.record_id = cco.record_id
join cleaned_runner_orders cro on cro.order_id = cco.order_id
where cro.cancellation is null)
select pizza_cost, coalesce(extras_cost, 0) as extras_cost, (pizza_cost+coalesce(extras_cost, 0))
as total_cost
from cte_extras_cost;
Q5。查询
with cte as (
select
sum(cro.distance*0.30) as delivery_cost,
sum(case when cco.pizza_id = 1 then 12 else 10 end) as total_cost
from cleaned_runner_orders cro
join cleaned_customer_orders cco on cro.order_id = cco.order_id
where cro.cancellation is null)
select cte.total_cost, cte.delivery_cost, (cte.total_cost - cte.delivery_cost) as actual_cost
from cte
我的输出:
Q1. 138
Q2。披萨成本=150,额外成本=4,总成本=154
Q5.总成本=138,交货成本=64,实际成本=73
正确答案是
Q1. 138
Q2。披萨成本=138,额外成本=4,总成本=142
Q5.总成本=138,交货成本=45,实际成本=73
如果您发布插入语句来填充临时表会更好
q1
select
sum(case when cco.pizza_id = 1 then 12 else 10 end) as pizza_cost
from customer_orders cco
left join runner_orders cro on cro.order_id = cco.order_id
where coalesce(cro.cancellation,'') not in ('Restaurant Cancellation', 'Customer Cancellation')
q2
with
cco as
(select *, row_number() over(order by order_id, pizza_id) as rn from customer_orders)
, ex as (
select rn, case when extras = 'null' then null else extras end as extras from
cco
where coalesce(case when extras = 'null' then null else extras end,'') <> ''
)
, ex1 as (
select trim(unnest(string_to_array(extras, ',')))::int as topping_id, rn from ex
)
, extras as (
select count(topping_id) as topping_cnt, rn from ex1 group by rn
)
, cte_extras_cost as (
select
sum(case when pn.pizza_id = 1 then 12 else 10 end) pizza_cost
, sum(topping_cnt*1) as extras_cost
from cco
join pizza_names pn on pn.pizza_id = cco.pizza_id
left join extras e on e.rn = cco.rn
join runner_orders cro on cro.order_id = cco.order_id
where coalesce(cro.cancellation,'') not in ('Restaurant Cancellation', 'Customer Cancellation')
)
select pizza_cost, coalesce(extras_cost, 0) as extras_cost, (pizza_cost+coalesce(extras_cost, 0))
as total_cost
from cte_extras_cost;
q5
with cro as ( select case when distance <> '' then cast(distance as numeric) else 0 end as distance, order_id, runner_id, pickup_time, duration, cancellation from ( SELECT order_id, runner_id, pickup_time, replace(replace(distance, 'km', ''), 'null', '') as distance, duration, cancellation from pizza_runner.runner_orders ) q1 ) , cco as ( select sum(case when pizza_id = 1 then 12 else 10 end) as pizza_cost, order_id from customer_orders group by order_id ) , cte as (
select
sum(cro.distance*0.30) as delivery_cost,
sum(pizza_cost) as total_cost from cro join cco on cro.order_id = cco.order_id where coalesce(cro.cancellation,'') not in ('Restaurant Cancellation', 'Customer Cancellation') ) select cte.total_cost, cte.delivery_cost, (cte.total_cost - cte.delivery_cost) as actual_cost from cte