分组问题(Teradata SQL)

时间:2012-08-17 13:17:35

标签: sql teradata

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

当我将“,current_date - pmnt_rlse_dt”添加为“付款后的天数”时,我开始收到此错误

我知道这与小组有关,但我还没有弄明白。

    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

2 个答案:

答案 0 :(得分: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表达需要一点时间才能完全理解,但它非常强大。

答案 1 :(得分:0)

你没有group by子句。

您只能使用聚合函数,例如max和在分组数据时使用