I am working against a mssql 2005 db. I have largely tought myself sql, and as you will probably notice from the script below, I have lots to learn.
The following logic collects encounters that need to be deleted, and then uses a fetch statement to accomplish the deletions. It works, but it is pretty crude. I am curious to see how those of you far more skilled than I would rewrite this into a more logical and efficient script.
Thanks in advance for assistance.
/*collect all drug study encounters for deletion*/
/*collect all appointments that resulted in 'drug study' encounters*/
select a.appt_id,a.enc_id,a.appt_date
into #DR
from appointments a
inner join appointment_members am on am.appt_id = a.appt_id
where a.event_id = '12928339-AA0D-4A56-8F26-972517A237E4'
and a.enc_id is not null
/*Count the charges for each of the encounters above*/
select count(c.charge_id) as Count,#DR.enc_id
into #e
from charges c
right outer join #DR on #DR.enc_id = c.source_id
group by #DR.enc_id
/*collect together all the stuff on the encounters without any charges*/
select pe.enc_nbr,#E.enc_id,#DR.appt_id,#DR.appt_date,ep.payer_id
into #Delete
from #E
inner join patient_encounter pe on pe.enc_id = #E.enc_id
left outer join encounter_payer ep on ep.enc_id = pe.enc_id
inner join #DR on #DR.enc_id = pe.enc_id
where #E.count = 0
/*fetch, fetch, fetch*/
declare @enc_nbr char(8),
@enc_id uniqueidentifier,
@appt_id uniqueidentifier,
@appt_date char(8),
@payer_id uniqueidentifier
declare DeleteDrugStudy CURSOR for
select #Delete.enc_nbr,#Delete.enc_id,#Delete.appt_id,#Delete.appt_date,#Delete.payer_id
from #Delete
Open DeleteDrugStudy
Fetch Next From DeleteDrugStudy
into @enc_nbr, @enc_id, @appt_id, @appt_date, @payer_id
While @@FETCH_STATUS = 0
Begin
/*remove entry in appointment*/
Update appointments set enc_id = null where appointments.appt_id = @appt_id
/*remove attached payers*/
if @payer_id is not null delete from encounter_payer
where encounter_payer.enc_id = @enc_id
and encounter_payer.payer_id = @payer_id
/*delete encounter itself*/
Delete from patient_encounter where patient_encounter.enc_id = @enc_id
Fetch Next From DeleteDrugStudy
into @enc_nbr, @enc_id, @appt_id, @appt_date, @payer_id
End
Close DeleteDrugStudy
Deallocate DeleteDrugStudy
Go
The following logic collects encounters that need to be deleted, and then uses a fetch statement to accomplish the deletions. It works, but it is pretty crude. I am curious to see how those of you far more skilled than I would rewrite this into a more logical and efficient script.
Thanks in advance for assistance.
/*collect all drug study encounters for deletion*/
/*collect all appointments that resulted in 'drug study' encounters*/
select a.appt_id,a.enc_id,a.appt_date
into #DR
from appointments a
inner join appointment_members am on am.appt_id = a.appt_id
where a.event_id = '12928339-AA0D-4A56-8F26-972517A237E4'
and a.enc_id is not null
/*Count the charges for each of the encounters above*/
select count(c.charge_id) as Count,#DR.enc_id
into #e
from charges c
right outer join #DR on #DR.enc_id = c.source_id
group by #DR.enc_id
/*collect together all the stuff on the encounters without any charges*/
select pe.enc_nbr,#E.enc_id,#DR.appt_id,#DR.appt_date,ep.payer_id
into #Delete
from #E
inner join patient_encounter pe on pe.enc_id = #E.enc_id
left outer join encounter_payer ep on ep.enc_id = pe.enc_id
inner join #DR on #DR.enc_id = pe.enc_id
where #E.count = 0
/*fetch, fetch, fetch*/
declare @enc_nbr char(8),
@enc_id uniqueidentifier,
@appt_id uniqueidentifier,
@appt_date char(8),
@payer_id uniqueidentifier
declare DeleteDrugStudy CURSOR for
select #Delete.enc_nbr,#Delete.enc_id,#Delete.appt_id,#Delete.appt_date,#Delete.payer_id
from #Delete
Open DeleteDrugStudy
Fetch Next From DeleteDrugStudy
into @enc_nbr, @enc_id, @appt_id, @appt_date, @payer_id
While @@FETCH_STATUS = 0
Begin
/*remove entry in appointment*/
Update appointments set enc_id = null where appointments.appt_id = @appt_id
/*remove attached payers*/
if @payer_id is not null delete from encounter_payer
where encounter_payer.enc_id = @enc_id
and encounter_payer.payer_id = @payer_id
/*delete encounter itself*/
Delete from patient_encounter where patient_encounter.enc_id = @enc_id
Fetch Next From DeleteDrugStudy
into @enc_nbr, @enc_id, @appt_id, @appt_date, @payer_id
End
Close DeleteDrugStudy
Deallocate DeleteDrugStudy
Go