从表和引用的表microsoft sql server中删除重复的行

时间:2017-04-11 09:48:52

标签: sql sql-server database tsql duplicates

我有桌子人员:

PersonId     | FirstName   | LastName |
1            | 'John'      | 'Doe'    |
2            | 'Mike'      | 'Test'   |
3            | 'John'      | 'Doe'    |
4            | 'Mike'      | 'Test'   |
5            | 'John'      | 'Doe'    |
6            | 'John'      | 'Doe'    |

表客户:

CustomerId      | PersonId    |
1001            | 1           | 
1002            | 2           |
1003            | 3           |
1004            | 4           |
1005            | 5           |
1006            | 6           |

我想删除客户1003,1004,1005,1006,因为他们的人员是重复的,但是PersonId不相同。

这应检查Person表中的FirstName和LastName并删除Customer表中的重复项,然后删除Person表中的重复项。 (3,4,5,6)

很抱歉,如果以前曾提出类似的问题,但我无法做到这一点。

5 个答案:

答案 0 :(得分:1)

检查这个。

我们先从Customer表中删除。首先,我们通过使用Row_number()并删除必须排名超过1的personid来找到重复记录。

查询下方显示重复记录:

/usr/bin/ld: cannot find -lContinuumTransfunctioner

找到Duplicates后,我们将其从customer表中删除,然后将其删除。

    select ROW_NUMBER () over ( partition by firstname,lastname order by 
    PersonId ) RID, PersonId,FirstName,LastName 
    from #Person  

答案 1 :(得分:0)

使用此查询查看重复项:

 with duplicatecte(personid,rownum)As
 (
    select personid ,
           row_Number() over(partition by FirstName+LastName order by personid) 
    from #person
   )
   select b.personid,customerid from duplicatecte a
   inner join #customer b on a.personid=b.personid where rownum>1

将此查询修改为删除,如下所示

 with duplicatecte(personid,rownum)As
 (
  select personid ,
        row_Number() over(partition by FirstName+LastName order by personid) 
  from #person
  )
 delete b
 from duplicatecte a
 inner join #customer b on a.personid=b.personid where rownum>1

答案 2 :(得分:0)

private void TextBox_KeyDown(object sender, KeyRoutedEventArgs e)
        {
            TextBox tb = (TextBox)sender;

            InputScope scope = new InputScope();
            InputScopeName name = new InputScopeName();
            name.NameValue = InputScopeNameValue.Number;
            scope.Names.Add(name);
            string textBoxName = tb.Name;
          //  TextBox text = new TextBox();

            //this line is not working for me
           // textBoxName.InputScope = scope;
            var state = CoreWindow.GetForCurrentThread().GetKeyState(VirtualKey.Shift);
            var newState = (state & CoreVirtualKeyStates.Down) == CoreVirtualKeyStates.Down;


            if ((!char.IsControl(Convert.ToChar(e.Key)) && !char.IsDigit(Convert.ToChar(e.Key)) && (Convert.ToChar(e.Key) != '.') && !newState))
            {
                e.Handled = true;
            }

        }

答案 3 :(得分:0)

declare @tbl table 
(pid int
)
;with cte
as
(
select t1.*,row_number() over (partition by firstname,lastname order by personid) as rownum
from
person t1
)

delete 
from
 cte
 output deleted.personid  into @tbl where rownum>1


 delete from customer where personid in (select personid from @tbl)

答案 4 :(得分:0)

这对你有用:

Declare @person As table
(
 PersonId int,
 FirstName varchar(25),
 LastName varchar(25)
)

Declare @customer As table
(
 CustomerId int,
 PersonId int
)


Insert Into @person (PersonId,FirstName,LastName) values(1,'John','Doe')
Insert Into @person (PersonId,FirstName,LastName) values(2,'Mike','Test')
Insert Into @person (PersonId,FirstName,LastName) values(3,'John','Doe')
Insert Into @person (PersonId,FirstName,LastName) values(4,'Mike','Test')
Insert Into @person (PersonId,FirstName,LastName) values(5,'John','Doe')
Insert Into @person (PersonId,FirstName,LastName) values(6,'John','Doe')
Insert Into @customer(CustomerId,PersonId) values(1001,1)
Insert Into @customer(CustomerId,PersonId) values(1002,2)
Insert Into @customer(CustomerId,PersonId) values(1003,3)
Insert Into @customer(CustomerId,PersonId) values(1004,4)
Insert Into @customer(CustomerId,PersonId) values(1005,5)
Insert Into @customer(CustomerId,PersonId) values(1006,6)


select p.PersonId into #temp from @person p right join 
(Select PersonId,FirstName,LastName, ROW_NUMBER() over (partition by FirstName,LastName Order by PersonId) rownumber 
from @person ) a
on p.PersonId=a.PersonId where a.rownumber>1

delete from @customer where PersonId in (select PersonId from #temp)
delete from @person where PersonId in (select PersonId from #temp)


select *from @customer
select *from @person