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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
awking00Information Technology SpecialistCommented:
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
awking00Information Technology SpecialistCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.