sql如何查询出完全相同的数据,并将其中的一条删除

luoyuewuheng 2014-10-26 09:06:26
这有一张表 table1 表中数据如下

f_period f_wh_no f_ord_no f_item_no f_col_ no
2014 018 2 b b
2014 018 1 2 3
2014 018 1 2 3
2014 018 1 2 a
2014 018 1 2 a



图片中是我加条件的查出来的几条数据,里面有完全相同的两条数据,也有不重复的,
查出有重复的sql语句怎么写
删除重复的其中一条的sql语句怎么写

...全文
627 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
reenjie 2014-10-26
  • 打赏
  • 举报
回复
引用 3 楼 reenjie 的回复:
对于这种表中没有主键,可以先将不重复的数据筛选出来插入到临时表中,再删除原表数据,最后将临时表的数据再插入到原表中。 1) select * into #temp from ( select f_period ,f_wh_no,f_ord_no,f_item_no,f_col_no,f_pas_qtyl,row_number() over(partition by f_period ,f_wh_no,f_ord_no,f_item_no,f_col_no,f_pas_qtyl order by getdate()) as rId from table1 ) as a 2) delete from table1 3) insert table1(f_period ,f_wh_no,f_ord_no,f_item_no,f_col_no,f_pas_qtyl) select f_period ,f_wh_no,f_ord_no,f_item_no,f_col_no,f_pas_qtyl from #temp 对于这样的表设计,建议新增主键或惟一索引。
小调一下。 1) select * into #temp from ( select f_period ,f_wh_no,f_ord_no,f_item_no,f_col_no,f_pas_qtyl,row_number() over(partition by f_period ,f_wh_no,f_ord_no,f_item_no,f_col_no,f_pas_qtyl order by getdate()) as rId from table1 ) as a where rId=1 2) delete from table1 3) insert table1(f_period ,f_wh_no,f_ord_no,f_item_no,f_col_no,f_pas_qtyl) select f_period ,f_wh_no,f_ord_no,f_item_no,f_col_no,f_pas_qtyl from #temp
reenjie 2014-10-26
  • 打赏
  • 举报
回复
对于这种表中没有主键,可以先将不重复的数据筛选出来插入到临时表中,再删除原表数据,最后将临时表的数据再插入到原表中。 1) select * into #temp from ( select f_period ,f_wh_no,f_ord_no,f_item_no,f_col_no,f_pas_qtyl,row_number() over(partition by f_period ,f_wh_no,f_ord_no,f_item_no,f_col_no,f_pas_qtyl order by getdate()) as rId from table1 ) as a 2) delete from table1 3) insert table1(f_period ,f_wh_no,f_ord_no,f_item_no,f_col_no,f_pas_qtyl) select f_period ,f_wh_no,f_ord_no,f_item_no,f_col_no,f_pas_qtyl from #temp 对于这样的表设计,建议新增主键或惟一索引。
luoyuewuheng 2014-10-26
  • 打赏
  • 举报
回复
我是想查出有重复的那一部分,
然后将有重复的那部分删除一条,完全相同的数据只保留一条

有重复的我查出来了,但是不知道怎么去删除,只保留其中的一条

select f_period ,f_wh_no,f_ord_no,f_item_no,f_col_no,f_pas_qtyl from table1
group by f_period ,f_wh_no,f_ord_no,f_item_no,f_col_no,f_pas_qtyl having count(1)>1


习惯性蹭分 2014-10-26
  • 打赏
  • 举报
回复

---支除重复
SELECT DISTINCT f_period,f_wh_no,f_ord_no,f_item_no,f_col_no
FROM table1
---查询有重复的,每组只会出现一条,查询全部用row_number()
SELECT   f_period,f_wh_no,f_ord_no,f_item_no,f_col_no
FROM table1
GROUP BY f_period,f_wh_no,f_ord_no,f_item_no,f_col_no
HAVING COUNT(1)>1
/*注意null值与'' 是不相等的*/

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧