【问题标题】:Partition by in Impala SQL throwing an errorImpala SQL中的分区引发错误
【发布时间】:2018-10-16 12:18:53
【问题描述】:

我正在尝试使用 TOAD 计算 Impala 上几个月的总损失

以下查询抛出错误 -选择列表表达式不是由聚合输出生成的(从 group by 子句中丢失)

select
segment,
year(open_dt) as open_year,
months,
sum(balance)
sum(loss) over (PARTITION by segment,year(open_dt) order by months) as NCL 
from

tableperf
where
year(open_dt) between 2015 and 2018

group by 1,2,3

【问题讨论】:

  • 您应该包括样本数据和所需的结果。

标签: sql group-by window-functions impala


【解决方案1】:

您正在混合聚合和窗口函数。我想你可能想要:

select segment, year(open_dt) as open_year, months,
       sum(balance)
       sum(sum(loss)) over (PARTITION by segment, year(open_dt) order by months) as NCL 
from tableperf
where year(open_dt) between 2015 and 2018
group by 1, 2, 3;

这会计算每年的累计损失。注意sum(sum(loss)) 的使用。内部sum() 是一个聚合函数。外层sum()是一个窗口函数。

【讨论】:

  • @Gordon Linoff,我在哪里可以找到有关混合使用聚合和窗口函数的语法的文档?
  • @lovechillcool 。 . .我其实不知道。语法约定一起工作,窗口函数在聚合之后进行评估。
猜你喜欢
  • 2012-02-01
  • 1970-01-01
  • 2022-01-12
  • 1970-01-01
  • 2020-10-05
  • 1970-01-01
  • 2018-03-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多