2016-11-03 1 views
0

私は、現在の報告上の問題について少し戸惑っています。SQLクエリーをコンセプトにするのが難しい

医者は引退しています。彼女の患者は退職通知を受ける必要があります。 「彼女の患者」を構成する人々は、過去2年間に他のどの医師よりも多くの時間を捉えています。それが失敗した場合(すなわち、過去2年間に彼女と他の医者がそれぞれ2回ずつ見られた場合)、直近に彼女を見に行った場合、彼女は "彼女の患者"です。

これまで私が持っていたことは、これまでのところです。他の医師への訪問を含め、すべての医師の訪問のリスト問題の医師のすべての患者を、私が持っているもの...

をおさらいする

select patient_id, last_name, first_name, post_fromdate, performing_md_id, performing_md_name, category_desc, procedure_code 
from [table] 
where post_fromdate >= ADD_MONTHS(SYSDATE,-24) 
and category_desc like '%WELL%' --specific visit type 
and patient id in ([list of patients]) 
group by patient_id, last_name, first_name, post_fromdate, performing_md_id, performing_md_name, category_desc, procedure_code 
order by patient_id, post_fromdate desc 

。引退医師が個々の患者で最も訪問されたかどうかを示すだろういくつかの追加の基準を:私が求めて何

。与えられた患者の他の医師と同等であるがそれよりも少なくないと見なされた場合、引退した医師が直近に訪問されたかどうか。

いずれかの援助が喜んで受け入れられています。前もって感謝します。

EDIT:select句に、患者の医師を示すpatient_idごとに1つの唯一のperform_md_idを持つ情報が出力されます(過去2年間で最も多く見た医師)。そうでなければ、最近彼らが見た医者)。

+0

スキーマ、サンプルデータ、および希望する結果を投稿します。 –

+0

関連するテーブルのクエリ・ポスト・スキーマにはcount(visits)が必要です。 – donlys

+0

これは災害です:これはすべて一つのテーブルにあります。このDBシステムは、すべての方法で解決策を縛られています。ここの持ち主でさえ、どこに情報があるか分かりません。 – sleddog

答えて

0

は、私はあなたがこのような別の方法

Select patient, 
sum(case when doc = 'retiring_doc' then 1 else 0 end) as [This DOC Visit Count], 
max(case when doc = 'retiring_doc' then visit_date end) as [This DOC Last Visit], 
sum(case when doc != 'retiring_doc' then 1 else 0 end) as [Other Doc Visit Count], 
max(case when doc != 'retiring_doc' then visit_date end) as [Other Doc Last visit] 
from from doc_patient_visit_table 
group by patient 
having sum(case when doc = 'retiring_doc' then 1 else 0 end) > sum(case when doc != 'retiring_doc' then 1 else 0 end) 
OR (sum(case when doc = 'retiring_doc' then 1 else 0 end) = sum(case when doc != 'retiring_doc' then 1 else 0 end) 
AND [This DOC Last Visit] > [Other Doc Last visit]) 
+0

それは完全に意味があります。私はそれをオールド '大学の試してみましょう。あなたのご意見ありがとうございます! – sleddog

1

何か患者、ドキュメント、訪問日

select patient,[This Doc] as [This DOC Visit Count], [Other Doc] as [Other Doc Visit Count] 
into #visit_counts 
from (select patient, case when doctor = 'retiring doc' then 'This Doc' else 'Other Doc' end as Doc, visit_date from doc_patient_visit_table) P 
Pivot (count(visit_date) for Doc in ([This Doc],[Other Doc])) Pvt 

select patient,[This Doc] as [This DOC Last Visit], [Other Doc] as [Other Doc Last visit] 
into #visit_times 
from (select patient, case when doctor = 'retiring doc' then 'This Doc' else 'Other Doc' end as Doc, visit_date from doc_patient_visit_table) P 
Pivot (max(visit_date) for Doc in ([This Doc],[Other Doc])) Pvt 

select patient, [This DOC Visit Count], [Other Doc Visit Count], [This DOC Last Visit], [Other Doc Last visit] 
from #visit_counts t1 
join #visit_times t2 on t1.patient = t2.patient 
where [This DOC Visit Count] > [Other Doc Visit Count] or ([This DOC Visit Count] = [Other Doc Visit Count] and [This DOC Last Visit] > [Other Doc Last visit]) 

の形状にテーブルを持っていると仮定するつもりです。表名と列名は構成されていますが、わかりやすい名前でなければなりません。

select patient_id, 
     max(doctor_id) keep (dense_rank last order by ct, last_visit) as primary_doctor_id 
from (
     select patient_id, doctor_id, count(*) as ct, max(visit_date) as last_visit 
     from  visits 
     group by patient_id, doctor_id 
     ) 
group by patient_id 
; 
関連する問題