Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql PGSQL在同一列中重复记录_Postgresql_Duplicates - Fatal编程技术网

Postgresql PGSQL在同一列中重复记录

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

我有一个表,我想知道相同列的重复记录在哪里。这些是我的专栏,我想得到一个记录,在这个记录中,对于相同的代码,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        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)