将记录合并到一个记录中时删除重复项的最佳方法是什么?
我有一种情况,表会跟踪玩家姓名及其记录,如下所示:
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
除此之外不能正常工作。我似乎仍然无法删除包含重复名称的其他重复行。我能做什么?有什么建议吗?
答案 0 :(得分:7)
这是您的更新:
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特有的,并且从this answer和this answer无耻地被盗。)
您可能还希望运行此命令以使所有名称都包含在内:
UPDATE STATS SET nick = lower(nick);
Aaaand在'nick'的小写版本上抛出一个唯一索引(或者向该列添加一个约束以禁止非小写值):
CREATE UNIQUE INDEX ON stats (LOWER(nick));
答案 1 :(得分: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)
答案 2 :(得分:3)
我认为在一个查询中最简单的方法是使用common table expressions:
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中我删除重复项并返回已删除的行,然后将已分组的已删除数据插回到表中。
答案 3 :(得分: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)
应该有效。