My table contains duplicate rows, how to show one?

假装没事ソ 提交于 2019-12-25 07:20:20

问题


Further to my previous question, this table (expanded from the last question) contains duplicate rows

 titel                             | interpret        | jahr
-----------------------------------+------------------+-----
 Beauty                            | Ryuichi Sakamoto | 1990
 Goodbye Country (Hello Nightclub) | Groove Armada    | 2001
 Glee                              | Bran Van 3000    | 1997
 Glee                              | Bran Van 3000    | 1997
 Beauty                            | Ryuichi Sakamoto | 1990
 Goodbye Country (Hello  Nightclub)| Groove Armada    | 2001
 Glee                              | Groove Armada    | 2001  

how would I SELECT to get one example of each set of duplicate rows? (the final row consists entirely of duplicate columns, but is not a duplicate row).

I.E, the result would return

| Beauty                            | Ryuichi Sakamoto | 1990
| Goodbye Country (Hello Nightclub) | Groove Armada    | 2001
| Glee                              | Bran Van 3000    | 1997

回答1:


You would do:

select distinct titel, interpret, jahr from tablename

To get the edited result, you could do something rather interesting like this:

select * 
from table1 
where rowid in 
(
    -- get the first row of each distinct titel
    select min(sr) 
    from 
    (select distinct titel from table1) a 
    inner join 
    (
        -- get first row number for each distinct titel/interpret/jahr
        select min(rowid) as sr, titel, interpret, jahr 
        from table1 
        group by titel, interpret, jahr
    ) b 
        on a.titel = b.titel 
    group by 
        a.titel
);

Results would look like this:

titel                            |interpret       |jahr
Beauty                           |Ryuichi Sakamoto|1990
Goodbye Country (Hello Nightclub)|Groove Armada   |2001
Glee                             |Bran Van 3000   |1997


来源:https://stackoverflow.com/questions/18680809/my-table-contains-duplicate-rows-how-to-show-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!