9

suppose i have this table:

group_id | image | image_id |
-----------------------------
23        blob       1
23        blob       2
23        blob       3
21        blob       4
21        blob       5
25        blob       6
25        blob       7

how to get results of only 1 of each group id? in this case,there may be multiple images for one group id, i just want one result of each group_id

i tried distinct but i will only get group_id. max for image also would not work.

Psychocryo
  • 2,103
  • 8
  • 26
  • 33
  • I don't know oracle, do you have `distinct on (group_id)` ? – some Nov 12 '13 at 03:21
  • Do you care which `blob` will be chosen? I suggest that you post the query that you tried and the expected result based on your data sample, – PM 77-1 Nov 12 '13 at 03:21
  • Do you care which row for each group id you get? – OldProgrammer Nov 12 '13 at 03:21
  • 1
    Found similar question: http://stackoverflow.com/questions/10515391/oracle-equivalent-of-postgres-distinct-on – some Nov 12 '13 at 03:22
  • @some : there's no distinct on for oracle if im not mistaken and the link you gave when i tried it gave me my some error which is inconsistent datatypes expected - got blob – Psychocryo Nov 12 '13 at 03:38
  • @PM77-1 : i dont mind which blob. as long as i get one record for each group id. – Psychocryo Nov 12 '13 at 03:39
  • @OldProgrammer : i dont mind which blob. as long as i get one record for each group id. – Psychocryo Nov 12 '13 at 03:40
  • @Psychocryo `distinct on` is a postgresql extension, and the link I gave you was had an answer of how to do that in oracle. Sorry that it didn't work for you. – some Nov 12 '13 at 03:41
  • Does this answer your question? [Oracle equivalent of Postgres' DISTINCT ON?](https://stackoverflow.com/questions/10515391/oracle-equivalent-of-postgres-distinct-on) – Vadzim May 14 '20 at 20:27

3 Answers3

26

There are no standard aggregate functions in Oracle that would work with BLOBs, so GROUP BY solutions won't work.

Try this one based on ROW_NUMBER() in a sub-query.

SELECT inn.group_id, inn.image, inn.image_id
FROM
(
    SELECT t.group_id, t.image, t.image_id, 
        ROW_NUMBER() OVER (PARTITION BY t.group_id ORDER BY t.image_id) num
    FROM theTable t
) inn
WHERE inn.num = 1;

The above should return the first (based on image_id) row for each group.

SQL Fiddle

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
PM 77-1
  • 12,933
  • 21
  • 68
  • 111
  • i've tried using max but my problem is that it wont work for blob. i get an error like this --> ORA-00932: inconsistent datatypes: expected - got BLOB – Psychocryo Nov 12 '13 at 03:58
  • What is the datatype for image column? – PM 77-1 Nov 12 '13 at 03:59
  • Do you need such query as a report or will it become a part of `INSERT` or `UPDATE`? – PM 77-1 Nov 12 '13 at 04:18
  • the datatype is blob. i will use it to create a view. where it will list only one blob record for one group_id. i just needed one for different purposes. as long as i can get a query to get listing of only one record for each group_id, that will suffice. – Psychocryo Nov 12 '13 at 04:25
  • OK. Working on a different method. Will post the script shortly. – PM 77-1 Nov 12 '13 at 04:30
  • Thanks @PM 77-1 . i got what i wanted and ur solution worked! – Psychocryo Nov 17 '13 at 15:40
1
SELECT  group_id, image, image_id
FROM    a_table
WHERE   (group_id, image_id) IN
        (
            SELECT  group_id, MIN(image_id)
            FROM    a_table
            GROUP   BY
                    group_id
        )
;
the_slk
  • 2,172
  • 1
  • 11
  • 10
0
select * from 
  (select t1.*,
   ROW_NUMBER() OVER (PARTITION BY group_id ORDER BY group_id desc) as seqnum
   from tablename t1) 
where seqnum=1;
Tom
  • 1,179
  • 12
  • 28
  • While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Adrian Mole Apr 02 '20 at 21:51