需要协助查询逻辑

时间:2016-03-14 14:13:38

标签: sql join temp-tables

我会尽量保持这一点。我尝试了几种方法,但总是卡住了。所以我的问题是,收集以下数据的最佳途径是什么?

逻辑: 如果提供商和运营商是地区'CI'中的状态LP,CP,PP,那么同一提供商和运营商应该在地区'UI'中处于状态'OP'。

上述逻辑将使用相同的表。这是一个错误报告。因此,当区域'CI'中有LP,CP,PP时,我需要显示区域'UI'中提供者/运营商组合不是'OP'的错误。如果他们拥有'CI'中的组合,他们就不需要'OP'记录。但是,如果他们确实在“CI”中有记录,那么我需要检查他们是否在“UI”中有记录。如果他们这样做,它必须是状态'OP'。

这是我根据你的逻辑使用的查询。我不确定这是否正确,因为我没有运行测试查询。

Select distinct aff.aff_no, aff.prov_no, IRS_No,prog_no, Last_name,     First_name + ' ' + Last_name as Provider_Name, prov.CONTROL_NO, office_no, aff.Carrier, aff.[Region], aff.pay_class, aff.status, aff.spec_1, aff.op_no, aff.hat_code, aff.area, aff.period_table_no, pat_to as pay_to, aff.medicare_no, aff. eff_date, aff.end_date, aff.trans_date, 'Status/Region Error' As [Rept Error], ACTIVE_PATIENTS as member_cnt
from  amisys.dbo.Provider Prov  

INNER JOIN amisys.dbo.Affiliation Aff
on Prov.Prov_no = Aff.Prov_No
where aff.end_date = '12/31/9999'
and aff.void = ' '
and PAY_CLASS <> 'dummy'
and irs_no = '721269878'
and status in ('PP','LP','CP')
and region = 'CI'
and HAT_CODE = 'SP'
and EXISTS(Select 1 from amisys.dbo.Affiliation aff2
       Where aff.prov_no = aff2.PROV_NO
          and aff.carrier = aff2.carrier
          and aff2.region = 'UI'
          and aff2.status = 'OP')

如果我走在正确的轨道上,有人可以帮我完成吗?如果没有,有人可以告诉我这应该如何编码吗?

非常感谢, 格雷格

1 个答案:

答案 0 :(得分:0)

如果我理解正确,并且您希望让所有成员_no同时拥有(country =&#39; A&#39;,status =&#39; CI&#39;)和(country =&#39; ; B&#39;,status =&#39; OP&#39;)那么你根本不需要使用临时表,它可以通过很多方式完成,其中一个是EXISTS:< / p>

SELECT m.member_No, m.Code_No, m.Status, m.County, m.City, m.State, m.Zip
FROM dbo.member m
WHERE m.county = 'B' and
      m.Status = 'OP' and
      EXISTS(select 1 from dbo.member m2
             where m.member_no = m2.member_no
                and m.code_no = m2.code_no
                and m2.country = 'A'
                and m2.status = 'CI')

您也可以使用join:

    SELECT m.member_No, m.Code_No, m.Status, m.County, m.City, m.State, m.Zip
    FROM dbo.member m
    INNER JOIN dbo.member m2
     ON(m.member_no = m2.member_no and m.code_no = m2.code_no
        and m.country = 'B' and m.status = 'OP'
        and m2.country = 'A' and m2.status = 'CI')
相关问题