错误 [HY000] 错误:聚合函数调用不能嵌套 */
Posted
技术标签:
【中文标题】错误 [HY000] 错误:聚合函数调用不能嵌套 */【英文标题】:ERROR [HY000] ERROR: Aggregate function calls may not be nested */ 【发布时间】:2017-05-22 01:50:43 【问题描述】:添加到我的查询时,我无法使以下行正常工作,有什么想法吗? stddev(stat) 偏差
with c as (
SELECT A.ENGINE_ASOF_CALENDAR_DATE, A.LEVEL_1_CODE CURR, CNTR_TO_ACTIVE_RISK, A.PRICE, a.ACTIVE_WEIGHT_PCT,
lag(a.PRICE, 1) over(partition by a.LEVEL_1_CODE order by a.ENGINE_ASOF_CALENDAR_DATE) price_lag,
lag(CNTR_TO_ACTIVE_RISK, 1) over(partition by a.LEVEL_1_CODE order by a.ENGINE_ASOF_CALENDAR_DATE) risk_lag,
price_lag/a.PRICE - 1 rtn,
a.ACTIVE_WEIGHT_PCT * rtn wgt_rtn
FROM DBS_APPL_RISK_DATAMART.USR_OWNR_RISK_DATAMART.VWC_FOREIGNEXCHANGE_FUND_EXPOSURE A
WHERE A.PORTFOLIO_CODE = 'Sunsuper Active - SUKHH3_Active'
)
SELECT c.*,
sum(wgt_rtn) over(partition by c.ENGINE_ASOF_CALENDAR_DATE)sum_rtn,
sum(risk_lag) over(partition by c.ENGINE_ASOF_CALENDAR_DATE)sum_risk_lag,
sum_risk_lag/sqrt(260) over(partition by c.ENGINE_ASOF_CALENDAR_DATE)sum_lag_risk2,
sum_rtn/nullif(sum_lag_risk2,0) stat,
stddev(stat) bias
FROM c
order by c.ENGINE_ASOF_CALENDAR_DATE desc
【问题讨论】:
【参考方案1】:您在定义它们的同一选择中的表达式中使用列别名。您还在没有 group by
的 select
中混合聚合函数。
也许这会解决你的问题:
WITH . . .
SELECT c.*, sum_rtn / nullif(sum_lag_risk2, 0) as stat,
stddev(sum_rtn / nullif(sum_lag_risk2, 0)) over () as bias
FROM (SELECT c.*,
sum(wgt_rtn) over (partition by c.ENGINE_ASOF_CALENDAR_DATE) as sum_rtn,
sum(risk_lag) over (partition by c.ENGINE_ASOF_CALENDAR_DATE) as sum_risk_lag,
sum_risk_lag/sqrt(260) over (partition by c.ENGINE_ASOF_CALENDAR_DATE) as sum_lag_risk2
FROM c
) c
ORDER BY c.ENGINE_ASOF_CALENDAR_DATE desc
我不确定标准差应该按什么划分。这是所有数据。
【讨论】:
感谢 Gordon,这在 c.ENGINE_ASOF_CALENDAR_DATE 分区时有效。 有没有办法让标准差数字(偏差)滞后 30 天? @TimCooper。 . .我建议你再问一个问题。如果您只关注bias
,请尽量简化查询。以上是关于错误 [HY000] 错误:聚合函数调用不能嵌套 */的主要内容,如果未能解决你的问题,请参考以下文章
SQLSTATE[HY000]: 一般错误: 1111 无效使用组函数 (SQL: select GROUP_CONCAT(sum(documents.grand_total) SEPARATOR &
SQLSTATE[HY000]:一般错误:1215 无法在表用户中添加外键约束 Laravel
mysql错误:错误1018(HY000):无法读取'.'的目录(错误号:13)