首页 > 解决方案 > Teradata:选定的非聚合值必须是相关组的一部分......我正在选择所有非聚合字段

问题描述

这真让我抓狂。有人可以帮我弄清楚为什么我不断收到“非聚合值必须是关联组的一部分”错误吗?这真的没有意义。我不能对窗口函数进行分组,所以只有两个非聚合的选定字段是前两个。内部查询完全没问题,错误只是在我运行整个过程时发生。我还尝试使用实际的字段名称与数字顺序,我得到了相同的错误,就好像我应该按其他东西分组一样。

select
end_of_week, 
deal_id, 
sum(sum_nob) as sum_nob, 
ROW_NUMBER() OVER(ORDER BY sum_nob desc) AS row_num
from ( 
    select
    td_week_end (report_date) as end_of_week, 
    report_date, 
    deal_id, 
    sum (nob) as sum_nob
    from sandbox.rev_mgmt_deal_funnel df
    left join (
        select 
        deal_uuid  
        from sandbox.sup_analytics_deal_counts_final) s on df.deal_id = s.deal_uuid 

    where s.deal_uuid is null
    and grt_l3 = 'L3 - High End'
    and report_date >= '2019-10-01'
    group by end_of_week, 
    report_date, 
    deal_id) a
group by 1,2

标签: sqlteradataaggregate-functions

解决方案


您的查询似乎比必要的复杂得多:

select td_week_end(report_date) as end_of_week, 
       deal_id, 
       sum(nob) as sum_nob,
       row_number() over (order by sum(nob)) as row_num
from sandbox.rev_mgmt_deal_funnel df left join
     sandbox.sup_analytics_deal_counts_final s
     on df.deal_id = s.deal_uuid 
where s.deal_uuid is null and
      grt_l3 = 'L3 - High End' and
      report_date >= '2019-10-01'
group by 1, 2;

笔记:

  • 不需要子查询。
  • 无需聚合两次。
  • 的子查询left join是不必要的。

推荐阅读