Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

oracle - How can I perform a SELECT DISTINCT on all fields except a BLOB?

I'm trying to perform a SELECT DISTINCT query in Oracle, like this:

SELECT 
    MOVIES.TITLE, CERTIFICATIONS.ID, PROJECTION.DAY, TIME_SLOTS.SLOT, 
    PROJECTION.REMAINING_SEATS, IMAGES.IMAGE 
FROM 
    [...]

It doesn't work because the column "IMAGES.IMAGE" is a BLOB. I would like to exclude this field from the DISTINCT (because I don't need it to be unique), and I don't know how. I also tried with a GROUP BY clause, but if I try to GROUP on all the fields except the BLOB, Oracle returns this error:

ORA-00979: not a GROUP BY expression

And if I add the field in the GROUP BY clause, then Oracle returns this:

ORA-00932: inconsistent datatypes: expected - got BLOB

What can I do?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
SELECT DISTINCT MOVIES.TITLE, CERTIFICATIONS.ID, PROJECTION.DAY, TIME_SLOTS.SLOT, PROJECTION.REMAINING_SEATS
FROM [...]

Distinct is applied to all columns from the SELECT list. And yes, you cannot use LOBs in GROUP BY, UNION, DISTINCT etc because Oracle doesn't know how to compare different LOBs

If you want to retrieve BLOB as well you may try something like this:

SELECT MOVIES.TITLE, CERTIFICATIONS.ID, 
       PROJECTION.DAY, TIME_SLOTS.SLOT, PROJECTION.REMAINING_SEATS, IMAGES.IMAGE
FROM (     
  SELECT MOVIES.TITLE, CERTIFICATIONS.ID, 
         PROJECTION.DAY, TIME_SLOTS.SLOT, PROJECTION.REMAINING_SEATS, IMAGES.IMAGE,
         row_number() over (partition by MOVIES.TITLE, CERTIFICATIONS.ID, PROJECTION.DAY, TIME_SLOTS.SLOT, PROJECTION.REMAINING_SEATS 
                            order by PROJECTION.DAY, TIME_SLOTS.SLOT) RW
  FROM [...]
) WHERE RW = 1;  

But you should understand what are you looking for. For example, the query above group all the columns except a BLOB column, order them by some two columns and assign a row number to each row in the group. The resulting query retrieves only the first row in each group


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...