scriptscribe
MIS
Hi,
I have a table in which I want to perform the following function:
If StartDate <= AdmitDate then
(If DischargeDate > End Date then ((EndDate - (AdmitDate + 1)))
Else (DischargeDate - AdmitDate)
Else If AcctNum(from Current Row) = AcctNum (from Previous Row) then (DischargeDate - StartDate)
Basically, the above is a big iif(,,) statement from Excel. That part I can figure out. What I'm having difficulty with is referencing the previous row to compare the account numbers to see if there is a match on that previous row. If so, I want to perform different date calculations than if it is a unique row.
Here is an example of data from my table:
HospitalFK AcctNum AdmitDate DischDate FirstName LastName SSN PatientAcctIPK
1310 1001725231 12/13/06 12/14/06 MARY HANKEY 550527648 25819966
1310 1001725231 01/20/07 01/22/07 MARY HANKEY 550527648 25819966
1310 1001392230 9/2/05 9/6/05 CATHERINE DIAZ 762488212 25868116
1310 1002054730 4/26/05 5/13/05 LILLIAN JONES 985200344 26046401
For anyone who's curious, yes (of course), I changed names and SSNs in the above data before posting them here.
Below is my code so far. I've isolated the dupes and just need to know how to reference the previous row. BTW, in the below code, #PartA is the temp table that is composed of data from the PatientAcctsCodes table and another called PatientAccts, which just has financial data on it, such as TotalCharges and CurrentBalance.
I wrote a cursor that I thought would work, but after it ran for over an hour, I cancelled it, as it doesn't do me any good if it's going to take that long to run. So I'm back to trying to do it this way.
Any help will be greatly appreciated.
Thanks,
Mike
I have a table in which I want to perform the following function:
If StartDate <= AdmitDate then
(If DischargeDate > End Date then ((EndDate - (AdmitDate + 1)))
Else (DischargeDate - AdmitDate)
Else If AcctNum(from Current Row) = AcctNum (from Previous Row) then (DischargeDate - StartDate)
Basically, the above is a big iif(,,) statement from Excel. That part I can figure out. What I'm having difficulty with is referencing the previous row to compare the account numbers to see if there is a match on that previous row. If so, I want to perform different date calculations than if it is a unique row.
Here is an example of data from my table:
HospitalFK AcctNum AdmitDate DischDate FirstName LastName SSN PatientAcctIPK
1310 1001725231 12/13/06 12/14/06 MARY HANKEY 550527648 25819966
1310 1001725231 01/20/07 01/22/07 MARY HANKEY 550527648 25819966
1310 1001392230 9/2/05 9/6/05 CATHERINE DIAZ 762488212 25868116
1310 1002054730 4/26/05 5/13/05 LILLIAN JONES 985200344 26046401
For anyone who's curious, yes (of course), I changed names and SSNs in the above data before posting them here.
Below is my code so far. I've isolated the dupes and just need to know how to reference the previous row. BTW, in the below code, #PartA is the temp table that is composed of data from the PatientAcctsCodes table and another called PatientAccts, which just has financial data on it, such as TotalCharges and CurrentBalance.
Code:
select Count(*) as Amount, acctnum, c.AdmitDate, c.DischargeDate, c.FirstName, c.LastName, c.DOB, c.SSN, c.PatientAcctIPK
from #PartA c, patientacctscodes pac, foundcoverage fc, medicaidcoverage mc
where c.patientacctipk = pac.patientacctifk and c.hospitalfk = pac.hospitalfk and c.patientacctipk = fc.patientacctifk
and fc.hospitalfk = mc.hospitalfk and fc.foundcoverageipk = mc.foundcoverageifk
and hitstatus in (1,2)
group by acctnum, c.firstname, c.lastname, c.dob, c.ssn, c.admitdate, c.dischargedate, fc.coverageid, mc.startdate, mc.enddate, mc.programtype, pac.finclasscurrent, pac.insurancecode1, pac.insurancecode2, pac.insurancecode3,
pac.servicetype
having count(AcctNum) > 1
order by acctnum
I wrote a cursor that I thought would work, but after it ran for over an hour, I cancelled it, as it doesn't do me any good if it's going to take that long to run. So I'm back to trying to do it this way.
Any help will be greatly appreciated.
Thanks,
Mike