Partition by in Impala SQL 抛出错误

Partition by in Impala SQL throwing an error

我正在尝试使用 TOAD

计算 Impala 月份的 运行 总损失

以下查询抛出错误 -select 列表表达式不是由聚合输出生成的(从 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

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

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() 是一个 window 函数。