How to delete after selection

Hi Experts

I need to make a deletion of a row that was selected within an inner select.

The idea is :
Select (select min(col) from tableA where tableA.ID = tableB.ID
 DELETE FROM tableA
WHERE tableA.ID = tableB.ID

) [colName]
From tableB

An alternate solution would too be highly appreciated

Thanks
devguru001DevAsked:
Who is Participating?
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.

PortletPaulfreelancerCommented:
any chance of using real table and field names?
plus, sample data & expected result?
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Give us a data mockup of what you're trying to pull off here, as looking at your question it's not clear.
0
PortletPaulfreelancerCommented:
Please consider the following carefully, it may show that what you have asked for may result in unwanted data loss

I'm only using one table, but watch what happens to [id] = 1

CREATE TABLE Table1
    ([ID] int, [Value] decimal(6,2));
    
INSERT INTO Table1     ([ID], [Value])
VALUES
    (1, 1.1),
    (2, 2.2),
    (2, 2.3),
    (2, 2.4),
    (3, 3.3),
    (3, 3.4),
    (3, 3.5),
    (3, 3.6)
;

Open in new window


Then:
;with CTE as (
        Select
             B.id
            ,(select min(value)from table1 where table1.ID = B.ID) as [mincol]
        From table1 B
        )
delete from table1
from table1
inner join CTE ON table1.id= cte.id and table1.value = CTE.mincol

Open in new window


Then:
select * from table1

| ID | Value |
|----|-------|
|  2 |   2.3 |
|  2 |   2.4 |
|  3 |   3.4 |
|  3 |   3.5 |
|  3 |   3.6 |

Open in new window


ID 1 has been deleted (because the value is = to min(value)
BUT also
there was only one row with ID=1
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
Vitor MontalvãoMSSQL Senior EngineerCommented:
COL exists in TableB? And is related to TableA.Col?
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.

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.