1

以下のテーブル作成クエリのような結果を返すクエリがあります

create table #testresults
(
  pat_id int,
  fill_date date,
  script_end_date date,
  drug_class char(3),
  distinctDrugs int
)

pat_id を指定できる薬には 7 つの異なるカテゴリがあります。列は、との時間枠内で投与できるdistinctDrugsさまざまな薬の数です。クエリを実行した結果は次のようになります。 pat_idfill_datescript_end_dateここに画像の説明を入力

それぞれpat_idに多くの異なる期間がfill_dateあります。script_end_dateこれらの異なる期間には、行ごとに異なるdrug_classandがありdistinctDrugsます。この例の右側の 2 つの列は、必要なものを示してfill_dateいます。このクエリを使用して、右端の 2 つの列をベース ビューに追加しましたscript_end_datedrug_classdistinctDrugsdrug_class

select distinct
 t.pat_id
,t.fill_date
,t.script_end_date
,t.drug_class
,t.distinctDrugs
,h3a.drug_class as h3aDrugClass
,h3a.distinctDrugs
from #temp as t
left join 
(
    select 
     pat_id
    ,fill_date
    ,script_end_date
    ,drug_class
    ,distinctDrugs
    from #temp 
    where drug_class='h3a'
) as h3a on h3a.pat_id=t.pat_id and h3a.fill_date between t.fill_date and t.script_end_date and t.drug_class !=h3a.drug_class
where h3a.drug_class is not null

残りのdrug_class列に対してこれを行うのは簡単ですが、これは非常に効率的ではありません。再帰を使用してこれをもっと簡単に行う方法はありますか (またはその点については他の方法)?

編集:これが私が探していたものの最終製品です:

select distinct 
 f.pat_id
,f.fill_date
,f.script_end_date
,case when h3a.drug_class is null then 'H3A' else 'H3A' end as H3A
,isnull(h3a.distinctDrugs,0) as h3aCounts
,case when h4b.drug_class is null then 'H4B' else 'H4B' end as H4B
,isnull(h4b.distinctDrugs,0) as h4bCounts
,case when h6h.drug_class is null then 'H6H' else 'H6H' end  as H6H
,isnull(h6h.distinctDrugs,0) as h6hCounts
,case when h2s.drug_class is null then 'H2S' else 'H2S' end as H2S 
,isnull(h2s.distinctDrugs,0) as h2sCounts
,case when h2e.drug_class is null then 'H2E' else 'H2E' end  as H2E
,isnull(h2e.distinctDrugs,0) as h2eCounts
,case when h2f.drug_class is null then 'H2F' else 'H2F' end as H2F
,isnull(h2f.distinctDrugs,0) as h2fCounts
,case when j7c.drug_class is null then 'J7C' else 'J7C' end  as J7C
,isnull(j7c.distinctDrugs,0) as j7cCounts
from familyStrata as f
left join 
(
    select
     pat_id
    ,drug_class
    ,distinctDrugs
    ,fill_date
    from familyStrata 
    where drug_class='h3a'
) as h3a on h3a.pat_id=f.pat_id and h3a.fill_date between f.fill_date and f.script_end_date
left join 
(
    select
     pat_id
    ,drug_class
    ,fill_date
    ,distinctDrugs
    from familyStrata
    where drug_class='h4b'
) as h4b on h4b.pat_id=f.pat_id and h4b.fill_date between f.fill_date and f.script_end_date
left join 
(
    select
     pat_id
    ,drug_class
    ,fill_date
    ,distinctDrugs
    from familyStrata
    where drug_class='h6h'
) as h6h on h6h.pat_id=f.pat_id and h6h.fill_date between f.fill_date and f.script_end_date
left join 
(
    select
     pat_id
    ,drug_class
    ,fill_date
    ,distinctDrugs
    from familyStrata
    where drug_class='h2f'
) as h2f on h2f.pat_id=f.pat_id and h2f.fill_date between f.fill_date and f.script_end_date
left join 
(
    select
     pat_id
    ,drug_class
    ,fill_date
    ,distinctDrugs
    from familyStrata
    where drug_class='h2s'
) as h2s on h2s.pat_id=f.pat_id and h2s.fill_date between f.fill_date and f.script_end_date
left join 
(
    select
     pat_id
    ,drug_class
    ,fill_date
    ,distinctDrugs
    from familyStrata
    where drug_class='h2e'
) as h2e on h2e.pat_id=f.pat_id and h2e.fill_date between f.fill_date and f.script_end_date
left join 
(
    select
     pat_id
    ,drug_class
    ,fill_date
    ,distinctDrugs
    from familyStrata
    where drug_class='j7c'
) as j7c on j7c.pat_id=f.pat_id and j7c.fill_date between f.fill_date and f.script_end_date

これは実際にはかなり高速ですが、リモートでエレガント/拡張可能というわけではありません。結果セットは次のようになります。 ここに画像の説明を入力

期間ごとに、異なる薬物ごとに、drug_class と distinctDrugs の数を確認できます。さて、この問題に対してこれよりもはるかに洗練された解決策はありますか?

4

2 に答える 2

0

したがって、再帰はこれを行うための最良の方法ではありません。で落ち着きました

select
 pat_id
,fill_date
,script_end_date
,'h3a' as h3a, coalesce(sum(case when drug_class='h3a' then distinctDrugs end),0) as h3aCounts
,'h4b' as h4b, coalesce(sum(case when drug_class='h4b' then distinctDrugs end),0) as h4bCounts
,'h6h' as h6h, coalesce(sum(case when drug_class='h6h' then distinctDrugs end),0) as h6hCounts
,'h2e' as h2e, coalesce(sum(case when drug_class='h2e' then distinctDrugs end),0) as h2eCounts
,'h3a' as h2f, coalesce(sum(case when drug_class='h2f' then distinctDrugs end),0) as h2fCounts
,'h3a' as h2s, coalesce(sum(case when drug_class='h2s' then distinctDrugs end),0) as h2sCounts
,'h3a' as j7c, coalesce(sum(case when drug_class='j7c' then distinctDrugs end),0) as j7cCounts
,row_number() over(order by pat_id) as rn
from x
group by pat_id,fill_date,script_end_date
)
于 2013-01-22T23:11:44.967 に答える
0

SQL サーバーでは、共通テーブル式 (CTE) を使用して再帰クエリを実行します。方法は次のとおりです。

http://msdn.microsoft.com/en-gb/library/ms186243(v=sql.105).aspx

于 2013-01-21T16:31:53.220 に答える