Oracle SQL - 不明确定义的列

时间:2016-01-01 19:47:23

标签: sql oracle

实现此查询 - 抛出“不明确定义的列”的错误。我知道它是RE列名称,并且它们的实例是重复的。无法确定导致错误的原因。

 SELECT case.case_id,
   case.client_id,
   case.description,
   case.date_filed,
   case.date_closed,
   solicitor.solicitor_id,
   solicitor.first_name,
   solicitor.surname,
   SUM(note.time_spent) total_time_spent
FROM note, case
INNER JOIN note
ON note.case_id = case.case_id
INNER JOIN solicitor
ON solicitor.solicitor_id = note.solicitor_id
GROUP BY note.case_id,
     note.solicitor_id,
     case.client_id,
     case.description,
     case.date_filed,
     case.date_closed
ORDER BY note.case_id,
     case.date_filed;

3 个答案:

答案 0 :(得分:2)

notefrom子句中两次。一个简单的规则:从不from子句中使用逗号。然后,GROUP BY子句应该包含FROM子句中的所有非聚合列。

此外,表别名可以使查询更容易编写和阅读:

SELECT c.case_id, c.client_id, c.description, c.date_filed, c.date_closed,
       s.solicitor_id, s.first_name, s.surname,
       SUM(n.time_spent)as  total_time_spent
FROM case c INNER JOIN
     note n
     ON n.case_id = c.case_id INNER JOIN
     solicitor s
     ON s.solicitor_id = n.solicitor_id
GROUP BY c.case_id, c.client_id, c.description, c.date_filed, c.date_closed,
         s.solicitor_id, s.first_name, s.surname,
ORDER BY c.case_id, c.date_filed;

而且,case是表的错误名称,因为它是一个SQL关键字。它不是Oracle保留字,但它仍然看起来很尴尬。

答案 1 :(得分:1)

我看起来真正的错误在于您使用note表两次,而没有别名。

答案 2 :(得分:0)

执行查询

SELECT case.case_id,
   case.client_id,
   case.description,
   case.date_filed,
   case.date_closed,
   solicitor.solicitor_id,
   solicitor.first_name,
   solicitor.surname,
   SUM(note.time_spent) total_time_spent
FROM note, case
INNER JOIN note
ON note.case_id = case.case_id
INNER JOIN solicitor
ON solicitor.solicitor_id = note.solicitor_id
GROUP BY case.case_id,
   case.client_id,
   case.description,
   case.date_filed,
   case.date_closed,
   solicitor.solicitor_id,
   solicitor.first_name,
   solicitor.surname
ORDER BY note.case_id,
     case.date_filed;

我修改了group by子句的列。