【问题标题】:Dividing 2 SELECT statements - 'SQL command not properly ended' error除以 2 SELECT 语句 - 'SQL 命令未正确结束' 错误
【发布时间】:2013-05-10 05:35:51
【问题描述】:

我收到以下语句的主题行中引用的 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;

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

【问题讨论】:

    标签: sql oracle oracle-sqldeveloper ora-00933


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      回答错误,见@Ben的评论

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


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

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

      收件人:

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

      【讨论】:

      • 你能再具体一点吗?我尝试了几种变体,但仍然遇到同样的错误。
      • 子查询不被命名...仅当它们被直接引用时,即如果在完整查询中有多个具有相同名称的列.
      • @Ben:有趣,SQL Server 和 MySQL 都需要名称。
      【解决方案3】:

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

      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 ...
       )
      )
      

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

      【讨论】:

        猜你喜欢
        • 2016-10-15
        • 1970-01-01
        • 1970-01-01
        • 2019-10-11
        • 2021-06-01
        • 2017-05-13
        • 1970-01-01
        • 2013-12-20
        • 1970-01-01
        相关资源
        最近更新 更多