PGSQL在同一列中重复记录

时间:2017-03-09 06:14:26

标签: postgresql duplicates

我有一张表,我想知道相同列的重复记录在哪里。这些是我的专栏,我想得到记录,其中group_id或周对于相同的代码和fweek和newcode不同

return py;

所需记录是

    .globl  decod
    .def    decod;  .scl    2;  .type   32; .endef
    .seh_proc   decod
decod:
    .seh_endprologue
    subl    %r8d, %edx
    movl    %edx, %eax
    imull   %edx, %ecx
    sall    $31, %eax
    sarl    $31, %eax
    xorl    %ecx, %eax
    ret
    .seh_endproc

1 个答案:

答案 0 :(得分:0)

要查找重复的值,我已将自己加入表中。 我们需要将结果与代码,fweek和newcode分组,以获得多个重复行(如果存在)。我用max()来获取最后一行。

你不需要使用是不同的(对于不等式+ NULL,它是相同的)。如果您不想比较NULL,请使用<>操作

您可以在此处找到有关此处info

的更多信息
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)