0

处理此查询时出现错误:

SELECT *
FROM example
WHERE pri IN (
    SELECT pri
    FROM (
        SELECT pri
            ,sbst
            ,st
            ,count(*) AS cnt
        FROM example
        WHERE sbst = 'oi'
        GROUP BY pri
        ) AS tmp
    WHERE cnt = 1
        AND st = 'ko'
    )
AND sbst = 'cp';

错误如下:

ORA-00907:缺少右括号 00907。00000 -“缺少右括号” *原因:
*操作:第 5 行错误:第 5 列:136

但我不认为我错过了一个括号。

4

2 回答 2

5

可能你需要 GROUP BY pri, sbst, st

SELECT * 
from example 
WHERE pri IN 
(
    SELECT pri 
    FROM 
    (
        SELECT  pri, 
                sbst, 
                st, 
                count(*) AS cnt 
        FROM example 
        WHERE sbst = 'oi' 
        GROUP BY pri, 
                 sbst, 
                 st
    ) tmp 
    WHERE cnt = 1 AND st = 'ko'
) AND sbst = 'cp';
于 2013-07-31T14:00:19.530 回答
0

请格式化长查询。然后你就可以看出问题所在了。还使用别名来避免隐藏的错误

select *
from example as e
where
   e.pri in (
       select
           tmp.pri
       from (
           select e2.pri, e2.sbst, e2.st, count(*) as cnt
           from example as e2
           where e2.sbst = 'oi'
           group by e2.pri
       ) as tmp where tmp.cnt = 1 and tmp.st = 'ko'
   ) and e.sbst = 'cp';

抱歉,这不是答案,因为我真的不喜欢在 SO 或生产代码中阅读未格式化的查询。

于 2013-07-31T14:04:25.287 回答