SELECT DISTINCT CLOB_COLUMN FROM TABLE;

跟風遠走 提交于 2019-12-05 00:25:35

Use this approach. In table profile column content is NCLOB. I added the where clause to reduce the time it takes to run which is high,

with
  r as (select rownum i, content from profile where package = 'intl'),
  s as (select distinct (select min(i) from r where dbms_lob.compare(r.content, t.content) = 0) min_i from profile t where t.package = 'intl')
select (select content from r where r.i = s.min_i) content from s
;

It is not about to win any prizes for efficiency but should work.

You could compare the hashes of the CLOB to determine if they are different:

SELECT your_clob
  FROM your_table
 WHERE ROWID IN (SELECT MIN(ROWID) 
                   FROM your_table
                  GROUP BY dbms_crypto.HASH(your_clob, dbms_crypto.HASH_SH1))

Edit:

The HASH function doesn't guarantee that there will be no collision. By design however, it is really unlikely that you will get any collision. Still, if the collision risk (<2^80?) is not acceptable, you could improve the query by comparing (with dbms_lob.compare) the subset of rows that have the same hashes.

Laurent

If it is acceptable to truncate your field to 32767 characters this works:

select distinct dbms_lob.substr(FIELD_CLOB,32767) from Table1
Tahir Usman

add TO_CHAR after distinct keyword to convert CLOB to CHAR

SELECT DISTINCT TO_CHAR(CLOB_FIELD) from table1;   //This will return distinct values in CLOB_FIELD

select distinct DBMS_LOB.substr(column_name, 3000) from table_name;

Chris Saxon

If truncating the clob to the size of a varchar2 won't work, and you're worried about hash collisions, you can:

  • Add a row number to every row;
  • Use DBMS_lob.compare in a not exists subquery. Exclude duplicates (this means: compare = 0) with a higher rownum.

For example:

create table t (
  c1 clob
);

insert into t values ( 'xxx' );
insert into t values ( 'xxx' );
insert into t values ( 'yyy' );

commit;

with rws as (
   select row_number () over ( order by rowid ) rn,
          t.*
   from   t
)
  select c1 from rws r1
  where  not exists (
    select * from rws r2
    where  dbms_lob.compare ( r1.c1, r2.c1 ) = 0
    and    r1.rn > r2.rn
  );

C1    
xxx   
yyy  

To bypass the oracle error, you have to do something like this :

SELECT CLOB_COLUMN FROM TABLE COPIA C1 WHERE C1.ID IN (SELECT DISTINCT C2.ID FROM COPIA C2 WHERE ....)

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