cree una columna cuya fecha se va actualizando conforme se va haciendo el cambio en el valor de algun organo de la columna test name de esta tabla final
El problema es que cuando en el estudio no se llego a contestar el formulario del test name y solo se contesto en el formuario que no se realizo la prueba de physical examination(physical examination performed) la fecha de last date aparece nula, he probado con varias formas pero ya sea que me cambie todos los valores de last date por la misma fecha o continue dejandome fechas vacias, necesito que estos espacios vacios se llenen con la fecha de physical examination performed
este es mi codigo
SELECT DISTINCT( Max(fe.cr) ) AS "Last Date",
sid.siteid AS "Site ID",
sbj.subjid AS "Subject ID",
vis.name AS "Visit Name",
perf.val AS "Physical examination performed?",
reas.val AS "Reason if not done",
DATE.val AS "Date",
TIME.val AS "Time",
CASE
WHEN test.name = 'Other' THEN other.val
ELSE test.name
END AS "Test name",
res.val AS "Result",
clinsig.val AS "Clinically significant?"
--siteid
FROM (SELECT dtc.unit_id,
ac.affix AS siteid,
ac.system_id AS sitesys
FROM adm_centers ac
join dat_treatment_centers dtc
ON ac.system_id = dtc.center_id) sid
--Performed?
left join (SELECT di.event_id,
di.system_id,
di.unit_id,
di.form_id,
di.val,
di.item_group_id
FROM dat_items di
left join sdf_datapoints sd
ON di.datapoint_id = sd.system_id
WHERE sd.item_oid = 'i_physexam_perf'
AND di.is_current = TRUE) perf
ON sid.unit_id = perf.unit_id
AND vis.system_id = perf.event_id
--LAST DATE
left join (SELECT di.event_id,
di.system_id,
di.unit_id,
di.form_id,
di.val AS sign,
di.created_on AS cr,
di.item_group_id
FROM dat_items di
left join sdf_datapoints sd
ON di.datapoint_id = sd.system_id
WHERE sd.form_oid = 'f_physexam_com'
AND sd.item_oid = 'i_pe_res'
AND di.is_current = TRUE) fe
ON test.item_group_id = fe.item_group_id
--Test name
left join (SELECT di.event_id,
di.system_id,
di.unit_id,
di.form_id,
di.item_group_id,
di.val,
sig.name
FROM dat_items di
left join sdf_datapoints sd
ON di.datapoint_id = sd.system_id
left join dat_igroups dig
ON dig.system_id = di.item_group_id
left join sdf_igroups sig
ON dig.def_id = sig.system_id
WHERE sd.item_group_oid LIKE 'g_pe_%'
AND sd.item_oid LIKE 'i_pe_res'
AND di.is_current = TRUE) test
ON sid.unit_id = test.unit_id
AND TIME.form_id = test.form_id
Si lo pongo asi se actualiza para cada organo pero me sale vacio como la segunda imagen pero si lo pongo asi
--LAST DATE
LEFT join
(
select di.event_id,
di.system_id,
di.unit_id,
di.form_id,
di.val as sign,
di.created_on as cr,
di.item_group_id
from dat_items di
left join sdf_datapoints sd
on di.datapoint_id = sd.system_id
where sd.form_oid = 'f_physexam_com'
and sd.item_oid in ('i_physexam_perf',
'i_pe_res')
and di.is_current=true) fe on test.item_group_id = fe.item_group_id
or
perf.item_group_id = fe.item_group_id
Me pone todo con la misma fecha