Nimble Fungus Nimble Fungus - 1 year ago 58
SQL Question

Delete data in same column

I am not sure how to explain my problem so i am showing the sample data.

I have the following table

|RecID |TypeOfData |Data |
|21 |Label |Name |
|21 |Data |Sam |
|22 |Label |Name |
|23 |Label |Name |
|23 |Data |Nimble |

I want to delete all those Label records which do not have Data Below them.
So in above example the RecID=22 needs to be deleted.

Kindly help me with this.

Db is SQL Server 2008.

Answer Source

could be you can use a subselect where the count of Rec_id is =1

  delete from my_table 
  where Rec_id in (select RecID from my_table 
                   group by RecId 
                   having count(*) =1)
 and TypeOdData = 'Label'