0
CREATE TABLE [dbo].[rx](
            [pat_id] [int] NOT NULL,
            [fill_Date] [date] NOT NULL,
            [script_End_Date]  AS (dateadd(day,[dayssup],[filldate])),
            [drug_Name] [varchar](50) NULL,
            [days_Sup] [int] NOT NULL,
            [quantity] [float] NOT NULL,
            [drug_Class] [char](3) NOT  NULL,
            CHECK(fill_Date <=script_End_Date
PRIMARY KEY CLUSTERED 
(
            [clmid] ASC
)
CREATE TABLE [dbo].[Calendar](
            [cal_date] [date] NOT NULL,
            [julian_seq] [int] IDENTITY(1,1) NOT NULL,
--unsure if the above line is an acceptable way of adding a 'julianized date number', the data in this database ranges from 1-1-2007 to 12-31-2009
PRIMARY KEY CLUSTERED 
(
            [cal_date] ASC
)

対象のテーブルと、上記の構造を持つカレンダー テーブルがあります。フィールド Drug_class によって、特定の時間 (重複する日付) に設定された数の家族で人が服用している個別の薬物の最大数を見つけようとしています。

ここのコミュニティの助けを借りて同様の問題に成功しましたが、現時点では何か間違ったことをしていて、非常に不正確な結果が得られています. 可能であれば、返される結果セットを次のようにしたいと思います

create table DesiredResults
(pat_id int, min_overlap date, max_overlap date, drug_class char(3),drug_name varchar(50))
insert into Desired_Results(patid, minoverlap, maxoverlap, drug_class,drug_name)
values (1111,'2008-11-28', '2008-12-18','h3a','drug X')
      ,(1111,'2008-11-28','2008-12-18','h3a','drug Y')

これは、患者 111 より上の時間枠で薬 x と薬 y が処方されたことを意味します。

私の質問は -

;with Overlaps (pat_id,cal_date,drug_class)
as
(
select
mdo.pat_id
,c1.cal_date
,mdo.drug_class
from
(
--this gives a table of all the scripts a person had within the classes restricted in the where rx.drug_class IN clause and their fill_date and script_end_dates
SELECT DISTINCT
 rx.pat_id
,rx.drug_class
,rx.drug_name
,rx.fill_date
,rx.script_end_date
FROM   rx
WHERE  rx.drug_class IN( 'h3a', 'h6h', 'h4b', 'h2f', 'h2s', 'j7c', 'h2e' )
--
) as mdo,Calendar as c1
where c1.cal_date between mdo.fill_date and mdo.script_end_date
group by mdo.pat_id,c1.cal_date,mdo.drug_class
having count(*) > 1--overlaps
)
,
Groupings(pat_id,cal_date,drug_class,grp_nbr)
as
(
select
o.pat_id
,o.cal_date
,o.drug_class
,c2.julian_seq

--julianized date
-row_number() over(partition by o.pat_id,o.drug_class order by o.cal_date) as grp_nbr
from Overlaps as o,calendar as c2
where c2.cal_date = o.cal_date
)
,x

as
(

--i think this is what's causing the problem

select pat_id,min(cal_date) as min_overlap,max(cal_date) as max_overlap,drug_class
from groupings
group by pat_id,grp_nbr,drug_class

)

select 
 x.pat_id
,x.min_overlap
,x.max_overlap
,y.drug_class
,y.drug_name
from x
inner join
(
select distinct
 rx.pat_id
,rx.drug_name
,rx.drug_class
,rx.fill_date
from rx
) as y on x.pat_id = y.pat_id and x.drug_class=y.drug_class
          and y.fill_date between x.min_overlap and x.max_overlap
order by datediff(day,min_overlap,max_overlap) desc

特定のクラスで最も多くの薬が処方されている日数を探しています。ただし、現在、これにより、単一の日付範囲よりも大きい日付範囲が得られますdatediff(day,fill_date,script_end_date)

これは、オーバーラップ範囲の一部が何年にも及ぶため、数値が人為的に膨らんでいます。多くても、医師がスクリプトを作成する日数とほぼ同じである必要があります。クラス 'h3a' の 5 つの薬が同じ日に処方された場合、その期間は、そのクラスの各薬に対してpat_id, fill_date, end_date,が 5 回繰り返されます。h3a

4

1 に答える 1

0

これで問題が解決するかどうかはわかりません。これにより、最大数の薬が処方される日付がわかります。

select c.cal_date
from (select c.cal_date, count(*) as NumDrugs,
             dense_rank() over (order by count(*) desc) as seqnum
      from Calendar c join
           rx
           on c.cal_date between rx.fill_date betwen rx.script_end_date and
              rx in IN( 'h3a', 'h6h', 'h4b', 'h2f', 'h2s', 'j7c', 'h2e' )
      group by c.cal_date
    ) crx
where seqnum = 1

それはあなたの質問よりもはるかに簡単なので、私は何かを逃したのではないかと思います。

これをピリオドに変換する必要がある場合、それも可能ですが、SQLはもう少し面倒です。

また、このSQLはテストされていないため、構文エラーが発生する可能性があります。

于 2013-01-12T02:41:02.100 に答える