zoukankan      html  css  js  c++  java
  • sql删除重复数据,保留一条

    原文参考:https://www.cnblogs.com/tracer-dhy/p/10664327.html

    按stIdCard,stYear,semester划分,删除每个学生每学年的重复数据,并保留rowid最小的一条记录(也可以是其他条件的一条记录)

    delete
    from a_h_mid b
    where b.stu_id in
                    (select a.stu_id from
                        (select stName, stu_id,rowid, stIdCard,stYear,semester
                     from a_h_mid
                     where (stIdCard,stYear,semester) in
                           (select stIdCard,stYear,semester
                             from a_h_mid
                             group by stIdCard,stYear,semester
                             having count(*) > 1)
                      and rowid not in
                           (select min(rowid)
                            from ( a_h_mid  )
                            group by stIdCard,stYear,semester
                            having count(*) > 1) ) a )

    一、a_h_mid为源数据表,记录的是学生每学期的成绩,但个学期可能会异常产生多条数据,比如一学期有两条语文成绩,所以需要去重操作。

    二、按学生证件号stIdCard、学年stYear、学期semester三个字段为依据,查看是否有重复记录。

    select stIdCard,stYear,semester
            from a_h_mid
            group by stIdCard,stYear,semester
            having count(*) > 1

    三、保留想要的数据,这里是随便保留了一个rowid最小的记录,可以保留分数最大的或者其他的

    select min(rowid)
           from ( a_h_mid  )
           group by stIdCard,stYear,semester
           having count(*) > 1

    四、删掉重复的保留最小rowid的记录

    delete
    from a_h_mid b
    where b.stu_id in
                    (select a.stu_id from
                        (select stName, stu_id,rowid, stIdCard,stYear,semester
                     from a_h_mid
                     where (stIdCard,stYear,semester) in
                           (select stIdCard,stYear,semester
                             from a_h_mid
                             group by stIdCard,stYear,semester
                             having count(*) > 1)
                      and rowid not in
                           (select min(rowid)
                            from ( a_h_mid  )
                            group by stIdCard,stYear,semester
                            having count(*) > 1) ) a ) 
  • 相关阅读:
    集成学习
    集成学习
    集成学习
    集成学习-Majority Voting
    pandas 之 groupby 聚合函数
    LDA-作为线性判别 降维 推导
    LDA-作为线性判别 分类器 推导
    ML-逻辑回归推导
    第一册:lesson eighty one.
    第一册:lesson seventy nine.
  • 原文地址:https://www.cnblogs.com/lhc-hhh/p/13256186.html
Copyright © 2011-2022 走看看