如何将由聚合函数确定的值设置为列

时间:2016-06-24 11:41:43

标签: sql oracle sql-update oracle-apex window-functions

我使用oracle Apex.I创建了一个列avg来设置它的值

UPDATE emp 
SET avg = SUM(price) 
OVER (PARTITION BY id)/COUNT(DISTINCT date) OVER(PARTITION BY id);

我已成功使用聚合函数生成了我想要的值,但我无法将其设置为Column.Suggest如何将查询结果设置为列。

1 个答案:

答案 0 :(得分:0)

将所有数据导入CTE并根据ID

在Emp表上更新

假设脚本:

在Oracle中:

    MERGE INTO EMP U
using (
 SELECT    ID
                , SUM(price) OVER (PARTITION BY id)/COUNT(DISTINCT date) OVER(PARTITION BY id) AS Rnum
        FROM    EMP
) S
on (u.id = s.id)
when matched then update set U.Avg = S.rnum

在SQL SERVER中

;WITH number AS (
        SELECT    ID
                , SUM(price) OVER (PARTITION BY id)/COUNT(DISTINCT date) OVER(PARTITION BY id) AS Avg
        FROM    EMP

    )
    UPDATE  t 
    SET     t.Avg = r.Avg
    FROM    Emp t
            JOIN number r ON t.ID = r.ID;