sqlserver查询重复数据,删除并保留一条。

sqlserver去除重复数据

  • 多字段查重
  • 查询重复数据
    • 删除重复数据只保留一条

多字段查重

·先存入临时表再进入重复数据查询·

 select  dy.productId,dy.goodsNo,dy.skuid,sc.DouYinStoreNo,sit.goodsqty,sit.placepointid  
  into #temp from lswx.dbo.MedicineInfo_DouYin dy   left join dsl.dbo.Store_inventory_table  sit  
  on dy.goodsNo=sit.DslGoodsId left join dsl.dbo.StoreCheck_Code sc on sc.DslStoreNo=sit.DslStoreId 
  and sc.DouYinStoreNo<>'-' where sit.isChange_DouYin=1 and sc.DouYinStoreNo is not null

SELECT a.* FROM #temp a,
(
SELECT productId,placepointid FROM #temp
GROUP BY placepointid,productId
HAVING COUNT(1)>1
) AS b
WHERE a.productId=b.productId AND a.placepointid=b.placepointid

drop table #temp

查询重复数据

	  select *  from SHSJ0101 where  XSDM in (select XSDM from SHSJ0101 where SHSJPCDM = 'b1f492d67da743dc838e214195a2a524'  group by XSDM having count(XSDM) > 1) and SQSJ not in(select  max(SQSJ) from SHSJ0101 where SHSJPCDM = 'b1f492d67da743dc838e214195a2a524' group by  XSDM having count(XSDM) > 1 )and SHSJPCDM = 'b1f492d67da743dc838e214195a2a524'  

删除重复数据只保留一条

delete from SHSJ0101 where  XSDM in (select XSDM from SHSJ0101 where SHSJPCDM = 'b1f492d67da743dc838e214195a2a524'  group by XSDM having count(XSDM) > 1) and SHSJPCDM = 'b1f492d67da743dc838e214195a2a524'  and SQSJ not in(select  max(SQSJ) from SHSJ0101 where SHSJPCDM = 'b1f492d67da743dc838e214195a2a524' group by  XSDM having count(XSDM) > 1 )  

你可能感兴趣的:(sqlserver,sql,sqlserver)