如何使用OVER PARTITION BY改善SQL Teradata

时间:2019-02-22 17:36:50

标签: sql teradata

我有一个包含超过7列的查询,其中包含一些counts()和sum()值,我非常慢,然后我尝试使用OVER PARTITION BY,但是在结果表中出现错误“ Selected non-汇总值必须是关联组的一部分”。

这是查询:

insert into  SQUEMA.TABLE_B
select 
SUBSTR(cast(date as char(6)) ,1,4) ||'-'|| SUBSTR(cast(date as char(6)) ,5,6) as date ,
column1,
column2,
column3,
.
.
.
.
column7,
count(column8) OVER (PARTITION BY date, column1,...,column7) as column8
SUM(column9) OVER(ORDER BY date ) as column9
from  SQUEMA.TABLE_A tb 
where tb.date between '201711' and '201812'
group by date, column1,...,column7;

错误

  

选定的非聚合值必须是关联组的一部分

1 个答案:

答案 0 :(得分:1)

我相信你想要

sum(count(column8)) OVER (PARTITION BY date, column1,...,column7) as column8
sum(sum(column9))) OVER (ORDER BY date ) as column9

这是将窗口函数用于聚合的方式。