多内部联接声明

时间:2019-04-11 03:19:26

标签: sql oracle

我目前正在尝试通过几个内部联接语句绘制多个表。但是,我不断遇到以下错误:

ORA-00904: "PART"."ITEM_CLASS": invalid identifier

我已经访问了其他几页here,但是这个问题与我的并不相同。

这是SQL查询:

SELECT Slsrep_Number, AVG(Commission_Rate) AS AVG_Rate, MAX(Total_Commission) as MAX_Comission 
  FROM Sales_Rep 
 WHERE Sales_Rep.Slsrep_Number = Customer.Slrsrep_Number 
   AND Customer.C_Number = Orders.C_Number 
   AND Orders.Order_Number = Order_Line.Order_Number 
   AND Order_Line.Part_Number = Part.Part_Number 
   AND Part.Item_Class = 'SG';

以下是正在使用的表(屏幕截图):

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

这些图片按照我在SQL查询中引用它们的顺序列出。

2 个答案:

答案 0 :(得分:2)

您需要引用FROM子句中的表。您不能只引用它们。

您还应该使用表别名和正确的,明确的,标准 JOIN语法。

所以:

SELECT sr.Slsrep_Number,
       AVG(?.Commission_Rate) AS AVG_Rate, 
       MAX(?Total_Commission) as MAX_Comission 
FROM Sales_Rep sr JOIN
     Customer c
     ON sr.Slsrep_Number = c.Slrsrep_Number JOIN
     Orders o
     ON c.C_Number = o.C_Number JOIN
     Order_Line ol
     ON o.Order_Number = ol.Order_Number 
     Part p
     ON ol.Part_Number = p.Part_Number 
WHERE p.Item_Class = 'SG';

?是带有佣金的列的别名。

查询现在“看起来”正确。但是,我认为它不是特别有用。如果是这种情况,请问另一个问题,并提供样本数据,所需结果以及要完成的操作的说明。

答案 1 :(得分:0)

Customer,Orders,Order_Line and Part表中的from子句丢失。尝试此操作。

SELECT Slsrep_Number,
       AVG(Commission_Rate) AS AVG_Rate,
       MAX(Total_Commission) as MAX_Comission
  FROM Sales_Rep,Customer,Orders,Order_Line,Part
 WHERE Sales_Rep.Slsrep_Number = Customer.Slrsrep_Number
   AND Customer.C_Number = Orders.C_Number
   AND Orders.Order_Number = Order_Line.Order_Number
   AND Order_Line.Part_Number = Part.Part_Number
   AND Part.Item_Class = 'SG';