【问题标题】:Impala SQL QueryImpala SQL 查询
【发布时间】:2020-12-21 01:46:06
【问题描述】:

错误信息:

选择列表表达式不是由聚合输出生成的(缺少 来自 GROUP BY 子句?): CASE WHEN (flag = 1) THEN date_add(lead_ctxdt, -1) ELSE ctx_date END lot_endt

代码:

select c.enrolid, c.ctx_date, c.ctx_regimen, c.lead_ctx, c.lead_ctxdt, min(c.ctx_date) as lot_stdt, 
case when (flag = 1 ) then date_add(lead_ctxdt, -1) 
else ctx_date
end as lot_endt
from
(
    select p.*, 
    case when (ctx_regimen <> lead_ctx) then 1 
    else 0
    end as flag
    from
    (
        select a.*, lead(a.ctx_regimen, 1) over(partition by enrolid order by ctx_date) as lead_ctx, 
        lead(ctx_date, 1) over (partition by enrolid order by ctx_date) as lead_ctxdt
        from 
        (
            select enrolid, ctx_date, group_concat(distinct ctx_codes) as ctx_regimen
            from lotinfo 
            where ctx_date between ctx_date and date_add(ctx_date, 5)
            group by enrolid, ctx_date
        ) as a
   ) as p
) as c
group by c.enrolid, c.ctx_date, c.ctx_regimen, c.lead_ctx, c.lead_ctxdt

我想得到lead_ctx日期减一作为标志为1的日期

【问题讨论】:

  • 取决于你想要什么,尝试应用聚合函数:MIN/MAX(case when (flag = 1 ) then date_add(lead_ctxdt, -1) else ctx_date end)
  • @dnoeth; 我想得到lead_ctx 日期减一作为标志为1 的日期。我想你在这里确实有答案。可能,每组只有一行启用了该标志。
  • 这看起来像是一个有趣的查询,可能与间隙和孤岛问题有关。如果您要以表格文本的形式提供示例数据和所需结果,则可以进一步优化它。

标签: sql impala group-concat hue case-when


【解决方案1】:

所以我通过执行几次小改动找到了答案。让我告诉你,当你试图在同一个查询中有 group_conact 的同时尝试最小或最大时,在 Impala 中这不起作用。您必须在每一个子查询和外部查询中的某些内容的 min() 中编写两个查询,反之亦然。

感谢@dnoeth 让我明白我已经有了答案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多