SQL SERVER 将数据库中重复数据删除且只保留一条
来源:互联网 发布:linux怎么上传压缩包 编辑:程序博客网 时间:2024/06/06 01:08
-- 设置可唯一识别的字段(此处是lmt)update update_t_datameas set lmt = t.lmtfrom update_t_datameas s,(SELECT stcd, POINTID, dt, v1, v2, v3, r1, r2, r3, DATEADD(second, ABS(CHECKSUM(NEWID())) % DATEDIFF(second,'00:00','23:59'), CONVERT(char(20),dt,20) ) as lmtfrom update_t_datameas s) twhere s.stcd = t.stcd and s.POINTID = t.POINTID and s.dt = t.dt-- 设置保留lmt时间最大的一条数据DELETE from update_t_datameas where stcd in (SELECT stcd from update_t_datameas GROUP BY stcd, POINTID, dt HAVING count(*)>1)and dt in (SELECT dt from update_t_datameas GROUP BY stcd, POINTID, dt HAVING count(*)>1)and POINTID in (SELECT POINTID from update_t_datameas GROUP BY stcd, POINTID, dt HAVING count(*)>1)and lmt not in (SELECT max(lmt) from update_t_datameas GROUP BY stcd, POINTID, dt HAVING count(*)>1)
阅读全文