Go Premium for a chance to win a PS4. Enter to Win

x
Solved

# Delete unwanted rows

Posted on 2013-06-15
Medium Priority
241 Views
I have a table "TreatmentData" defined as:

create table #TreatmentData(

PatientId nvarchar(18),
TreatedWith nvarchar(30) );

The table lists patient treatments.  A patient can have many rows in the table.  There are six possible values for TreatedWith.

I need to delete all the patient records where the patient has "NoTreatment" for all his/her rows in the database.  i.e. they have visited the clinic but not been treated.

I imagine it must be where the number of rows is equal to the count where the TreatedWith is "NoTreatment" when grouped by PatientId...

but i just can not get my head round it.

Any suggestions?
0
Question by:soozh

LVL 16

Assisted Solution

Surendra Nath earned 1000 total points
ID: 39250522

``````;with CTE as
(
select PatientId,TreatedWith  from #TreatmentData group by PatientId,TreatedWith
), CTE1 AS
(
select PatiendID from CTE C WHERE NOT EXISTS ( select 1 FROM CTE C1 where C1.PatiendID = C.PatiendID and C1.TreatedWith  <> 'NoTreatment')
)
DELETE T
FROM #TreatmentData T
JOIN CTE1 C
ON T.PatiendID = C.PatiendID
``````
0

LVL 5

Expert Comment

ID: 39250869
Essentially what we are saying here is that delete the patient record if we find only 'NoTreatment' records for that patient:
``````Delete from TreatmentData a
where exists
(Select PatientId from TreatmentData b
where b.PatientId = a.PatientId
and b.TreatedWith = 'NoTreatment')
and NOT exists
(Select PatientId from TreatmentData b
where b.PatientId = a.PatientId
and b.TreatedWith <> 'NoTreatment')
``````
0

LVL 32

Accepted Solution

Ephraim Wangoya earned 1000 total points
ID: 39251119
Here

``````with cte as
(
select *
from TreatmentData
where TreatedWith <> 'NoTreatment'
)

delete from TreatMentData
where not exists(select 1
from cte A
where TreatmentData.PatientID = A.PatientID)
``````
0

LVL 25

Expert Comment

ID: 39252003
Just kidding: when you apply all the SQL statements against you treatment table that you have asked during last week, you will probably end up with a nice empty table. Ha-ha
0

LVL 32

Expert Comment

ID: 39253464
delete from treatmentdata
where patientid in
(select patientid from treatmentdata where treatedwith = 'NoTreatment'
except
select patientid from treatmentdata where treatedwith <> 'NoTreatment')
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.â€‹
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calculâ€¦
###### Suggested Courses
Course of the Month9 days, 12 hours left to enroll