0

我收到错误“选定的非聚合值必须是关联组的一部分”

当我添加行“,current_date - pmnt_rlse_dt as “Days since Paid”时,我开始收到此错误

我知道这与 group by 有关,但我还没有弄清楚。

    select ee.alias_last_nm || ', ' || ee.alias_first_nm as "Custodian"
   ,clm.i_sys_clm
   ,clm.c_clm as "Claim Number"
   ,clm.i_pol as "Policy Number"
   ,clm.n_lst as "Last Name"
   ,clm.n_fst as "First Name"
   ,clm.c_sta_clm as "Status"
   ,mx_dt as "Last Payment Date"

from ltc_p.vltc_clm_mo clm

left join (select indiv_sorce_syst_cd
            ,sorce_upc_indiv_id
            ,alias_last_nm
            ,alias_first_nm
from edw_p.upc_indiv_alias_v2
where row_end_dt = '9999-12-31' and
indiv_sorce_syst_cd = 'ORG') ee
on clm.c_ams_clm = ee.sorce_upc_indiv_id

inner join(select sorce_claim_id
            ,max(pmnt_rlse_dt) as mx_dt
            ,current_date - pmnt_rlse_dt as "Days Since Paid"
from ltc_p.claim_pmnt pp

having  max(pmnt_rlse_dt) is not null) as d
on clm.i_sys_clm = d.sorce_claim_id

where clm.c_sta_clm = 'AC'
order by 1,2
4

2 回答 2

1

您的错误消息与MAX在派生表查询中使用该函数d而不使用 GROUP BY 子句有关。

但是,我猜目的是返回每个 sorce_id 的最大 pmnt_rlse_dt。如果是这样,请尝试使用 OLAP 窗口函数:

inner join(
   select  sorce_claim_id
         , pmnt_rlse_dt   as mx_dt
         , current_date - pmnt_rlse_dt as "Days Since Paid" 
   from    ltc_p.claim_pmnt
   where   pmnt_rlse_dt is not null
   qualify row_number() over (partition by sorce_claim_id
                              order by pmnt_rlse_dt desc) = 1
   ) as d 

QUALIFY表达式需要一点时间才能完全理解,但它非常强大。

于 2012-08-17T14:03:36.277 回答
0

您没有按子句分组。

在对数据进行分组时,您只能使用 max 和 have 等聚合函数

于 2012-08-17T13:20:02.120 回答