10

在将记录合并为一个时删除重复项的最佳方法是什么?

我有一种情况,表格会跟踪玩家姓名和他们的记录,如下所示:

stats
-------------------------------
nick     totalgames     wins   ...
John     100            40
john     200            97
Whistle  50             47
wHiStLe  75             72
...

我需要合并 nick 重复的行(忽略大小写时)并将记录合并为一个,如下所示:

    stats
    -------------------------------
    nick     totalgames     wins   ...
    john     300            137
    whistle  125            119
    ...

我在 Postgres 中这样做。最好的方法是什么?

我知道我可以通过这样做来获取存在重复项的名称:

select lower(nick) as nick, totalgames, count(*) 
from stats 
group by lower(nick), totalgames
having count(*) > 1;

我想到了这样的事情:

update stats
set totalgames = totalgames + s.totalgames
from (that query up there) s
where lower(nick) = s.nick

除非这不能正常工作。而且我似乎仍然无法删除包含重复名称的其他重复行。我能做些什么?有什么建议么?

4

4 回答 4

10

SQL小提琴

这是您的更新:

 UPDATE stats
 SET totalgames = x.games, wins = x.wins
 FROM (SELECT LOWER(nick) AS nick, SUM(totalgames) AS games, SUM(wins) AS wins
     FROM stats
      GROUP BY LOWER(nick) ) AS x
 WHERE LOWER(stats.nick) = x.nick;

这是删除重复行的删除:

 DELETE FROM stats USING stats s2
 WHERE lower(stats.nick) = lower(s2.nick) AND stats.nick < s2.nick;

(请注意,'update...from' 和 'delete...using' 语法是 Postgres 特有的,并且从这个答案这个答案中被无耻地窃取了。)

您可能还想运行它来小写所有名称:

 UPDATE STATS SET nick = lower(nick);

Aaaand 在小写版本的 'nick' 上添加唯一索引(或向该列添加约束以禁止非小写值):

CREATE UNIQUE INDEX ON stats (LOWER(nick)); 
于 2013-08-17T05:30:51.607 回答
4

我认为在一个查询中执行此操作的最简单方法是使用公用表表达式

with cte as (
    delete from stats
    where lower(nick) in (
      select lower(nick) from stats group by lower(nick) having count(*) > 1
    )
    returning *
)
insert into stats(nick, totalgames, wins)
select lower(nick), sum(totalgames), sum(wins)
from cte
group by lower(nick);

如您所见,在 cte 内部,我正在删除重复项并返回已删除的行,然后将分组的已删除数据插入回表中。

sql fiddle demo

于 2013-08-17T17:04:11.757 回答
3

这一切都可以在一个语句中完成,使用RETURNING.

-- The data
CREATE TABLE stats
        ( nick VARCHAR PRIMARY KEY
        , totalgames INTEGER NOT NULL DEFAULT 0
        , wins INTEGER NOT NULL DEFAULT 0
        );

INSERT INTO stats(nick, totalgames,wins) VALUES
 ( 'John', 100, 40) ,( 'john', 200, 97)
,( 'Whistle', 50, 47) ,( 'wHiStLe', 75, 72)
, ( 'Single', 42, 13 ) -- this person has only one record
        ;
SELECT * FROM stats;

-- The query:
WITH upd AS (
        UPDATE stats dst
        SET totalgames = src.totalgames
                , wins = src.wins
        FROM ( SELECT MIN(nick) AS nick -- pick the "lowest" nick as the canonical nick
                , SUM(totalgames) AS totalgames
                , SUM(wins) AS wins
                FROM stats
                GROUP BY lower(nick)
                ) src
        WHERE dst.nick = src.nick
        RETURNING dst.nick -- only the records that have been updated
        )
-- Delete the records that were NOT updated.
DELETE FROM stats del
WHERE NOT EXISTS (
        SELECT * FROM upd
        WHERE upd.nick = del.nick
        )
        ;

SELECT * FROM stats;

输出:

INSERT 0 5
  nick   | totalgames | wins 
---------+------------+------
 John    |        100 |   40
 john    |        200 |   97
 Whistle |         50 |   47
 wHiStLe |         75 |   72
 Single  |         42 |   13
(5 rows)

DELETE 2
  nick   | totalgames | wins 
---------+------------+------
 wHiStLe |        125 |  119
 john    |        300 |  137
 Single  |         42 |   13
(3 rows)
于 2013-08-17T15:25:54.297 回答
0

UPDATE stats SET totalgames=s.totalgames, wins=s.wins
FROM (SELECT lower(nick) AS nick,SUM(totalgames) AS totalgames,SUM(wins) AS wins FROM stats GROUP BY lower(nick))s WHERE lower(nick)=s.nick;
DELETE FROM stats WHERE
lower(nick) IN (SELECT lower(nick) FROM stats GROUP BY lower(nick) HAVING COUNT(*)>1)
AND NOT lower(nick) IN (SELECT first(nick) FROM stats GROUP BY lower(nick)应该管用。

于 2013-08-17T05:34:58.820 回答