如何解决SQL 查询 - 删除重复项
我有一个表格,其中包含重要的以下列:
val fun=(lst:List[Int]):List[Int]=>lst+10
error: identifier expected but integer literal found.
而且我打算删除commentid重复的那几行,也就是没有按照ID编号的顺序。 对于此示例,ID 为 7 和 8 的行将被删除。
解决方法
是否要在重复时返回除最后一条评论 id 之外的所有行?
select t.*
from (select t.*,count(*) over (partition by commentid) as commentid_cnt,max(id) over (partition by commentid) as max_commentid_id,max(id) over () as max_id
from t
) t
where max_id = max_comment_id and commentid_cnt > 1;
编辑:
哦,我想我明白了。您只想保留 commentid
的第一个“分组”。假设 is 是连续的,没有间隙,那么一种方法是:
- 枚举每个
commentid
的行 - 从
id
中减去值 - 如果这大于最小 id 减 1,那么您不在“第一”组中。
这看起来像:
select t.*
from (select t.*,min(id) over (partition by commentid) as min_id,row_number() over (partition by commentid order by id) as seqnum
from t
) t
where id - seqnum = min_id - 1
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。