Я ожидаю что-то вроде этого:
select nc.patientunitstayid, gs.hr,
avg(case when nc.nibp_systolic >= 1 and nc.nibp_systolic <= 250
then nibp_systolic
end) as nibp_systolic_avg
from (select nc.*,
min(nursingchartoffset) over (partition by patientunitstayid) as min_nursingchartoffset,
max(nursingchartoffset) over (partition by patientunitstayid) as max_nursingchartoffset
from nc
) nc cross join lateral
generate_series(ceil(min_nursingchartoffset/60.0),
ceil(max_nursingchartoffset/60.0)
) as gs(hr)
group by nc.patientunitstayid, hr
order by nc.patientunitstayid asc, hr asc;
То есть вам нужно агрегировать по hr
.Я поместил это в предложение from
, чтобы подчеркнуть, что это генерирует строк .Если вы используете более старую версию Postgres, то у вас может не быть боковых соединений.Если это так, просто используйте подзапрос в предложении from
.
EDIT:
Вы также можете попробовать:
from (select nc.*,
generate_series(ceil(min(nursingchartoffset) over (partition by patientunitstayid) / 60.0),
ceil(max(nursingchartoffset) over (partition by patientunitstayid)/ 60.0)
) hr
from nc
) nc
и настроить ссылки на hr
во внешнем запросе.