对于员工人数超过5人的每个分支机构,查找每个分支机构的产品数量和购买总数。
SELECT SUM(ORDERS.PRODUCT_NO) AS Products_Sold ,
SUM(ORDERLINE.ORDERLINE_NO) AS Total_Purchases, SUM(EMPLOYEE.EMPLOYEE_NO)
FROM BRANCH JOIN ORDERS
ON BRANCH.BRANCH_NO = ORDERS.BRANCH_NO
JOIN ORDERLINE
ON ORDERS.ORDER_NO = ORDERLINE.ORDER_NO
JOIN EMPLOYEE
ON BRANCH.BRANCH_NO = EMPLOYEE.BRANCH_NO
GROUP BY (SUM(ORDERS.PRODUCT_NO), SUM(ORDERLINE.ORDERLINE_NO),
SUM(EMPLOYEE.EMPLOYEE_NO))
HAVING SUM(EMPLOYEE_NO) >= 5;
我收到了这个错误
ORA-00934: group function is not allowed here
00934. 00000 - "group function is not allowed here"
*Cause:
*Action:
Error at Line: 19 Column: 10
答案 0 :(得分:0)
SELECT SUM(ORDERLINE.PRODUCT_NO) AS Products_Sold , SUM(ORDERLINE.ORDERLINE_NO) AS Total_Purchases
FROM BRANCH JOIN ORDERS
ON BRANCH.BRANCH_NO = ORDERS.BRANCH_NO
JOIN ORDERLINE
ON ORDERS.ORDER_NO = ORDERLINE.ORDER_NO
JOIN EMPLOYEE
ON BRANCH.BRANCH_NO = EMPLOYEE.BRANCH_NO
GROUP BY ORDERLINE.PRODUCT_NO, ORDERLINE.ORDERLINE_NO, EMPLOYEE.EMPLOYEE_NO
HAVING SUM(EMPLOYEE_NO) >= 5;
答案 1 :(得分:0)
总结主键似乎不太可能是您的教学期望的答案。
在ORDERLINE的某处,必须有一列或多列代表购买的价值;也许这是一个名为ORDER_VALUE的列,或者您可能需要使用ORDER_LINE.QUANTITY将PRODUCT.COST多个。计算分支机构中的员工数量也是如此。您需要计算,而不是将其主键加起来。
要求是模糊的,因此该解决方案计算销售的不同产品的数量以及订单总数。但是,数据模型(在您的查询中表示)很奇怪:通常我们希望产品在订单行上,因为订单可以有多个产品(这就是为什么我们有线)。
因此,此查询会向您显示如何执行操作,但可能无法生成预期的答案。您需要将其与表结构进行比较并相应地进行修改。
select branch.branch_no
, count(distinct orders.product_no) as no_of_products sold
, count(distinct orders.order_no) as no_of_orders ,
, sum(orderline.amt * product.product_cost) as total_purchase_amt
from (select branch.*
from branch
join employee
on branch.branch_no = employee.branch_no
group by brancn.branch_no
having count(employee_no) >= 5 ) branch
join orders
on branch.branch_no = orders.branch_no
join orderline
on orders.order_no = orderline.order_no
join product
on orders.product_no = product.product_no
group by branch.branch_no;