查询返回ORA-00936:缺少表达式

时间:2017-05-26 08:03:27

标签: oracle

我正在运行以下查询,试图找到第一个插入行的重复实例: -

    SELECT COUNT(*)
    FROM rlx_service_info
    WHERE row(personalaccountid,serviceid,serviceline,
              userserviceid,servicerf) IN (SELECT personalaccountid,
                                                  serviceid,
                                                  serviceline,
                                                  userserviceid,
                                                  servicerf
                                             FROM rlx_service_info
                                            WHERE masteraccountid = 'x'
                                              AND status          = 30
                                              AND rownum          =1
                                            ORDER BY startdate );

2 个答案:

答案 0 :(得分:2)

要修复语法,您必须删除roworder by子句。 此外,如果子查询只提供一行(rownum = 1),则您不需要IN

SELECT COUNT(*)
  FROM rlx_service_info
 WHERE (personalaccountid,
        serviceid,
        serviceline,
        userserviceid,
        servicerf) = (  SELECT personalaccountid,
                                serviceid,
                                serviceline,
                                userserviceid,
                                servicerf
                           FROM rlx_service_info
                          WHERE     masteraccountid = 'x'
                                AND status = 30
                                AND ROWNUM = 1)

例如:

SQL> select 1
  2  from dual
  3  where (1, 2) in ( select 1, 2 from dual);

         1
----------
         1

SQL> select 1
  2  from dual
  3  where row(1, 2) in ( select 1, 2 from dual);
where row(1, 2) in ( select 1, 2 from dual)
               *
ERROR at line 3:
ORA-00936: missing expression

但是,如果您需要子查询仅返回最小startDate的一行,请考虑order byrownum条件组合不是一种方式;你会找到很多关于如何做到这一点的好答案。

例如:

SQL> select *
  2  from (
  3          select 1 x from dual union all
  4          select 2 x from dual
  5       )
  6  where rownum = 1
  7  order by x;

         X
----------
         1

SQL> select *
  2  from (
  3          select 2 x from dual union all
  4          select 1 x from dual
  5       )
  6  where rownum = 1
  7  order by x;

         X
----------
         2

答案 1 :(得分:0)

您可以使用分析函数找到所有重复行(无需第二次表扫描):

SELECT *
FROM   (
  SELECT r.*,
         ROW_NUMBER()
           OVER (
             PARTITION BY personalaccountid,
                          serviceid,
                          serviceline,
                          userserviceid,
                          servicerf
             ORDER BY     startdate
           ) as rn,
         COUNT( CASE WHEN masteraccountid = 'x' AND status = 30 THEN 1 END )
           OVER (
             PARTITION BY personalaccountid,
                          serviceid,
                          serviceline,
                          userserviceid,
                          servicerf
           ) as ct
  FROM   rlx_service_info r
)
WHERE ct > 0
AND   rn > 1;

如果您只想计算行数,请将外部查询更改为:

SELECT COUNT(*)
FROM   (
  SELECT COUNT( CASE WHEN masteraccountid = 'x' AND status = 30 THEN 1 END )
           OVER (
             PARTITION BY personalaccountid,
                          serviceid,
                          serviceline,
                          userserviceid,
                          servicerf
           ) as ct
  FROM   rlx_service_info r
)
WHERE  ct > 0;