其中ID为参数集而无其他位置(1行数据)

时间:2016-12-13 22:29:26

标签: sql sql-server row

使用Sql Server: 我想找到这个ID(MrHCClaim_id)在这组标准中的位置,但不属于任何其他标准。基本上找到只有一行数据的ID。

我试过了:

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)

1 个答案:

答案 0 :(得分: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值的结果。