通过子查询计算

时间:2014-03-11 00:09:50

标签: sql oracle subquery

我对子查询有一点问题,并希望得到一些输入。我正在尝试汇总来自两个表的数据,我已经从财务表中获得了很好的聚合(孤立地),但我在索赔片上遇到了问题。它正在转换所有Cartesian,所以我尝试在第二个子查询中编写声明计算,但主要查询引用字段状态c.CLM_LN_PD_AMT不存在时出现问题。这是我将计算移动到第二个子查询之前的部分。 (在子查询中加上CLM_LN_PD,然后在主查询中调用c.clm_ln_pd。任何轻推都会很棒。

select sum(f.MED_ELIGTY_IND) as MEDMEMBERMONTHS,
       sum(f.PHARM_ELIGTY_IND) as RXMONTHS,
       avg(f.DIAG_CD_RSK_SCORE) as MEDRISK,
       avg(f.DIAG_CD_PHARM_RSK_SCORE) as PHARMRISK,
       count(distinct c.CLM_ID_TXT) as NUMCLAIMS,
       sum(c.CLM_LN_PD_AMT) as PAIDAMT,
       sum(c.CLM_LN_ALWD_AMT) as ALWDAMT,
       f.ELIGTY_MO_YYYYMM,
       f.SE_NAME
  from (select CTG_ID_TXT,
               MED_ELIGTY_IND,
               PHARM_ELIGTY_IND,
               DIAG_CD_RSK_SCORE,
               DIAG_CD_PHARM_RSK_SCORE,
               ELIGTY_MO_YYYYMM,
               SE_NAME
          from FINCL_ATTRBN_RESLT_PMPM
         Where to_char(ELIGTY_AS_OF_DT, 'mm/dd/yyyy') = '02/20/2014'
           and ELIGTY_MO_YYYYMM between '201301' and '201312'
           and CNTRCT_TYP_ID = '331929495'
           and ACTRL_LOB not in ('BOEI', 'SC_ASO')) f,
       (select distinct (CTG_ID), CLM_ID_TXT, CLM_LN_PD_AMT, CLM_LN_ALWD_AMT
          from CLM_MED_DETL_VW
         where to_char(CLM_LN_SVC_FROM_DT, 'yyyymm') between '201301' and
               '201312'
           and CLM_MED_DETL_VW.PD_PRD_YYYYMM between '201301' and '201312'
           and CLM_MED_DETL_VW.CLM_ADJ_PRD_YYYYMM is null) c
 where c.CTG_ID = f.CTG_ID_TXT
 Group by f.SE_NAME, f.ELIGTY_MO_YYYYMM
 order by f.SE_NAME, f.ELIGTY_MO_YYYYMM

0 个答案:

没有答案