我遇到问题(是的,这是家庭作业)..问题是:
SELECT
语句,为每个拥有这些列的订单的客户返回一行:来自Customers表的email_address
订单数量
每笔订单的总金额(提示:首先,从价格中减去折扣金额。然后,乘以数量。)
仅返回客户订单超过1的行。 按行序项总和的降序对结果集进行排序。 我对此部分的查询有效。它是:
SELECT email_address, COUNT(o.order_id) as number_of_orders, sum((item_price-discount_amount)*quantity) As Total
FROM Customers c JOIN Orders o
ON c.customer_id = o.customer_id
JOIN order_items oi on oi.order_id =o.order_id
GROUP BY email_address
HAVING COUNT (o.order_id) > 1
ORDER BY number_of_orders DESC;`
之后,我应该修改该查询,以便它只计算和总计item_price
值大于400的订单项。我似乎无法弄明白。有人可以指点我正确的方向吗?
答案 0 :(得分:2)
如果item_price是order_items表的一部分,则必须为item_price添加大于400的过滤器。
SELECT email_address, COUNT(o.order_id) as number_of_orders, sum((item_price-discount_amount)*quantity) As Total
FROM Customers c JOIN Orders o
ON c.customer_id = o.customer_id
JOIN order_items oi on oi.order_id =o.order_id
where oi.item_price > 400
GROUP BY email_address
HAVING COUNT (o.order_id) > 1
ORDER BY number_of_orders DESC;
答案 1 :(得分:0)
char
这应该可行。
此外,即使没有名字冲突,你也应该在处理coulmn时使用表别名。它提高了可读性并防止您犯下令人讨厌的错误。