window函数调用需要一个OVER子句

时间:2015-07-28 10:00:16

标签: sql postgresql

我的SQL查询如下所示:

UPDATE people p
SET p.email = (SELECT 
                 MAX(FIRST_VALUE(email)) OVER (PARTITION BY person_id ORDER BY updated_at DESC)
               FROM person_emails pe
               WHERE pe.person_id = p.id);

它返回错误:

 window function call requires an OVER clause

为什么?

编辑:

我已将查询更新为以下内容:

UPDATE people p
  SET p.email = (
    SELECT MAX(
      FIRST_VALUE(email) OVER (PARTITION BY person_id ORDER BY updated_at DESC)
    )  
    FROM person_emails pe WHERE pe.person_id = p.id
  );

现在它返回:

ERROR:  aggregate function calls cannot contain window function calls
LINE 4:       FIRST_VALUE(email) OVER (PARTITION BY person_id ORDER ...
              ^

1 个答案:

答案 0 :(得分:3)

尝试:

UPDATE people p
  SET p.email = (
    SELECT MAX(first_email) 
    FROM (
     SELECT FIRST_VALUE(email) AS first_email, person_id OVER (PARTITION BY person_id ORDER BY updated_at DESC)
        FROM person_emails pe
    )  AS q
  WHERE q.person_id = p.id
  );