有人可以在下面的查询中看到我出错的地方吗?我收到GROUP BY
列不存在的错误消息,但是当我不使用GROUP BY
时,我在输出中看到列名时显然会这样做。
SELECT
(SELECT customer_address.post_code FROM customer_address WHERE customer_address.address_type = 0 AND customer_address.customer_no = orders.customer_no) postcode, SUM(orders.order_no) orders
FROM
orders, customer_address
WHERE
orders.delivery_date = '27-MAY-15'
GROUP BY
postcode;
答案 0 :(得分:2)
答案是:您不能在GROUP BY中使用别名。
所以:
GROUP BY (SELECT customer_address.post_code ...);
或者:
select postcode, sum(order_no)
from
(
SELECT
(SELECT customer_address.post_code FROM customer_address WHERE customer_address.address_type = 0 AND customer_address.customer_no = orders.customer_no) postcode,
orders.order_no
FROM orders, customer_address
WHERE orders.delivery_date = '27-MAY-15'
)
GROUP BY postcode;
编辑:
但是,您的查询似乎有误。为什么要交叉加入订单和customer_address?我猜错了。使用连接时使用显式连接(INNER JOIN customer_address ON ...
)以避免此类错误。但在这里,我猜您只需删除, customer_address
。
那为什么要添加订单号?这似乎没有意义。