2016-12-13 10 views
0

Sql Serverを使用しています: このID(MrHCClaim_id)がこの一連の基準に含まれている場所を探していますが、 。基本的に、データの行が1つしかないIDを探します。ここでIDはパラメータのセットであり、その他の場所はありません(1行のデータ)

私が試してみました

select MrHcClaim_id, HospCd, PyrNm, payor, PatNo, SvDate, 
    sum(case when HcPcS in ('99211', '99212', '99213', '99214', '99215') 
       and SvMod like '%25%' 
      then cast(units as decimal) else 0 end) as "E&M25s", 
    sum(cast (units as decimal)) as "units" 

from MrHcClaim as a 
    left join MrHcClaimDtl as c 
    on a.MrHcClaim_id = c.MrHcClaimDtl_id 

where SvDate between '11/01/2016' 
       and '11/30/2016' 
    and payor = '1' 
    and (billstatus = 't' or billstatus = 'p') 
    and hospcd <> 'saltzer' 

group by MrHcClaim_id, HospCd, PyrNm, payor, PatNo, SvDate 
having sum(case when HcPcS in ('99211', '99212', '99213', '99214', '99215') 
        and svmod like '%25%' 
       then cast(units as decimal) else 0 end) > 0 
     and sum(cast(units as decimal)) = sum(case when hcpcs in 
        ('99211', '99212', '99213', '99214', '99215') 
     and svmod like '%25%' then units else 0 end) 
+0

サンプルデータと期待される出力が役立ちます – artm

答えて

0

を私はあなたのHAVING句に別の条件を追加したいと考えている:

select MrHcClaim_id, HospCd, PyrNm, payor, PatNo, SvDate, 
    sum(case when HcPcS in ('99211', '99212', '99213', '99214', '99215') 
       and SvMod like '%25%' 
      then cast(units as decimal) else 0 end) as "E&M25s", 
    sum(cast (units as decimal)) as "units" 

from MrHcClaim as a 
    left join MrHcClaimDtl as c 
    on a.MrHcClaim_id = c.MrHcClaimDtl_id 

where SvDate between '11/01/2016' 
       and '11/30/2016' 
    and payor = '1' 
    and (billstatus = 't' or billstatus = 'p') 
    and hospcd <> 'saltzer' 

group by MrHcClaim_id, HospCd, PyrNm, payor, PatNo, SvDate 
having sum(case when HcPcS in ('99211', '99212', '99213', '99214', '99215') 
        and svmod like '%25%' 
       then cast(units as decimal) else 0 end) > 0 
     and sum(cast(units as decimal)) = sum(case when hcpcs in 
        ('99211', '99212', '99213', '99214', '99215') 
     and svmod like '%25%' then units else 0 end) 
     and COUNT(DISTINCT MrHcClaim_id) = 1 

これはあなたの唯一の単一のレコードがあるだけの結果が得られますMrHcClaim_idの値。

関連する問題