0

我上一个问题之后,此表(从上一个问题扩展)包含重复的行

 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  

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

IE,结果会返回

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

1 回答 1

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
于 2013-09-08T05:47:01.787 回答