划分2个SELECT语句 - “SQL命令未正确结束”错误

时间:2013-05-15 15:02:42

标签: sql oracle oracle-sqldeveloper ora-00933

我收到以下语句的主题行中引用的ORA-00933错误:

 select
 (select count(name) as PLIs
 from (select
   a.name,
   avg(b.list_price) as list_price
 from 
   crm.prod_int a, crm.price_list_item b
 where 
   a.row_id = b.product_id
   and a.x_sales_code_3 <> '999'
   and a.status_cd not like 'EOL%'
   and a.status_cd not like 'Not%'
   and a.x_sap_material_code is not null
 group by a.name)
 where list_price = 0)
 /
 (select count(name) as PLIs
 from (select
   a.name,
   avg(b.list_price) as list_price
 from 
   crm.prod_int a, crm.price_list_item b
 where 
   a.row_id = b.product_id
   and a.x_sales_code_3 <> '999'
   and a.status_cd not like 'EOL%'
   and a.status_cd not like 'Not%'
   and a.x_sap_material_code is not null
 group by a.name))
 as result from dual;

我已尝试在其他帖子中删除别名作为建议的解决方案,但这并没有改变问题。有任何想法吗?感谢。

3 个答案:

答案 0 :(得分:1)

如果您在SQLPlus中运行它,则可能会在语句终结符字符的第一列中错误解释除法运算符。其他工具也可能易受影响。尝试移动除法运算符,例如where list_price = 0) \

答案 1 :(得分:0)

答案错误,请参阅@Ben评论

不必命名的子查询...只有在它们被直接引用时才会被命名,即如果在完整查询中有多个具有相同名称的列


必须命名子查询。考虑改变:

from (select
      ...
      group by a.name)

要:

from (select
      ...
      group by a.name) SubQueryAlias

答案 2 :(得分:0)

这不会直接回答您的问题,但我认为可以简化查询:

select case PLIs when 0 then -1 else PLIs_noprice / PLIs end from (
 select 
  count(name) as PLIs,
  count(case list_price when 0 then 1 end) as PLIs_noprice
 from (
  .... your innermost subselect, up to "group by" goes here ...
 )
)

不知何故,我无法在此处粘贴您的实际子选择代码,收到“提交帖子时出错”...未经测试,因为我没有您的桌子。