2016-09-23 1 views
0

새 버전이 아닌 이전 버전의 문서가 연결된 활성 클라이언트 (case_status = 'A')를 나열하는 보고서를 만들려고합니다. 그러나 우리 데이터베이스가 배치 된 방식으로 모든 문서가 하나의 테이블에 나열되므로 일부 클라이언트에는 12 개의 서로 다른 문서가 첨부됩니다. 그들 중 일부는 중복 된 문서입니다.하나의 열 값과 연관이있는 클라이언트 선택

Ex.

patient_id  Doc_code 
p01   doc1 
p01   doc2 
p01   doc3 
po1   doc4 
p02   doc2 
po2   doc3 

나는 'DIAGDOC'의 doc_code을 가지고 있으며, 'DIAGDOC5'의 doc_code이없는 사람 알아야 할, 그래서 우리는 업데이트 할 필요가 누구인지.

select 

de.patient_id, 
de.episode_id 

from doc_entity de 
join patient p 
on p.patient_id = de.patient_id and p.episode_id = de.episode_id 
where p.case_status = 'A' 

group by de.patient_id, de.episode_id, de.doc_code 
having (de.doc_code in ('DIAGDOC'))and (de.doc_code not in ('DIAGDOC5')) 
order by de.patient_id, de.episode_id 

답변

1

except 절은 작동합니까?

select 
de.patient_id, 
de.episode_id 

from doc_entity de 
join patient p 
on p.patient_id = de.patient_id and p.episode_id = de.episode_id 
where p.case_status = 'A' 
and de.doc_code = 'DIAGDOC' 

EXCEPT 

select 

de.patient_id, 
de.episode_id 

from doc_entity de 
join patient p 
on p.patient_id = de.patient_id and p.episode_id = de.episode_id 
where p.case_status = 'A' 
and de.doc_code = 'DIAGDOC5'* 

상단 블록은 diagdoc 모든 행을 반환하고 블록을 제외하고는 날짜 문서의 밖으로 만 떠나 diagdoc5 모든 사람들을 제거

+0

나는 것 일부 고객이 균열을 빠져 나갈 수 있도록 이유를 알아 내려고하는 중입니다. –

+1

diagdoc2와 같은 사람들이 있지만 diagdoc이 아닌 사람들이 겪고있는 문서가 있습니까? 상위 블록의 diagdoc을 'diagdoc %'와 같이 바꾸면 파일 버전이 있지만 최신 버전이 아닌 다른 사용자에게 결과를 필터링하므로 docs 2,3,4,5를 가진 누군가는 올 수 없지만 그러나 2,3,4 그들은 내 원래 쿼리와 함께 cuaght되지 않았을 것입니다. – MarkD

+0

사실 나는 그것이 효과가 있다고 생각합니다. 이전에 Except 절을 사용하지 않았지만 새로운 친한 친구가 될 것 같아요. 우리는 임상가가 오늘 아침 일찍 작성한 내 목록의 맨 위에있는 두 개의 문서를 업데이트하도록했습니다. 검사를 한 번 더 해보고 확인 표시를 보냅니다. –

1

합니까 아래 작업 -

select 
    aa.patient_id, aa.episode_id 
from 
(
    select 
      de.patient_id 
     , de.episode_id 
     , MAX(case doc_code WHEN 'DIAGDOC' THEN 'YES' ELSE 'NO' end) as 'DIAGDOCExists' 
     , MAX(case doc_code WHEN 'DIAGDOC5' THEN 'YES' ELSE 'NO' end) as 'DIAGDOC5Exists' 
    from 
     doc_entity de join patient p on p.patient_id = de.patient_id and p.episode_id = de.episode_id 
    where 
     p.case_status = 'A' 
    group by 
     de.patient_id, de.episode_id 
) as aa where aa.DIAGDOCExists = 'YES' and aa.DIAGDOC5Exists = 'NO' 
+0

8시 9 분에 1이 있어야하기 때문에 구문 오류가 발생합니다. 그러나 8시에 구문 오류가 발생할 때 중복을 제거하면됩니다. –

+1

지금 시도하십시오. 도입 된시기가 어떻게 두 번 인지도 모르고, 마지막 절도 누락되었습니다. – Neeraj