0

我有一个这样的查询...

SELECT 1,2,3 
FROM (SELECT CASE WHEN something THEN TO_CHAR(1)... END,2,3 
      FROM tables WHERE condtions) 
      WHERE 1 NOT LIKE 'String'

所有列都是 varchar2,我在这个查询中得到一个 Invalid Number 错误,指向内部 case 语句。内部查询单独运行良好..如果我删除外部 WHERE 条件,整个查询运行良好。

我知道这与 SQL 优化器有关,但我在这里做错了什么,我该如何解决这个问题?对字符串或外部选择语句使用 To_CHAR 不起作用...


更新以获得更好的理解,

SELECT COL1, COL2, COL3 FROM (
SELECT CASE WHEN LOGIC THEN TO_CHAR(1) ELSE TO_CHAR(0) END AS COL1, 
COUNT(SOME_COL) AS  COL2 , COUNT(SOME_COL2) AS COL3 FROM TABLES WHERE CONDTIONS
) WHERE COL1 NOT LIKE ‘0’

我被告知 Oracle SQL Optimizer 正在干扰并导致上述错误,并且查询必须以不同的方式编写。

4

2 回答 2

0

通过用反引号括起来表示 1 是列名。

SELECT 1,2,3 FROM (SELECT CASE WHEN something THEN TO_CHAR(1)... END,2,3 
FROM tables
WHERE condtions) WHERE `1` NOT LIKE 'String'
于 2013-03-27T08:46:05.307 回答
0

您需要使用正确的列名(以字母开头) - 1,2,3 不起作用(除非引用):

create table tab1 as 
select 1 as pk from dual
union all
select 2 as pk from dual
union all
select 3 as pk from dual;

SELECT col1,col2,col3 
FROM (SELECT (CASE WHEN 1=2 THEN TO_CHAR(1) else to_char(2) END) as col1 ,
      2 as col2,
      3 as col3
      FROM tab1 WHERE pk > 1) 
      WHERE col1 NOT LIKE '%1%'
于 2013-03-27T11:24:01.327 回答