SQL server 去掉重复数据

只要数据表“列名”数据相同,则说明是两条重复的数据(ID为数据表的主键自动增长)。

推荐使用方法一

-- 方法一

select * from 表名 A where not exists(select 1 from 表名 where 列名=A.列名 and ID

补充:SQL SERVER 查询去重 PARTITION BY

rownumber() over(partition by col1 order by col2)

去重的方法,很不错,在此记录下:

row_number() OVER ( PARTITION BY COL1 ORDER BY COL2)

表示根据COL1分组,在分组内部根据 COL2排序,而此函数计算的值就表示每组内部排序后的顺序编号(组内连续的唯一的).

直接查询,中间很多相同的,但我只想取AddTime时间最大的一条

  select * from CoreNetApproveRecord where  TableName= 'CoreNetStockInOut' and ApproveState=1

 

 

使用

PARTITION BY fromid ORDER BY creatdate DESC

根据中的 fromid分组,根据creatdate组内排序

WHERE RN= 1;取第一条数据

  select * from (select  ApproveRecordId  ,ApproveTypeId,StepId ,UserId ,Id,ApproveState ,TableName ,AddTime,UpdateTime, ROW_NUMBER() OVER( PARTITION BY Id ORDER BY AddTime DESC)RN FROM CoreNetApproveRecord where TableName= 'CoreNetStockInOut' and ApproveState=1)t where RN= 1

 

 


————————————————
版权声明:本文为CSDN博主「weixin_39667509」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_39667509/article/details/113005451

posted @ 2022-03-30 18:38  闪耀星空  阅读(1058)  评论(0编辑  收藏  举报