William,
Are you counting the day they leave/arrive, it does not look like it. There
are 31 days in January, so a patient that arrives on January 4 and doesn't
leave until March 31st would be on site for 28 days, not 27.
I've got a slightly different technique than Karl,
1. Create a new table (I call it tbl_Numbers), with one field [Number]
which is defined as a long integer. Fill this table with the numbers 1-12
(for the months).
2. Create a couple of functions. These will be used in the query to
determine the number of days the patient is in the hospital during a given
month. These are functions I created a while ago to give me the minimum and
maximum from among a group of values passed to the function. The nice thing
is that they work just as well for dates as it does for numbers or strings.
Public Function Minimum(ParamArray MyArray() As Variant) As Variant
Dim intLoop As Integer
For intLoop = LBound(MyArray) To UBound(MyArray)
If IsEmpty(Minimum) Then
Minimum = MyArray(intLoop)
ElseIf IsNull(MyArray(intLoop)) Then
'do nothing
ElseIf MyArray(intLoop) < Minimum Then
Minimum = MyArray(intLoop)
End If
Next
End Function
Public Function Maximum(ParamArray MyArray() As Variant) As Variant
Dim intLoop As Integer
For intLoop = LBound(MyArray) To UBound(MyArray)
If IsEmpty(Maximum) Then
Maximum = MyArray(intLoop)
ElseIf IsNull(MyArray(intLoop)) Then
'do nothing
ElseIf MyArray(intLoop) > Maximum Then
Maximum = MyArray(intLoop)
End If
Next
End Function
3. Create the following query. This query gives you a record for each
month that each patient is present, the patients ID number, and the number
of days they were present during the month. You will have to play with the
parameters inside the DateDiff, Maximum, and Minimum functions to get the
values you want, which are determined by whether you are counting the start
and end dates of their stay. Once you have the durations working out the
way you want, then modify the query and group it by the Stay_Month, Count on
the Pt_ID column, and Sum on the Duration column
SELECT tbl_Numbers.Number AS Stay_Month
, tbl_Patient_Dates.Pt_ID
,
DateDiff("d",Maximum([Start_Date],DateSerial(2006,[Number],1)),
Minimum(DateSerial(2006,[Number]+1,1),[End_Date])) AS Duration
FROM tbl_Numbers, tbl_Patient_Dates
WHERE (((tbl_Numbers.Number) Between Month([Start_Date]) And
Month([End_Date]))
AND ((tbl_Patient_Dates.Start_Date)<DateSerial(2007,1,1))
AND ((tbl_Patient_Dates.End_Date)>DateSerial(2006,1,0)))
ORDER BY tbl_Numbers.Number;
Hope this helps.
William said:
I have database with medicare patients that needs to determine the length
of
stay and number of patients by month. The data looks like this:
Pt# Start dt End dt
123 1/4/06 3/31/06
456 1/6/06 4/11/06
676 5/1/06 5/10/06
Whenever a patients stay is longer than a month need to count that
patients
days in that month.
What the result should be is this:
Month #Patients Length of stay
Jan 06 2 52 (days) - both patients days to the
end
of the month
Feb 06 2 56 (days)
Mar 06 2 62 (days)
Apr 06 1 11 (days)
May 06 1 10 (days)
Any suggestions ?