@@ -267,7 +267,15 @@ CREATE OR REPLACE VIEW public.timeline_establishment_def
267267 FROM public .stat_for_unit AS sfu
268268 WHERE sfu .establishment_id = es .id
269269 AND from_until_overlaps(t .valid_from , t .valid_until , sfu .valid_from , sfu .valid_until )
270- ) AS sfu ON TRUE
270+ ) AS sfu_ds ON TRUE
271+ LEFT JOIN LATERAL (
272+ SELECT sfu .edit_comment , sfu .edit_by_user_id , sfu .edit_at
273+ FROM public .stat_for_unit AS sfu
274+ WHERE sfu .establishment_id = es .id
275+ AND from_until_overlaps(t .valid_from , t .valid_until , sfu .valid_from , sfu .valid_until )
276+ ORDER BY sfu .edit_at DESC
277+ LIMIT 1
278+ ) AS sfu_le ON TRUE
271279 LEFT JOIN LATERAL (
272280 SELECT array_agg(ds .id ) AS ids
273281 , array_agg(ds .code ) AS codes
@@ -277,7 +285,7 @@ CREATE OR REPLACE VIEW public.timeline_establishment_def
277285 OR COALESCE(ds .id = sa .data_source_id , FALSE)
278286 OR COALESCE(ds .id = phl .data_source_id , FALSE)
279287 OR COALESCE(ds .id = pol .data_source_id , FALSE)
280- OR COALESCE(ds .id = ANY(sfu .data_source_ids ), FALSE)
288+ OR COALESCE(ds .id = ANY(sfu_ds .data_source_ids ), FALSE)
281289 ) AS ds ON TRUE
282290 LEFT JOIN LATERAL (
283291 SELECT edit_comment, edit_by_user_id, edit_at
@@ -288,7 +296,8 @@ CREATE OR REPLACE VIEW public.timeline_establishment_def
288296 (sa .edit_comment , sa .edit_by_user_id , sa .edit_at ),
289297 (phl .edit_comment , phl .edit_by_user_id , phl .edit_at ),
290298 (pol .edit_comment , pol .edit_by_user_id , pol .edit_at ),
291- (c .edit_comment , c .edit_by_user_id , c .edit_at )
299+ (c .edit_comment , c .edit_by_user_id , c .edit_at ),
300+ (sfu_le .edit_comment , sfu_le .edit_by_user_id , sfu_le .edit_at )
292301 ) AS all_edits(edit_comment, edit_by_user_id, edit_at)
293302 WHERE edit_at IS NOT NULL
294303 ORDER BY edit_at DESC
0 commit comments