Solved

Selecting a distinct column that is a CLOB datatpye

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
passing parameters to sql script oracle 4 62
sort a spool into file output in oracle 1 46
Migration from sql server to oracle 5 27
Oracle Nested table uses ? 2 36
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
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 Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

820 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