[sql查询] 重复数据只取一条
SELECT * FROM tab_init WHERE id IN ( --根据Data分类获取数据最大ID列表 select max(id) from tab_init group by a,b )
先找出重复数据的 最大的Id ( group by 后面可跟多列, 根据规则找到重复数据), 取出 id 最大或最小的 Id, 然后使用 in查询,就可以得到不重复的数据
配合 With .. As () 非常方便
--tab_jcxh_CertificateInfoAbility_New导入最新数据 go with tab_init as ( select ROW_NUMBER() over (order by OperateTime) as Id,* from [Person].tab_jcxh_CertificateInfoAbility ) insert into [Person].tab_jcxh_CertificateInfoAbility_New(IndexId,CertificateNumber,ItemCode,ItemName,CertificateDeadline,zzType,IsOperate,OperateTime) SELECT IndexId,CertificateNumber,ItemCode,ItemName,CertificateDeadline,zzType,IsOperate,OperateTime FROM tab_init WHERE id IN ( --根据Data分类获取数据最大ID列表 select max(id) from tab_init group by CertificateNumber )
本文来自博客园,作者:兴想事成,转载请注明原文链接:https://www.cnblogs.com/mjxxsc/p/5622666.html