Solved

Selecting a distinct column that is a CLOB datatpye

Posted on 2004-08-25
5
3,441 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 34

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 34

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.

Join & Write a Comment

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now