ALIAS在where子句中不起作用

时间:2015-02-02 14:52:37

标签: sql oracle substring alias where-clause

我的列RESPONSE_XML and REQUEST_XML由大字符串组成。我已经为这个大字符串使用了substring函数,以便从RESPONSE_XML获取SUBSCRIPTION_ID,从REQUEST_XML获取orderType。查询工作正常。但是现在我想为这个查询添加条件,使它只返回orderType='NEW'的SUBSCRIPTION_ID。我将Substring的结果存储到别名中,并在where condition中使用这些别名。但它不起作用并给出错误ORA-01722 Invalid numnber。这是我的疑问:

    SELECT REPLACE(REPLACE(REGEXP_SUBSTR(RESPONSE_XML, '<ax2147:subscriptions xsi:type="ax2127:SubscriptionDTO"><ax2130:id>\d+</ax2130:id>'), 
    '<ax2147:subscriptions xsi:type="ax2127:SubscriptionDTO"><ax2130:id>', ''), '</ax2130:id>', '') 
    AS SUBSCRIPTION_ID , 
    REPLACE(REPLACE(REGEXP_SUBSTR(REQUEST_XML, '<ns7:orderType>\d+</ns7:orderType>'), '<ns7:orderType>', ''), '</ns7:orderType>', '') 
    AS order_type
    FROM
    SOAP_MONITORING 
    where WEB_SERVICE_NAME='RatorWebShopService' and WEB_METHOD_NAME='placeShopOrder' 
and order_type='NEW' order by CREATE_DATE desc

我也以这种方式尝试了查询,但导致错误,即第10行的ORA-00932数据类型不一致

SELECT REPLACE(REPLACE(REGEXP_SUBSTR(RESPONSE_XML, '<ax2147:subscriptions xsi:type="ax2127:SubscriptionDTO"><ax2130:id>\d+</ax2130:id>'), 
'<ax2147:subscriptions xsi:type="ax2127:SubscriptionDTO"><ax2130:id>', ''), '</ax2130:id>', '') 
AS SUBSCRIPTION_ID from SOAP_MONITORING 
where 
REQUEST_XML
in
(
REPLACE(REPLACE(REGEXP_SUBSTR(REQUEST_XML, '<ns7:orderType>\d+</ns7:orderType>'), '<ns7:orderType>', ''), '</ns7:orderType>', '') 
)
and WEB_SERVICE_NAME='RatorWebShopService' and WEB_METHOD_NAME='placeShopOrder' and REQUEST_XML='NEW'

1 个答案:

答案 0 :(得分:0)

为了对列别名执行谓词(WHERE),您可以使用内联视图(也称为子查询,尽管子查询实际上更常用于不同的事物)或常见表格表达式(也称为CTE或“with”子句)。

内联视图你可以这样做:

select iw.order_type
from (
   SELECT REPLACE(REPLACE(REGEXP_SUBSTR(RESPONSE_XML, '<ax2147:subscriptions xsi:type="ax2127:SubscriptionDTO"><ax2130:id>\d+</ax2130:id>'), 
   '<ax2147:subscriptions xsi:type="ax2127:SubscriptionDTO"><ax2130:id>', ''), '</ax2130:id>', '') 
   AS SUBSCRIPTION_ID , 
   REPLACE(REPLACE(REGEXP_SUBSTR(REQUEST_XML, '<ns7:orderType>\d+</ns7:orderType>'), '<ns7:orderType>', ''), '</ns7:orderType>', '') 
   AS order_type,
   CREATE_DATE
   FROM
   SOAP_MONITORING 
   where WEB_SERVICE_NAME='RatorWebShopService' and WEB_METHOD_NAME='placeShopOrder' 
) iw
where iw.order_type='NEW'
order by iw.CREATE_DATE desc

公用表表达式可以这样做:

with cte as (
   SELECT REPLACE(REPLACE(REGEXP_SUBSTR(RESPONSE_XML, '<ax2147:subscriptions xsi:type="ax2127:SubscriptionDTO"><ax2130:id>\d+</ax2130:id>'), 
   '<ax2147:subscriptions xsi:type="ax2127:SubscriptionDTO"><ax2130:id>', ''), '</ax2130:id>', '') 
   AS SUBSCRIPTION_ID , 
   REPLACE(REPLACE(REGEXP_SUBSTR(REQUEST_XML, '<ns7:orderType>\d+</ns7:orderType>'), '<ns7:orderType>', ''), '</ns7:orderType>', '') 
   AS order_type,
   CREATE_DATE
   FROM
   SOAP_MONITORING 
   where WEB_SERVICE_NAME='RatorWebShopService' and WEB_METHOD_NAME='placeShopOrder' 
)
select cte.order_type
from cte
where cte.order_type='NEW'
order by cte.CREATE_DATE desc