这是我的疑问:
select email_address, count(customer_id) AS order_count, sum((item_price - discount_amount) * (quantity)) AS order_total,
round(avg((item_price - discount_amount) * (quantity)),2) AS avg_order_total
from customers join orders
using(customer_id)
join order_items
using(order_id)
group by customer_id
having count(order_id) > 1
order by round(avg((item_price - discount_amount) * (quantity)),2) desc
我的输出
想要输出
如何使用子查询生成所需的输出?不能使用限制BTW
答案 0 :(得分:0)
以下是猜测,这是否有效,是否更好
SELECT
c.customer_id
, COUNT(o.customer_id) AS order_count
, SUM((oi.item_price - oi.discount_amount) * (oi.quantity)) AS order_total
, ROUND(AVG((oi.item_price - oi.discount_amount) * (oi.quantity)), 2) AS avg_order_total
FROM customers c
INNER JOIN orders o ON c.customer_id = o.customer_id
INNER JOIN order_items oi ON o.order_id = oi.order_id
GROUP BY
c.customer_id
HAVING COUNT(o.order_id) > 1
ORDER BY
ROUND(AVG((oi.item_price - oi.discount_amount) * (oi.quantity)), 2) DESC
我假设这些item_price
,discount_amount
,quantity
来自order_items
表,f不是真的更改" oi。 "正确的别名。