执行Oracle查询时出错

时间:2012-09-23 14:57:13

标签: sql oracle

我是Oracle的新手,我正在为我的项目开发一个查询。对我来说似乎一切都还可以但是在执行查询时oracle会给出错误“ORA-00905:missing keyword”。

她是我的查询::

SELECT DISTINCT ba.uuid AS uuid
     , COUNT(*) over() AS rowcount 
FROM basicaddress ba 
WHERE ba.postalcode='143456' 
   OR ba.lastname LIKE '%143456%' 
   OR ba.city LIKE '%143456%'
GROUP BY CASE WHEN ba.postalcode='143456' THEN ba.postalcode, ba.uuid END
       , CASE WHEN ba.lastname LIKE '%143456%' THEN ba.lastname, ba.uuid END
       , CASE WHEN ba.city LIKE '%143456%' THEN ba.city, ba.uuid 
                                           ELSE ba.postalcode,ba.uuid END 
ORDER BY CASE WHEN ba.postalcode='143456' THEN ba.postalcode END DESC
       , CASE WHEN ba.lastname LIKE '%143456%' THEN ba.lastname END ASC
       , CASE WHEN ba.city LIKE '%143456%' THEN ba.city ASC 
                                           ELSE ba.postalcode END DESC

我缺少什么关键词?任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

错误发生在CASE声明中

CASE 
  WHEN ba.postalcode='143456' 
  THEN ba.postalcode, ba.uuid  <-- here. You can return value of only one column
END;                          

您必须在ba.postalcodeba.uuid之间做出决定。或者,如果要查看最终结果集中两列的值,请使用隐式或显式类型转换使用||运算符或CONCAT函数将它们连接起来。

答案 1 :(得分:0)

我怀疑您打算在ELSE表达式中使用CASE子句,可能是这样的:

SELECT DISTINCT ba.uuid AS uuid
     , COUNT(*) AS rowcount
FROM basicaddress ba  
WHERE ba.postalcode='143456'
   OR ba.lastname LIKE '%143456%'
   OR ba.city LIKE '%143456%' 
GROUP BY CASE WHEN ba.postalcode='143456' 
              THEN ba.postalcode
              ELSE ba.uuid END
       , CASE WHEN ba.lastname LIKE '%143456%' 
              THEN ba.lastname
              ELSE ba.uuid END
       , CASE WHEN ba.city LIKE '%143456%' 
              THEN ba.city
              ELSE ba.postalcode,ba.uuid END
ORDER BY CASE WHEN ba.postalcode='143456'
              THEN ba.postalcode END DESC
       , CASE WHEN ba.lastname LIKE '%143456%' 
              THEN ba.lastname END ASC
       , CASE WHEN ba.city LIKE '%143456%'
              THEN ba.city ASC
              ELSE ba.postalcode END DESC

您的over()函数也不需要COUNT()子句。此外,我以某种方式重新构建了您的查询,希望使语法更容易理解,并纠正我认为是CASE个表达式中的语法错误。请注意,为了安全起见,您应始终提供ELSE条件。我会把那部分留给你。