此查询会返回季度销售额,但我希望隐藏order_date列以显示在结果中。 order_date仅用于确定该季度是否购买了该商品。
SELECT item.item_id, item.item_name, item.item_type, item.item_price, SUM(orderdetail.order_quantity) AS 'Quantity Ordered',
SUM(item.item_price*orderdetail.order_quantity) AS 'Total Sales Per Item', orders.order_date FROM item
JOIN orderdetail ON item.item_id = orderdetail.item_id
JOIN orders ON orderdetail.order_id = orders.order_id
GROUP BY item.item_id, item.item_name, item.item_price, item.item_type
HAVING order_date BETWEEN '2017-01-01' AND '2017-03-31'
ORDER BY item.item_id;
答案 0 :(得分:2)
只需将其从select
列表中删除即可。这不会阻止您在where
或having
条款中使用它:
SELECT item.item_id, item.item_name, item.item_type, item.item_price, SUM(orderdetail.order_quantity) AS 'Quantity Ordered',
SUM(item.item_price*orderdetail.order_quantity) AS 'Total Sales Per Item'
-- order_date removed here
FROM item
JOIN orderdetail ON item.item_id = orderdetail.item_id
JOIN orders ON orderdetail.order_id = orders.order_id
GROUP BY item.item_id, item.item_name, item.item_price, item.item_type
HAVING order_date BETWEEN '2017-01-01' AND '2017-03-31'
ORDER BY item.item_id;
答案 1 :(得分:1)
不要在select下包含它就像那样简单:)
SELECT item.item_id, item.item_name, item.item_type, item.item_price, SUM(orderdetail.order_quantity) AS 'Quantity Ordered',
SUM(item.item_price*orderdetail.order_quantity) AS 'Total Sales Per Item' FROM item
JOIN orderdetail ON item.item_id = orderdetail.item_id
JOIN orders ON orderdetail.order_id = orders.order_id
GROUP BY item.item_id, item.item_name, item.item_price, item.item_type
HAVING order_date BETWEEN '2017-01-01' AND '2017-03-31'
ORDER BY item.item_id
答案 2 :(得分:1)
从SELECT
中删除列,并将条件放在WHERE
子句而不是HAVING
子句中:
SELECT i.item_id, i.item_name, i.item_type, i.item_price,
SUM(od.order_quantity) AS `Quantity Ordered`,
SUM(i.item_price * od.order_quantity) AS `Total Sales Per Item`
FROM item i JOIN
orderdetail od
ON i.item_id = od.item_id JOIN
orders o
ON od.order_id = o.order_id
WHERE o.order_date BETWEEN '2017-01-01' AND '2017-03-31'
GROUP BY i.item_id, i.item_name, i.item_price, i.item_type
ORDER BY i.item_id;
注意:
WHERE
子句中。它更有效,因为数据在聚合之前而不是之后过滤。o.order_date
是否应该在group by
。如果你想为每个日期分别设一行,它应该只去那里。