
July 25th, 2005, 08:05 PM
|
 |
Evil Republican...
|
|
Join Date: Jun 2005
Location: Probably running over your cat right now...
|
|
|
Sloppy query works, but would like to know a better way...
The goal of this code is to examine all transactions earlier than a specified date and find out what visit they came from. If the patient is covered by medicare and the visit does not have a specific transaction code in the group of transaction codes that corellate to that visit, then output the patient's name and the visit date that lacks the correct transaction code.
It's sloppy and takes about 20 minutes to run, but I couldn't think of any other way to do it!
Code:
/*
A script to scan transactions for medicare recipients who do not have
one of two transaction codes before a specific date.
*/
declare medicare_cursor scroll cursor
for
select distinct t.source_id
from transactions t
inner join visits v
on t.source_id = v.visit_id
inner join network_people np
on t.hin = np.hin
inner join network_payers npay
on np.hin = npay.hin
inner join payer_mstr pm
on pm.payer_id = npay.payer_id
inner join contract_links cl
on cl.payer_id = pm.payer_id
inner join contract_mstr cm
on cm.contract_id = cl.contract_id
where v.visit_date < '2005-01-06'
and cm.contract_id = 'MedicareContractNumber'
open medicare_cursor
declare @tnt int
declare @nmt int
declare @source_id varchar(30)
declare @tnt_result int
fetch first from medicare_cursor into @source_id
while @@fetch_status = 0
begin
--This will be the number of medicare transactions, NMT
set @nmt = (select count(t.source_id)
from transactions t
inner join claims c
on t.source_id = c.visit_id
where t.tran_code_id in ( 'code1', 'code2')
and t.source_id = @source_id)
--This will be the total number of transactions, TNT
set @tnt = (select count(t.source_id)
from transactions t
inner join claims c
on t.source_id = c.visit_id
where t.tran_code_id not in ( 'code1', 'code2')
and t.source_id = @source_id)
set @tnt_result = @tnt
/*
If the difference of TNT - NMT = TNT for a given source id,
then select the patient information, source_id and the date.
If TNT - NMT < TNT, then do not return anything.
*/
if (@tnt - @nmt = @tnt_result)
begin
declare @firstname varchar(40)
declare @lastname varchar(40)
declare @visitdate varchar(40)
set @firstname = (select distinct np.first_name
from transactions t
inner join network_people np
on np.hin = t.hin
inner join claims c
on c.visit_id = t.source_id
where t.source_type <> 'A'
and t.source_id = @source_id)
set @lastname = (select distinct np.last_name
from transactions t
inner join network_people np
on np.hin = t.hin
inner join claims c
on c.visit_id = t.source_id
where t.source_type <> 'A'
and t.source_id = @source_id)
set @visitdate = (select distinct c.visit_date
from transactions t
inner join network_people np
on np.hin = t.hin
inner join claims c
on c.visit_id = t.source_id
where t.source_type <> 'A'
and t.source_id = @source_id)
print @firstname + ' ' + @lastname + ' DATE: ' + @visitdate
end
fetch next from medicare_cursor into @source_id
end
close medicare_cursor
deallocate medicare_cursor
|