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(); 其中一个ts_code 存在多个lend_rate,报错Expression 'ztd.lend_rate' is not being grouped
时间: 2024-04-15 12:30:04 浏览: 142
在你提供的代码中,报错是由于你在SELECT子句中引用了一个未在GROUP BY子句中进行分组的列 ztd.lend_rate。Flink SQL要求在SELECT子句中引用的列必须要么进行聚合,要么在GROUP BY子句中进行分组。
为了解决这个问题,你可以在GROUP BY子句中添加 ztd.lend_rate 列,或者将其包装在聚合函数中。以下是修改后的代码示例:
```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, " +
" 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, ztd.lend_rate" +
") t " +
"GROUP BY id, data_cal_date, stock_block_code, stock_block_name";
tEnv.executeSql(sql).print();
```
在这个修改后的代码中,我们将 ztd.lend_rate 列添加到了 GROUP BY 子句中,以满足 Flink SQL 的要求。或者,你也可以使用 SUM 聚合函数来对 ztd.lend_rate 进行聚合,这样就不需要将其添加到 GROUP BY 子句中。
请确保根据你的业务需求选择适当的解决方案。
阅读全文