Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.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
如何使用psql中的row_number()从表中删除重复项?_Sql_Duplicates_Sql Delete_Greenplum - Fatal编程技术网

如何使用psql中的row_number()从表中删除重复项?

如何使用psql中的row_number()从表中删除重复项?,sql,duplicates,sql-delete,greenplum,Sql,Duplicates,Sql Delete,Greenplum,我已经编写了以下查询,我正在使用greenplum db和dbeaver实现 with cte as (select *, row_number() over(partition by first_name order by roll_num) row_num from table_name where roll_num in ('0011')) delete from cte where row_num>1; 上面的查询返回错误。有人能帮我吗 请像这样尝试它对我有效: s

我已经编写了以下查询,我正在使用greenplum db和dbeaver实现

with cte as 
(select 
*, 
row_number() over(partition by first_name order by roll_num) row_num 
from table_name 
where roll_num in ('0011')) 
delete from cte where row_num>1;


上面的查询返回错误。有人能帮我吗

请像这样尝试它对我有效:

select *  from tablename as t 
where exists 
      ( select * 
        from tablename as d 
        where d.ctid > t.ctid 
          and d.* is not distinct from t.*
      ) ;

delete from tablename as t 
    where exists 
          ( select * 
            from tablename as d 
            where d.ctid > t.ctid 
              and d.* is not distinct from t.*
          ) ;
这个怎么样:


Ref:

如果您将滚动数值限制为仅单个值
0011
,那么我看不出像您这样使用
行编号的意义。你能解释一下你的逻辑吗?以roll_num作为测试用例,这里的实际属性是一个字符串。一个问题,我们可以使用where inside with,然后像上面那样执行删除查询吗?@TimBiegeleisen即使删除delete子句,查询仍然返回error这是否回答了你的问题?我已经厌倦了,问题是我有一些字段有空值,它们在表中有最小或最大ctid。因此,在删除重复项之后,仍然会保留一些字段为null的行,但我确实希望这样。这就是为什么我想使用row_number(),因为非空字段总是得到min row,并且没有numm值的行保留在endBro。在您的示例中,您有唯一的字段id,但是我没有这样的字段。如果是这样,那么只需在多个字段上连接cte_子集(例如first_name和roll_num)在最后的where子句中?cte_子集必须包含这些附加字段。