Teradata - 案例陈述中的Nest Aggregate操作

时间:2015-07-15 14:46:50

标签: teradata case-statement

我收到错误"无法嵌套聚合操作"在下面尝试运行脚本时。我搜索了类似标题的其他问题,但他们没有解决这个问题。

我试图对一个时期的数据求和,将同一时期的其他数据相加,然后从另一个中减去一个数据。看起来在我脑海里如此简单!有人可以帮忙吗?

SELECT 
SUM(CASE WHEN a.post_ts(DATE) between 1140921 and 1140927 THEN (pnt_amt(DEC (16,0)))   
- sum ((dlr_val*1000)*-1)END) Week 34 
FROM DB.T1 b  
,DB.T2 a 
LEFT JOIN DB.T3 c ON A.OID = C.OID 
WHERE a.lid = b.lid 

或者这个:

Sel
SUM(CASE WHEN a.post_ts(DATE) between 1140921 and 1140927 THEN (pnt_amt(DEC (16,0)))  
-  SUM(CASE WHEN a.post_ts(DATE) between 1140921 and 1140927 THEN sum ((dlr_val*1000)*-1)END)week_34
 FROM DB.T1 b  
,DB.T2 a 
LEFT JOIN DB.T3 c ON A.OID = C.OID 
WHERE a.lid = b.lid 

1 个答案:

答案 0 :(得分:2)

我不确切地知道你要做什么,但它可能是这样的:

SELECT 
SUM(CASE WHEN a.post_ts(DATE) BETWEEN 1140921 AND 1140927 THEN (pnt_amt(DEC (16,0))) END  
- SUM((dlr_val*1000)*-1)) Week 34 
FROM DB.T1 b  
,DB.T2 a 
LEFT JOIN DB.T3 c ON A.OID = C.OID 
WHERE a.lid = b.lid 

SEL
SUM(CASE WHEN a.post_ts(DATE) BETWEEN 1140921 AND 1140927 THEN (pnt_amt(DEC (16,0))) END) 
-  SUM(CASE WHEN a.post_ts(DATE) BETWEEN 1140202 AND 1140208 THEN (dlr_val*1000)*-1 END)week_34
 FROM DB.T1 b  
,DB.T2 a 
LEFT JOIN DB.T3 c ON A.OID = C.OID 
WHERE a.lid = b.lid 

顺便说一下,您可以简单地使用dlr_val*1000)*-1而不是-dlr_val*1000),而不是DATE '2014-02-02',而不是1140202,而不是res/values-xlarge <dimen name="widget_min">196dp</dimen> res/values-large <dimen name="widget_min">144dp</dimen> res/values-normal <dimen name="widget_min">96dp</dimen> res/values-small <dimen name="widget_min">72dp</dimen> ,这会更容易理解。