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语句怎么写
4 个解决方案
#1
---支除重复
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值与'' 是不相等的*/
#2
我是想查出有重复的那一部分,
然后将有重复的那部分删除一条,完全相同的数据只保留一条
有重复的我查出来了,但是不知道怎么去删除,只保留其中的一条
然后将有重复的那部分删除一条,完全相同的数据只保留一条
有重复的我查出来了,但是不知道怎么去删除,只保留其中的一条
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
![sql如何查询出完全相同的数据,并将其中的一条删除 sql如何查询出完全相同的数据,并将其中的一条删除](https://image.shishitao.com:8440/aHR0cHM6Ly93d3cuaXRkYWFuLmNvbS9nby9hSFIwY0hNNkx5OXBiV2N0WW1KekxtTnpaRzR1Ym1WMEwzVndiRzloWkM4eU1ERTBNVEF2TWpZdk1UUXhOREk1TURnek9GOHhPRFEzT1RjdWFuQm4%3D.jpg?w=700&webp=1)
#3
对于这种表中没有主键,可以先将不重复的数据筛选出来插入到临时表中,再删除原表数据,最后将临时表的数据再插入到原表中。
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
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
对于这样的表设计,建议新增主键或惟一索引。
#4
小调一下。
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
#1
---支除重复
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值与'' 是不相等的*/
#2
我是想查出有重复的那一部分,
然后将有重复的那部分删除一条,完全相同的数据只保留一条
有重复的我查出来了,但是不知道怎么去删除,只保留其中的一条
然后将有重复的那部分删除一条,完全相同的数据只保留一条
有重复的我查出来了,但是不知道怎么去删除,只保留其中的一条
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
![sql如何查询出完全相同的数据,并将其中的一条删除 sql如何查询出完全相同的数据,并将其中的一条删除](https://image.shishitao.com:8440/aHR0cHM6Ly93d3cuaXRkYWFuLmNvbS9nby9hSFIwY0hNNkx5OXBiV2N0WW1KekxtTnpaRzR1Ym1WMEwzVndiRzloWkM4eU1ERTBNVEF2TWpZdk1UUXhOREk1TURnek9GOHhPRFEzT1RjdWFuQm4%3D.jpg?w=700&webp=1)
#3
对于这种表中没有主键,可以先将不重复的数据筛选出来插入到临时表中,再删除原表数据,最后将临时表的数据再插入到原表中。
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
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
对于这样的表设计,建议新增主键或惟一索引。
#4
小调一下。
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