Oracle get DISTINCT numeric with a CLOB in the query

依然范特西╮ 提交于 2019-12-31 04:15:10

问题


EDIT: I am looking for a DISTINCT NUMERIC while including a CLOB within the query.

I have two relations.

Relation One:

LOGID_NBR           NUMBER (12)
APPID_NBR           NUMBER (2)      
EVENTID_NBR         NUMBER (10)     
KEYID_NBR           NUMBER (8)      
KEYVALUE            VARCHAR2 (100 Byte)     
ARGUMENTSXML        VARCHAR2 (4000 Byte)    
SENTINDICATOR       CHAR (5 Byte)       
RECEIVED_DATEDATE           DATE sysdate    
LAST_UPDATED        DATE    sysdate 
TEXTINDICATOR       VARCHAR2 (5 Byte)       
UPSELL_ID           VARCHAR2 (5 Byte)       
GECKOIMAGEIND       CHAR (1 Byte)       
DELIVERYTYPE        VARCHAR2 (30 Byte)      

Relation Two:

LOGID_NBR       NUMBER (12)     
INPUT_ARGS      CLOB    

I have queried the relations as follows:

SELECT EVENTID_NBR, INPUT_ARGS 

FROM RELATION_ONE, RELATION_TWO 

WHERE RELATION_ONE.LOGID_NBR = RELATION_TWO.LOGID_NBR AND 
   EVENTID_NBR BETWEEN 143 AND 192 AND 
   EVENTID_NBR != 172 AND SYSDATE - 7 >= RELATION_ONE.LAST_UPDATED 
   ORDER BY EVENTID_NBR;

I am receiving the same EVENTID_NBR in my result set too often and only interested in DISTINCT results. However, adding the DISTINCT keyword to the query as in:

SELECT DISTINCT EVENTID_NBR, INPUT_ARGS ...

produces the following error results:

[Error] Execution (8: 32): ORA-00932: inconsistent datatypes: expected - got CLOB

So, I started searching the net for how to solve this problem and found this and even this. What am I converting this CLOB to that enables a DISTINCT EVENTID_NBR and its' associated CLOB to be present within my result set?


回答1:


If you hash the clob, you can use it in a subquery to grab the max rowid for each eventid_nbr with the same clob hash value. Then you just filter your relation_two table in the where clause.

SELECT EVENTID_NBR, INPUT_ARGS 

FROM RELATION_ONE, RELATION_TWO 

WHERE RELATION_ONE.LOGID_NBR = RELATION_TWO.LOGID_NBR AND 
  EVENTID_NBR BETWEEN 143 AND 192 AND 
  EVENTID_NBR != 172 AND SYSDATE - 7 >= RELATION_ONE.LAST_UPDATED

  AND (RELATION_TWO.EVENTID_NBR, RELATION_TWO.ROWID) IN
      (SELECT DISTINCT EVENTID_NBR,
       MAX(ROWID) OVER (PARTITION BY EVENTID_NBR, DBMS_HASH(INPUT_ARGS,3))
       FROM RELATION_TWO);

The 3 in the HASH specified SHA, but you can also use MD4 (1) or MD5 (2) if you like.

I'm thinking this could be very slow if you have a lot of rows in the RELATION_TWO table and I'm sure this can be written to perform better, but the concept is sound.



来源:https://stackoverflow.com/questions/24184089/oracle-get-distinct-numeric-with-a-clob-in-the-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!