我的表包含重复的行,如何显示一行?

时间:2013-09-08 05:44:27

标签: sql sqlite duplicates

除了my previous question之外,此表(从上一个问题扩展而来)包含重复的行

 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  

我将如何选择每个重复行的一个示例? (最后一行完全由重复列组成,但不是重复行。)

I.E,结果将返回

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

1 个答案:

答案 0 :(得分:1)

你会这样做:

select distinct titel, interpret, jahr from tablename

要获得编辑结果,你可以做一些有趣的事情:

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
);

结果如下:

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