Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get only one record for each duplicate rows of the id in oracle?

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.

like image 690
Psychocryo Avatar asked Nov 12 '13 03:11

Psychocryo


2 Answers

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

like image 131
PM 77-1 Avatar answered Nov 12 '22 16:11

PM 77-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
        )
;
like image 1
the_slk Avatar answered Nov 12 '22 16:11

the_slk