如何获得Sum(Column)结束(按其他列划分)?

时间:2019-03-21 10:55:37

标签: hive

我正在尝试转换如下编写的Teradata代码

Select A.col1, sum(A.metric1) over (partition by A.col1, B.col1 order by   
  A.col2 asc) as Cust_col, B.col1 from A JOIN B on (A.join_key=B.join_key) 
  where A.col3='X' QUALIFY ROW_NUMBER () OVER (PARTITION BY A.col1,B.COL1
  ORDER BY A.col3 DESC) = 1

在Hive中:

Select C.col1,C.cust_col,C.col1,ROW_NUMBER () OVER (PARTITION BY A.col1,C.COL1 
  ORDER BY C.col3 DESC) as Row_num from (Select A.col1, sum(A.metric1) over 
  (partition by A.col1, B.col1 order by A.col2 asc) as Cust_col,B.col1 from A 
  JOIN B on (A.join_key=B.join_key) where A.col3='X') C where C.Row_num =1

但是,我遇到类似

的错误
  

SemanticException无法将窗口调用分解为组。   至少一组必须仅取决于输入列。同时检查   循环依赖。潜在错误:Primitve类型DATE不正确   值边界表达式中支持

我知道这是因为使用Sum(A.metric1)分区在这里造成了问题,但是如何解决呢?

1 个答案:

答案 0 :(得分:0)

select a_col1,
       sum(metric1) over (partition by a_col1, b_col1 order by a_col2 asc) as Cust_col, 
       b_col1
from
(
Select A.metric1, A.col1 a_col1, B.COL1 b_col1, A.col2 a_col2
       ROW_NUMBER () OVER (PARTITION BY A.col1,B.COL1 ORDER BY A.col3 DESC ) as rn
from A JOIN B on (A.join_key=B.join_key) 
  where A.col3='X' 

) s 
where rn=1