Hi
I am trying to provide an average length of stay from this set of data, I have pasted it into Excel and calculated the average, which I want to round up.
So I want SQL to calculate this as 4, not 3.9 or 3
My SQL Query:
--Average Length of Stay over quarter
select case
when [Count]=0 then 0
else [No days]/[Count]
end as [Average Length of Stay]
from (
select
count (ppa.PATIENT_ID) as [Count]
,sum(
case
WHEN ppa.activitystartdate < @StartDate and ppa.activityenddate < @StartDate
THEN null
when ppa.activitystartdate > @EndDate
THEN null
when ppa.activitystartdate <= @StartDate and ppa.activityenddate <= @EndDate
then datediff(d,@StartDate,ppa.activityenddate)+1
when ppa.activitystartdate <= @StartDate and ppa.activityenddate >= @EndDate
then datediff(d,@StartDate,@EndDate)
when ppa.activitystartdate >= @StartDate
and datediff(dd,ppa.activitystartdate,@enddate) <=1
then 1
when ppa.activitystartdate >= @StartDate and ppa.activityenddate <= @EndDate
then datediff(d,ppa.activitystartdate,ppa.activityenddate)+1
when ppa.activitystartdate >= @StartDate and ppa.activityenddate >= @EndDate
then datediff(d,ppa.activitystartdate,@EndDate)+1
end
) as [No Days]
FROM
PATIENT_PRIMHD_activity as ppa
where
case
WHEN ppa.activitystartdate < @StartDate and ppa.activityenddate < @StartDate
THEN null
when ppa.activitystartdate > @EndDate
THEN null
when ppa.activitystartdate <= @StartDate and ppa.activityenddate <= @EndDate
then datediff(d,@StartDate,ppa.activityenddate)+1
when ppa.activitystartdate <= @StartDate and ppa.activityenddate >= @EndDate
then datediff(d,@StartDate,@EndDate)
when ppa.activitystartdate >= @StartDate
and datediff(dd,ppa.activitystartdate,@enddate) <=1
then 1
when ppa.activitystartdate >= @StartDate and ppa.activityenddate <= @EndDate
then datediff(d,ppa.activitystartdate,ppa.activityenddate)+1
when ppa.activitystartdate >= @StartDate and ppa.activityenddate >= @EndDate
then datediff(d,ppa.activitystartdate,@EndDate)+1
end is not null
--Filter by House
and ppa.referralteamID=@ResID
and ppa.ActivityTypeCodeID=60
and ppa.ActivitySettingID=173
--Only during period
and (ppa.activityStartDate between @StartDate and @EndDate
or ppa.activityEndDate between @StartDate and @EndDate)
) as a
However, the result it shows is always 3.
I have had a look into using CONVERT to convert the calculated fields to decimal, but cannot get the formula to work.
I have tried:
select case
when [Count]=0 then 0
else convert(decimal(3,2),[No days]/[Count])
end as [Average Length of Stay]
from ( etc.. as above
and the result is 3.00
Can anyone shed some light on this please? I want it to round up, but I could do that manually if it would just show me the decimal places.