Solved

Selecting a distinct column that is a CLOB datatpye

Posted on 2004-08-25
5
3,731 Views
Last Modified: 2011-09-23
I have the following table:

create table foo(
base_id      number(12),
threat_id    number(12),
description CLOB,
Unique(base_id,threat_id))

My first attempt at my query is the following:

    select distinct description,threat_id from foo where threat_id=1234567;

...and I got the error:
   Inconsistent datatypes: expected - got CLOB

Since I know I can't select distinct on a clob datatype, How can I select from this
table such that I do not get duplicate descriptions?

ipaman
0
Comment
Question by:ipaman
5 Comments
 
LVL 35

Expert Comment

by:Mark Geerlings
ID: 11896346
The supplied PL\SQL package: dbms_lob has lots of procedures and functions that you can use on CLOB columns, but no, you cannot use most standard SQL commands on CLOB columns.
0
 
LVL 7

Expert Comment

by:BobMc
ID: 11898666
Just a thought - do what Oracle does when you enter a query - hash the description into something more manageable, and put it in another column, and select distinct on that instead.

Assuming you arent updating these descriptions continuously, it will be much more efficient than scanning your CLOBs again and again.

I havent got a decent hash algorithm at hand, but a simple convert it all to ASCII character by character and add it up isnt a bad starting point.

HTH
Bob
0
 
LVL 7

Expert Comment

by:grim_toaster
ID: 11902191
A perhaps not-so-elegant solution (but one that works - if slowly) would be to take the first 4000 characters of your clob, cast it to a varchar2 and then do a distinct on that!  Fewer characters would be preferable, and this would only work if the differences do occur in the first 4000 characters.

SELECT DISTINCT CAST (SUBSTR(description, 0, 4000) AS VARCHAR2(4000)) AS description, threat_id
FROM   foo
0
 
LVL 35

Expert Comment

by:Mark Geerlings
ID: 11902198
Also, be careful with the "distinct" operator in Oracle queries.  If you have SQL Server experience, you may be used to including that in most queries out of habit. ***DON'T DO THAT IN ORACLE QUERIES!***  If you only select one column, then it is OK, but if you want to select multiple columns based on a distinct value in one (or more) columns, test the results and the response times thoroughly before you deploy this.  "Distinct" will always force a sort that you may not need.   Make sure that Oracle's implementation of "distinct" matches your knowledge of the data - the results may surprise you.  It usually works better in Oracle to use a group operator (min, max, avg, count, etc.) when you want a distinct result set.
0
 
LVL 23

Accepted Solution

by:
paquicuba earned 50 total points
ID: 11905001
markgeer is right, you can use:

select distinct dbms_lob.substr(description) description, threat_id from foo where threat_id = 1234567;

Good luck!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problems using Provider=OraOLEDB.Oracle via VBScript/Classic ASP 5 68
Processing of multiple cursor 6 47
Oracle - SQL Parse String 5 34
Oracle DBLINKS From 11g to 8i 3 35
Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This video shows how to recover a database from a user managed backup

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question