String sql = "select id,data_cal_date,stock_block_code,stock_block_name,block_ref_compact_bal from(" + "SELECT UUID() as id,'" + lastTD + "' as data_cal_date," + "biv.stock_block_code," + "biv.stock_block_name," + "sum (ztd.lend_rate * ztd.lend_num * twp.close_price / sum(ztd.lend_num * twp.close_price)) as block_ref_compact_bal " + "from bid_info_view biv " + "left join zrq_trade_detail ztd on " + "biv.ts_code = ztd.ts_code " + "left join temp_wind_price twp " + "on biv.ts_code = twp.ts_code " + "group by biv.stock_block_code,biv.stock_block_name " + ")"; tEnv.executeSql(sql).print(); 报错SQL validation failed. From line 1, column 178 to line 1, column 267: Aggregate expressions cannot be nested
时间: 2024-04-15 18:29:55 浏览: 152
在你提供的代码中,报错是由于在聚合函数中嵌套了另一个聚合函数导致的。在 Flink SQL 中,聚合函数不能嵌套使用。
具体来说,你使用了两个聚合函数:`SUM(ztd.lend_rate * ztd.lend_num * twp.close_price)` 和 `SUM(ztd.lend_num * twp.close_price)`。这是不允许的。
为了解决这个问题,你可以将第一个聚合函数的计算结果提取为一个子查询,然后在外部查询中使用该结果进行计算。以下是修改后的代码示例:
```java
String sql = "SELECT id, data_cal_date, stock_block_code, stock_block_name, " +
"SUM(block_ref_compact_bal) AS block_ref_compact_bal " +
"FROM (" +
" SELECT UUID() AS id, '" + lastTD + "' AS data_cal_date, " +
" biv.stock_block_code, " +
" biv.stock_block_name, " +
" ztd.lend_rate * ztd.lend_num * twp.close_price / SUM(ztd.lend_num * twp.close_price) AS block_ref_compact_bal " +
" FROM bid_info_view biv " +
" LEFT JOIN zrq_trade_detail ztd ON biv.ts_code = ztd.ts_code " +
" LEFT JOIN temp_wind_price twp ON biv.ts_code = twp.ts_code " +
" GROUP BY biv.stock_block_code, biv.stock_block_name" +
") t " +
"GROUP BY id, data_cal_date, stock_block_code, stock_block_name";
tEnv.executeSql(sql).print();
```
在这个修改后的代码中,我们将第一个聚合函数的计算结果提取为子查询,并在外部查询中进行了第二个聚合函数的计算。这样就避免了聚合函数嵌套的问题。
请注意,如果你的 Flink 版本较低,可能不支持将聚合函数用于聚合结果的情况。在这种情况下,你可能需要将聚合函数的计算逻辑拆分为多个步骤,在中间步骤使用临时表或视图来保存部分结果。
阅读全文