sql - CASE 语句和 GROUP BY 子句中的聚合函数错误
问题描述
我在更大的查询中使用了以下 CTE,并且根据我的分组方式收到了两条不同的错误消息。
我正在使用 Redash 并使用 Amazon Athena。我可以按tenant_id 分组,也可以按tenant_id
& 我的 case 语句分组"active"
。无论哪种方式,我都会收到错误消息。
active_billpay AS
(SELECT o.tenant_id as tenant_id, CASE WHEN o.created_date >= min(mbpc.created_date)
THEN true else false end as active
FROM reporting.t_order o
LEFT JOIN reporting.t_me_bill_pay_charge mbpc ON o.tenant_id = mbpc.tenant_id
WHERE o.retired_date is null
AND mbpc.retired_date is null
GROUP by 1),
如果我只按tenant_id 分组:
运行查询时出错:SYNTAX_ERROR: line 13:32: '(CASE WHEN ("o"."created_date" >= "min"("mbpc"."created_date")) THEN true ELSE false END)' 必须是聚合表达式或出现在 GROUP BY 子句中
如果我同时按tenant_id 和活动分组:
运行查询时出错:SYNTAX_ERROR:第 13:32 行:GROUP BY 子句不能包含聚合或窗口函数:["min"("mbpc"."created_date")]
先感谢您。
解决方案
我认为您只想通过tenant_id
and聚合created_date
:
SELECT o.tenant_id as tenant_id,
(CASE WHEN o.created_date >= MIN(mbpc.created_date) THEN true ELSE false
END) as active
FROM reporting.t_order o LEFT JOIN
reporting.t_me_bill_pay_charge mbpc
ON o.tenant_id = mbpc.tenant_id
where o.retired_date is null
and mbpc.retired_date is null
group by o.tenant_id, o.created_date
推荐阅读
- cucumber - cucumber-js 并行功能是否重用节点
- python - 如何在目录中的所有文件类型中搜索正则表达式
- zapier - 捆绑上下文和变量传播
- python - 如何在散景中使用“DataRange1D”?
- reporting-services - 在 Visual Studio 2015 中构建 rdl 正在从我的 xml 代码中删除换行符
- javascript - Matching Contents from One Array to Another And Returning the Filtered Array
- r - 通过 R 使用 System Shell 命令添加标头
- html - Not able to store dynamic FormArray in FormGroup
- python - Python 在不知道密钥名称的情况下下载 S3 文件
- python - Python - 使用 sum 函数尝试/排除