Home > Database > Mysql Tutorial > 删除sqlserver中的重复记录并按照条件保存其中一条

删除sqlserver中的重复记录并按照条件保存其中一条

WBOY
Release: 2016-06-07 16:37:34
Original
1104 people have browsed it

删除sqlserver中的重复记录并按照条件保存其中一条 实施代码如下: SELECT *FROM [extract].[dbo].[tbTradeFullinfoGet] WHERE tid IN(SELECT tidFROM [extract].[dbo].[tbTradeFullinfoGet]GROUP BY tidHAVING COUNT(tid) 1 ) order by tid descselect dist

删除sqlserver中的重复记录并按照条件保存其中一条
实施代码如下:

SELECT *
FROM [extract].[dbo].[tbTradeFullinfoGet] WHERE tid IN
(SELECT tid
FROM [extract].[dbo].[tbTradeFullinfoGet]
GROUP BY tid
HAVING COUNT(tid) > 1 ) order by tid desc
select distinct tid,*? into #aaa from [extract].[dbo].[tbTradeFullinfoGet]
truncate table [extract].[dbo].[tbTradeFullinfoGet]
insert [extract].[dbo].[tbTradeFullinfoGet] select * from #aaa
delete from [extract].[dbo].[tbTradeFullinfoGet]
where tid in (select tid from [extract].[dbo].[tbTradeFullinfoGet] group by tid having count(tid) > 1)
and intime not in (select max(intime) from [extract].[dbo].[tbTradeFullinfoGet] group by tid having count(tid )>1)
select a.*,
ROW_NUMBER() over (partition by a.tid order by a.intime desc) as rows_id
into?? ##test_a
from?? [extract].[dbo].[tbTradeFullinfoGet] a
delete from ##test_a
where? rows_id'1'
alter table ##test_a drop column rows_id
drop table [extract].[dbo].[tbTradeFullinfoGet]
select *
into?? [extract].[dbo].[tbTradeFullinfoGet]
from?? ##test_a
Copy after login

代码的功能是删除tbTradeFullinfoGet里重复的记录,且只保存表中intime最大的一条记录!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template