sql - 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
解决方案
您的查询似乎比必要的复杂得多:
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
是不必要的。
推荐阅读
- java - 如何使用单独的方法通过剪辑停止音频
- aws-appsync - 在 AWS AppSync 中测试 Direct Lambda Resolver
- c++ - 为什么我缺少 C++20 标头?我该如何解决这个问题?
- r - 部署 Shiny 应用程序时出现问题。找不到“tidyverse”
- google-sheets - 加入标签时如何在谷歌表格中添加空白行?
- windows - 如何在 powershell 中安装 TDM-GCC 并打开 Mingw 命令提示符?
- javascript - 在字符串中查找模式
- django - Nginx 502. 网关错误,但使用 CORS。我应该在生产中使用 CORS 吗?
- javascript - 谜语:为什么 onEdit 只触发在我显然从未定义过的范围内进行的编辑?还是我?
- javascript - NodeJS 或 Browser JS Thrift 客户端消费者如何内省 Thrift 响应标头?