在标量子查询中使用窗口函数时,需要确保正确使用窗口规范来定义函数的范围。以下是修改代码的示例:
原始代码:
SELECT column1, (SELECT SUM(column2) OVER () FROM table2 WHERE table1.column1 = table2.column1) AS sum_column2 FROM table1
修改后的代码:
SELECT column1, (SELECT SUM(column2) OVER () FROM table2 WHERE table1.column1 = table2.column1 WINDOW w AS (ORDER BY column2)) AS sum_column2 FROM table1
在子查询中添加窗口规范“w”,以确保窗口函数在正确的范围内计算。