从 table 中删除用户记录很少的行
Delete rows from table with few user records
有这个table:
id | lat | lon | timestamp
---------+------------+------------+------------
5590761 | 41.0106651 | -8.4618407 | 1491071642
5590761 | 41.0088282 | -8.4613129 | 1491071670
5590761 | 41.0081011 | -8.4626845 | 1491071763
5590761 | 41.008415 | -8.4621233 | 1491071767
5590761 | 41.0084717 | -8.46211 | 1491071768
5590761 | 41.008525 | -8.4620333 | 1491071769
5590761 | 41.00859 | -8.4619783 | 1491071770
5590761 | 41.008635 | -8.461875 | 1491071771
5590761 | 41.0087083 | -8.4616717 | 149107177
有些记录的实例很少。我想删除唯一 id
计数小于 10 的所有记录。
您可以在 delete
中使用子查询:
delete from t
where t.id in (select t2.id
from t t2
group by t2.id
having count(*) < 10
);
如果只想删除 select
查询中的行,可以使用 window 函数:
select t.*
from (select t.*, count(*) over (partition by id) as cnt
from t
) t
where cnt < 10
有这个table:
id | lat | lon | timestamp
---------+------------+------------+------------
5590761 | 41.0106651 | -8.4618407 | 1491071642
5590761 | 41.0088282 | -8.4613129 | 1491071670
5590761 | 41.0081011 | -8.4626845 | 1491071763
5590761 | 41.008415 | -8.4621233 | 1491071767
5590761 | 41.0084717 | -8.46211 | 1491071768
5590761 | 41.008525 | -8.4620333 | 1491071769
5590761 | 41.00859 | -8.4619783 | 1491071770
5590761 | 41.008635 | -8.461875 | 1491071771
5590761 | 41.0087083 | -8.4616717 | 149107177
有些记录的实例很少。我想删除唯一 id
计数小于 10 的所有记录。
您可以在 delete
中使用子查询:
delete from t
where t.id in (select t2.id
from t t2
group by t2.id
having count(*) < 10
);
如果只想删除 select
查询中的行,可以使用 window 函数:
select t.*
from (select t.*, count(*) over (partition by id) as cnt
from t
) t
where cnt < 10