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

分割2条SELECT语句-“SQL命令未正确结束”错误,sql,oracle,oracle-sqldeveloper,ora-00933,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' a

我得到以下语句主题行中引用的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;
我在其他帖子中尝试过删除别名,但这并没有改变问题。有什么想法吗?谢谢。

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

子查询不必命名为。。。仅当它们被直接引用时,即如果完整查询中有多个同名列

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

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


如果在SQLPlus中运行此操作,则可能会将第一列中的除法运算符误解为语句终止符字符。其他工具也可能易受影响。尝试移动除法运算符,例如,其中list_price=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 ...
 )
)

不知何故,我不能粘贴您的实际子选择代码在这里,得到错误提交您的文章。。。没有测试,因为我没有您的表格。

您能说得更具体一点吗?我尝试了几种不同的方法,但仍然得到相同的错误。子查询不必命名。。。仅当它们被直接引用时,即如果完整查询中有多个列具有相同的名称。@Ben:有趣的是,SQL Server和MySQL都需要名称。
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 ...
 )
)