CASE 语句和 GROUP BY 子句中的聚合函数出错
Error with aggregate function in a CASE statement and GROUP BY clause
我在更大的查询中使用了以下 CTE,并且根据我的分组方式收到了两条不同的错误消息。
我正在使用 Redash 并使用 Amazon Athena。我可以按 tenant_id 分组,或者我可以按 tenant_id
和名为 "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 分组:
Error running query: SYNTAX_ERROR: line 13:32: '(CASE WHEN
("o"."created_date" >= "min"("mbpc"."created_date")) THEN true ELSE
false END)' must be an aggregate expression or appear in GROUP BY
clause
如果我同时按 tenant_id 和活动进行分组:
Error running query: SYNTAX_ERROR: line 13:32: GROUP BY clause cannot
contain aggregations or window functions:
["min"("mbpc"."created_date")]
提前致谢。
我认为您只想按 tenant_id
和 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
为了应用像 min
这样的聚合函数,SQL 要求您非常具体地说明聚合适用于哪些数据集。即使 SQL 允许您编写的查询,您仍然只会获得每一行的最小值 created_date
,而不是每一行 tenant_id
.
为了实现我认为您正在尝试做的事情,您应该使用 sub-query 为每个 tenant_id
获取最小值 created_date
,然后使用该值告知你的 active
字段。
SELECT o.tenant_id AS tenant_id,
CASE WHEN o.created_date >= min_created_date THEN TRUE ELSE FALSE END AS active
FROM reporting.t_order o
LEFT JOIN
(SELECT tenant_id, MIN (created_date) AS min_created_date
FROM reporting.t_me_bill_pay_charge
WHERE retired_date IS NULL) mbpc
ON o.tenant_id = mbpc.tenant_id
WHERE o.retired_date IS NULL
一般来说,如果您发现自己试图通过 group by 1
之类的操作来欺骗 SQL 语法要求,则强烈表明您的方法存在缺陷。
我在更大的查询中使用了以下 CTE,并且根据我的分组方式收到了两条不同的错误消息。
我正在使用 Redash 并使用 Amazon Athena。我可以按 tenant_id 分组,或者我可以按 tenant_id
和名为 "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 分组:
Error running query: SYNTAX_ERROR: line 13:32: '(CASE WHEN ("o"."created_date" >= "min"("mbpc"."created_date")) THEN true ELSE false END)' must be an aggregate expression or appear in GROUP BY clause
如果我同时按 tenant_id 和活动进行分组:
Error running query: SYNTAX_ERROR: line 13:32: GROUP BY clause cannot contain aggregations or window functions: ["min"("mbpc"."created_date")]
提前致谢。
我认为您只想按 tenant_id
和 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
为了应用像 min
这样的聚合函数,SQL 要求您非常具体地说明聚合适用于哪些数据集。即使 SQL 允许您编写的查询,您仍然只会获得每一行的最小值 created_date
,而不是每一行 tenant_id
.
为了实现我认为您正在尝试做的事情,您应该使用 sub-query 为每个 tenant_id
获取最小值 created_date
,然后使用该值告知你的 active
字段。
SELECT o.tenant_id AS tenant_id,
CASE WHEN o.created_date >= min_created_date THEN TRUE ELSE FALSE END AS active
FROM reporting.t_order o
LEFT JOIN
(SELECT tenant_id, MIN (created_date) AS min_created_date
FROM reporting.t_me_bill_pay_charge
WHERE retired_date IS NULL) mbpc
ON o.tenant_id = mbpc.tenant_id
WHERE o.retired_date IS NULL
一般来说,如果您发现自己试图通过 group by 1
之类的操作来欺骗 SQL 语法要求,则强烈表明您的方法存在缺陷。