Solved

Selecting a distinct column that is a CLOB datatpye

Posted on 2004-08-25
5
4,289 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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 …
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

717 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