2017-07-28 56 views
2
USE TEMPDB 
go 

IF OBJECT_ID(N'#TEST', N'U') IS NOT NULL 
    DROP TABLE dbo.#TEST 


CREATE TABLE #TEST 
(NAME VARCHAR(50), 
line int, 
RANKS INT) 
INSERT INTO #TEST 
(name, line,RANKS) 
VALUES 
('Tom',1, 1), --keep 
('Tom',2, 1),--keep 
('Toms',1, 0), --keep 
('Toms',2, 0),--keep 
('Dave',1, 0),--delete 
('Dave',2, 0),--keep 
('Dave',1, 1),--keep 
('TIm',1,1),--keep 
('TIm',1,0),--delete 
('Matt',1,0),--delete 
('Matt',1,1)--keep 
  • 如果相同的名稱和線下不同的等級下降,那麼我需要刪除ONDE下等級「0」,如果屬於同一個人用同一行屬於不同的行列,則應刪除行列0以下的行。

回答

1

像這樣的東西應該工作:

delete t1 
    from #TEST t1 
where exists(select * 
       from #TEST t2 
       where t2.NAME = t1.NAME 
       and t2.line = t1.line 
       and t2.RANKS <> t1.RANKS) 
    and t1.RANKS = 0; 
+0

完美,它的工作。萬分感謝。 – epistemophilic

相關問題