SQL Select to remove rows

I need SQL Select to remove some records when the same value is in two columns.  From the data below, I just want to remove the records where the same value in column Level 1 ID is in column Level 2 ID.

Level 1 ID               Level 2 ID
webfolder2-1        webfolder2-257
webfolder2-1              webfolder2-257
webfolder2-1        webfolder2-258
webfolder2-1              webfolder2-261
webfolder2-257      webfolder2-1121
webfolder2-257      webfolder2-1122
webfolder2-1121      
webfolder2-1122      
webfolder2-258      webfolder2-1117
webfolder2-1117      

This what it should look like:
Level 1 ID              Level 2 ID
webfolder2-1              webfolder2-257
webfolder2-1        webfolder2-257
webfolder2-1        webfolder2-258
webfolder2-1       webfolder2-261
webfolder2-1121      
webfolder2-1122      
webfolder2-1117
tmajor99Asked:
Who is Participating?
 
Anoo S PillaiCommented:
A logic similar to the following may work -

SELECT Level1,Level2
FROM   Test tab1
WHERE NOT EXISTS ( SELECT level2
                                      FROM test tab2
                                      WHERE tab1.level1 = tab2.Level2)
OR tab1.level2 = ''
OR tab1.level2 IS NULL
0
 
Mike EghtebasDatabase and Application DeveloperCommented:
re:>  I just want to remove the records where the same value in column Level 1 ID is in column Level 2 ID

webfolder2-1	.webfolder2-257
webfolder2-1	webfolder2-257
webfolder2-1	webfolder2-258
webfolder2-1	webfolder2-261
*webfolder2-257	webfolder2-1121
*webfolder2-257	webfolder2-1122
*webfolder2-1121	NULL
*webfolder2-1122	NULL
*webfolder2-258	webfolder2-1117
*webfolder2-1117	NULL

Open in new window


The 6 folders names starting * exit in the second column. Is changing these values to null acceptable as shown below:
webfolder2-1	.webfolder2-257
webfolder2-1	webfolder2-257
webfolder2-1	webfolder2-258
webfolder2-1	webfolder2-261
NULL	webfolder2-1121
NULL	webfolder2-1122
NULL	NULL
NULL	NULL
NULL	webfolder2-1117
NULL	NULL

Open in new window


If so, then try:
UPDATE  #t SET [Level 1 ID] = NULL
WHERE [Level 1 ID] IN (SELECT [Level 2 ID] From #t) 

-- temp table #t:
create table #t([Level 1 ID] varchar(30), [Level 2 ID] varchar(30));
--delete From #t;
insert #t values
('webfolder2-1',        'webfolder2-257'),
('webfolder2-1',        'webfolder2-257'),
('webfolder2-1',        'webfolder2-258'),
('webfolder2-1',        'webfolder2-261'),
('webfolder2-257',        'webfolder2-1121'),
('webfolder2-257',        'webfolder2-1122'),
('webfolder2-1121',  null),
('webfolder2-1122',null),      
('webfolder2-258',        'webfolder2-1117'),
('webfolder2-1117', null)  

Open in new window


Mike
0
 
awking00Commented:
You're removing the both rows with values of webfolder2-257 in Level 1 ID. Is that because there are also two rows with that value in Level 2 ID? Would that be the same if level 1 had one record and level 2 had two records or vice versa? Perhaps you can provide a little more description of the criteria you are using to remove rows.
0
 
awking00Commented:
If you want to remove all of the rows where the value in Level1ID is in Level2ID, then
delete from table where Level1ID in
(select Level1ID from table
 intersect
 select Level2ID from table)
You may want to replace the delete with select * first to see you are getting the correct rows to remove.
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
tmajor99, do you still need help with this question?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.