sql删除重复数据(多个KEY值)

sql删除重复数据(多个KEY值)

sql删除重复数据(多个KEY值)

// 创建一个临时表
select key1,key2 INTO #aaa from 表名 group by key1,key2 having count(*) > 1
//执行删除
DELETE FROM 表名 WHERE key1 IN(SELECT key1 FROM #aaa) 
 AND key2 IN(SELECT key2 FROM #aaa) 
 AND INDX NOT IN(SELECT  MIN(INDX) from 表名 group by key1,key2 having count(*) > 1)
--集报出口去重复
select STOCKOUT_NO,STOCKOUT_LINE_NO,TRAN_TYPE_CODE,RESERVER5 INTO #aaa from BMS_PROCESS_OUT group by STOCKOUT_NO,STOCKOUT_LINE_NO,TRAN_TYPE_CODE,RESERVER5 having count(*) > 1

SELECT *  FROM BMS_PROCESS_OUT WHERE STOCKOUT_NO IN(SELECT STOCKOUT_NO FROM #aaa) 
 AND STOCKOUT_LINE_NO IN(SELECT STOCKOUT_LINE_NO FROM #aaa)
 AND TRAN_TYPE_CODE IN(SELECT TRAN_TYPE_CODE FROM #aaa)
 AND RESERVER5 IN(SELECT RESERVER5 FROM #aaa)
 AND INDX NOT IN(SELECT  MIN(INDX) from BMS_PROCESS_OUT group by STOCKOUT_NO,STOCKOUT_LINE_NO,TRAN_TYPE_CODE,RESERVER5 having count(*) > 1)

查询重复数据
SELECT H_INDX,
                 GDSSEQNO,
               COUNT(1) AS num
        FROM dbo.BSWL_NEMS_LIST
        GROUP BY H_INDX,
                 GDSSEQNO
        HAVING COUNT(1) > 1

SELECT L.*
FROM dbo.BSWL_NEMS_LIST L
    INNER JOIN
    (
        SELECT H_INDX,
                 GDSSEQNO,
               COUNT(1) AS num
        FROM dbo.BSWL_NEMS_LIST
        GROUP BY H_INDX,
                 GDSSEQNO
        HAVING COUNT(1) > 1
    ) T
        ON L.H_INDX = T.H_INDX
           AND L.GDSSEQNO = T.GDSSEQNO
     ORDER BY L.H_INDX,L.GDSSEQNO
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值