WITH l AS ( SELECT DISTINCT logid FROM rlarp.osm_pool ) SELECT doc->>'user' as user, (doc->>'stamp')::timestamptz stamp, CASE WHEN logid IS NULL THEN 'undone' ELSE '' END flag, doc->>'tag', doc->>'message', doc->'scenario'->>'quota_rep_descr' dsm FROM rlarp.osm_log LEFT OUTER JOIN l ON logid = id LIMIT 1000;