Postgresql PGSQL在同一列中重复记录
我有一个表,我想知道相同列的重复记录在哪里。这些是我的专栏,我想得到一个记录,在这个记录中,对于相同的代码,fweek和newcode,group_id或week是不同的Postgresql PGSQL在同一列中重复记录,postgresql,duplicates,Postgresql,Duplicates,我有一个表,我想知道相同列的重复记录在哪里。这些是我的专栏,我想得到一个记录,在这个记录中,对于相同的代码,fweek和newcode,group_id或week是不同的 Id newcode fweek code group_id week 1 343001 2016-01 343 100 8 2 343002 2016-01 343 100 8 3 343001 2016-01 343 101
Id newcode fweek code group_id week
1 343001 2016-01 343 100 8
2 343002 2016-01 343 100 8
3 343001 2016-01 343 101 08
所需记录为
Id newcode fweek code group_id week
3 343001 2016-01 343 101 08
为了找到重复的值,我将表与表本身连接起来。 我们需要使用code、fweek和newcode对结果进行分组,以获得多个重复行(如果存在)。我已使用max()获取最后插入的行 您不需要使用is distinct from(对于不等式+NULL也是如此)。如果不想比较空值,请使用运算符 你可以在这里找到更多信息
1) 如果{group_id,week}是相同的,它不也是重复的吗?2)
id
唯一吗?是id唯一
select r.*
from your_table r
where r.id in (select max(r.id)
from your_table r
join your_table r2 on r2.code = r.code and r2.fweek = r.fweek and r2.newcode = r.newcode
where
r2.group_id is distinct from r.group_id or
r2.week is distinct from r.week
group by r.code,
r.fweek,
r.newcode
having count(*) > 1)