Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old July 25th, 2005, 08:05 PM
kcconnor's Avatar
kcconnor kcconnor is offline
Evil Republican...
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Location: Probably running over your cat right now...
Posts: 250 kcconnor User rank is Sergeant Major (2000 - 5000 Reputation Level)kcconnor User rank is Sergeant Major (2000 - 5000 Reputation Level)kcconnor User rank is Sergeant Major (2000 - 5000 Reputation Level)kcconnor User rank is Sergeant Major (2000 - 5000 Reputation Level)kcconnor User rank is Sergeant Major (2000 - 5000 Reputation Level)kcconnor User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 21 h 35 m 23 sec
Reputation Power: 37
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

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseMicrosoft SQL Server > Sloppy query works, but would like to know a better way...


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT