这些天我一直在尝试解决这个问题,但尚未找到解决方案。这是我当前的查询:
SELECT e.id, case when 7768 = any(e.class) then 'INP' else 'OUT' END AS type,
cc.display AS typedesc, ccc.display,
cccc.display AS status3 , pat.id AS patid,
pat.gender , pat.birthdate,
l.alias AS room_ref, l.name AS room_name,
l.id AS room_id , ll.id AS bed_id,
ll.alias AS bed_name, l.partof,
concat(hn.prefix,' ',hn.given,' ',hn.family), e.plannedenddate
FROM ecosystem.encounter e
LEFT JOIN ecosystem.codeableconcept cc ON cc.id = any(e.class)
LEFT JOIN ecosystem.codeableconcept ccc ON ccc.id = e.priority
LEFT JOIN ecosystem.codeableconcept cccc ON cccc.id = e.subjectstatus
LEFT JOIN ecosystem.patient pat ON pat.id = (e.subject->>'identifier')::int
LEFT JOIN ecosystem.practitioner prac
ON prac.id = (SELECT (zz->'actor'->>'identifier')::int
FROM jsonb_array_elements(e.participant) zz
WHERE zz->'type'->>'code' = 'ATND')
LEFT JOIN ecosystem.humanname hn ON hn.id = any(prac.name)
LEFT JOIN (SELECT * FROM ecosystem.locations WHERE form = 7930) l
ON l.id = (e.location[jsonb_array_length(e.location)-1]->>'identifier')::int
LEFT JOIN (SELECT * FROM ecosystem.locations WHERE form = 7931) ll
ON ll.id = (e.location[jsonb_array_length(e.location)-1]->>'identifier')::int
WHERE EXISTS (
SELECT FROM jsonb_array_elements(e.participant) AS papi
WHERE jsonb_typeof(papi->'actor') = 'object'
AND (papi->'actor'->>'identifier') = '27'
AND (papi->'actor'->>'reference') LIKE 'Organization/%')
AND e.status in ('in-progress','on-hold','discharged')
AND 3 = any(l.partof)
AND EXISTS (
SELECT FROM jsonb_array_elements(participant) pa
WHERE pa->'type'->>'code' = 'ATND'
AND pa->'actor'->>'identifier' ='99')
这是我从旧查询中尝试查询患者在医院内的位置。之前我们只需要房间,因此:
left join (select * from ecosystem.locations where form = 7930) l
on l.id = (e.location[jsonb_array_length(e.location)-1]->>'identifier')::int
有效,因为 e.location 中的最后一个元素是患者的房间。现在情况并非如此,因为它可以是一个房间或一张床,不知何故,我需要过滤
any
并匹配与左侧子查询的任何 id
匹配的最新项目。
尝试下面,我引入了一个
LATERAL
连接来动态处理 e.location JSONB
数组中的最后一个元素可以是房间或床的情况。此连接首先识别 e.location
中的最新位置,然后根据该位置是被识别为房间 (form 7930)
还是床 (form 7931)
有条件地连接到位置表。
SELECT e.id,
CASE
WHEN 7768 = ANY(e.class) THEN 'INP'
ELSE 'OUT'
END AS type,
cc.display as typedesc,
ccc.display,
cccc.display as status3,
pat.id as patid,
pat.gender,
pat.birthdate,
l.alias as room_ref,
l.name as room_name,
l.id as room_id,
ll.id as bed_id,
ll.alias as bed_name,
l.partof,
CONCAT(hn.prefix,' ',hn.given,' ',hn.family),
e.plannedenddate
FROM ecosystem.encounter e
LEFT JOIN ecosystem.codeableconcept cc on cc.id = ANY(e.class)
LEFT JOIN ecosystem.codeableconcept ccc on ccc.id = e.priority
LEFT JOIN ecosystem.codeableconcept cccc on cccc.id = e.subjectstatus
LEFT JOIN ecosystem.patient pat on pat.id = (e.subject->>'identifier')::int
LEFT JOIN ecosystem.practitioner prac on prac.id = (SELECT (zz->'actor'->>'identifier')::int FROM jsonb_array_elements(e.participant) zz WHERE zz->'type'->>'code' = 'ATND')
LEFT JOIN ecosystem.humanname hn on hn.id = ANY(prac.name)
LEFT JOIN LATERAL (
SELECT *
FROM ecosystem.locations
WHERE id = (e.location[jsonb_array_length(e.location)-1]->>'identifier')::int
AND form IN (7930, 7931)
) loc ON TRUE
LEFT JOIN ecosystem.locations l ON loc.id = l.id AND l.form = 7930
LEFT JOIN ecosystem.locations ll ON loc.id = ll.id AND ll.form = 7931
WHERE EXISTS (
SELECT 1
FROM jsonb_array_elements(e.participant) AS papi
WHERE
jsonb_typeof(papi->'actor') = 'object'
AND (papi->'actor'->>'identifier') = '27'
AND (papi->'actor'->>'reference') LIKE 'Organization/%'
)
AND e.status IN ('in-progress','on-hold','discharged')
AND 3 = ANY(l.partof)
AND EXISTS (
SELECT 1
FROM jsonb_array_elements(participant) pa
WHERE pa->'type'->>'code' = 'ATND'
AND pa->'actor'->>'identifier' ='99'
)