PARTITION BY表达式引用列sku既不是分组也不是聚合

时间:2018-05-22 19:45:36

标签: sql google-bigquery window-functions

我正在尝试执行此查询

select
rank() over (partition by substr(upper(sku),1,19) order by round(avg(inpf.amount),2) desc) as ranking from erp.input_factor  inpf

我一直收到错误: PARTITION BY表达式引用列SKU既未在[5:40]分组也未汇总

当然,我不希望将sku添加到group,这就是为什么它在over子句中。这里看起来怎么了?

实际查询:

select sq.fab_id , sq.sku as sku
from
(
select upper(inpf.fab_id) as fab_id, substr(upper(sku),1,19) as sku ,round(avg(inpf.amount),2) as amount,
  rank() over (partition by substr(upper(sku),1,19) order by round(avg(inpf.amount),2) desc) as ranking ,
fi.main_construction as construct from erp.input_factor  inpf
left join
erp.fabric_information fi
on upper(inpf.fab_id) = upper(fi.fab_id)

where length(inpf.fab_id) > 3
group by inpf.fab_id , substr(upper(sku),1,19) , fi.main_construction
) sq
where (sq.construct = 1)

2 个答案:

答案 0 :(得分:2)

这是BigQuery的特点。一个简单的解决方案是子查询:

select sq.fab_id , sq.sku as sku
from (select upper(inpf.fab_id) as fab_id, sku19 as sku, 
             round(avg(inpf.amount),2) as amount,
             rank() over (partition by sku19 order by inpf.amount desc) as ranking,
            fi.main_construction as construct
      from erp.input_factor inpf left join
           (select fi.*, substr(upper(sku), 1, 19) as sku19
            from erp.fabric_information fi
           ) fi
           on upper(inpf.fab_id) = upper(fi.fab_id)
      where length(inpf.fab_id) > 3
      group by inpf.fab_id, sku19 , fi.main_construction
     ) sq
where sq.construct = 1;

注意:你没有注意到sku的来源,所以我猜对了。

答案 1 :(得分:0)

最好提供一些源表描述和数据样本。

我只是猜测问题出在onConnectorder by round(avg(inpf.amount),2) desc是聚合函数,您既没有Avg()子句,也没有group by是唯一的列。