2

構造のあるテーブルがあります

(処方箋)

clmID         int
patid         int
drugclass     char(3)
drugName      char(25)
fillDate      date
scriptEndDate date
strength      int

そしてクエリ

;with PatientDrugList(patid, filldate,scriptEndDate,drugClass,strength)      
as      
(      
select rx.patid,rx.fillDate,rx.scriptEndDate,rx.drugClass,rx.strength      
 from rx      
)      
,      
DrugList(drugName)      
as      
(      
select x.drugClass      
from (values('h3a'),('h6h'))      
as x(drugClass)      
where x.drugClass is not null      
)      
SELECT PD.patid, C.calendarDate AS overlap_date      
 FROM PatientDrugList AS PD, Calendar AS C      
 WHERE drugClass IN ('h3a','h6h')      
  AND calendardate BETWEEN filldate AND scriptenddate      
 GROUP BY PD.patid, C.CalendarDate      
HAVING COUNT(DISTINCT drugClass) = 2     
order by pd.patid,c.calendarDate  

これCalendarは、他の列がない、調査期間中のすべての可能な日付を含む単純なカレンダーテーブルです。

私のクエリは次のようなデータを返します

ここに画像の説明を入力してください

overlay_dateは、PatientDrugListCTEの後にリストされている2つのクラスで人が薬を処方された毎日を表します。

一人一人が両方の薬の家族に処方された連続日数を知りたいです。誰かがこのレジメンを止めてから再開したかどうかがわからないため、単純な骨材を使用することはできませんmaxminこれを見つけるための効率的な方法は何ですか?

編集:DrugList CTEの行コンストラクターは、ストアドプロシージャのパラメーターである必要があり、この例の目的のために修正されました。

4

2 に答える 2