Sql 使用带有更新的CTE和聚合函数

Sql 使用带有更新的CTE和聚合函数,sql,postgresql,sql-update,common-table-expression,postgresql-10,Sql,Postgresql,Sql Update,Common Table Expression,Postgresql 10,在PostgreSQL 10.6中,我试图将两个聚合函数调用的结果存储到words\u users表的avg\u score和avg\u time列中,但不幸的是,得到了语法错误: WITH last_week_moves AS ( SELECT m.gid, m.uid, m.played - LAG(m.played) OVER(PARTITION BY m.gid ORDER BY played) AS dif

在PostgreSQL 10.6中,我试图将两个聚合函数调用的结果存储到
words\u users
表的
avg\u score
avg\u time
列中,但不幸的是,得到了语法错误:

WITH last_week_moves AS (
    SELECT
            m.gid,
            m.uid,
            m.played - LAG(m.played) OVER(PARTITION BY m.gid ORDER BY played) AS diff
    FROM words_moves m
    JOIN words_games g ON (m.gid = g.gid AND 5 IN (g.player1, g.player2))
    WHERE m.played > CURRENT_TIMESTAMP - INTERVAL '1 week'
)
UPDATE words_users SET
        avg_score = (SELECT ROUND(AVG(score), 1) FROM words_moves WHERE uid = 5),
        avg_time = TO_CHAR(AVG(diff), 'HH24:MI')
FROM last_week_moves
WHERE uid = 5
GROUP BY uid;
WITH last_week_moves AS (
                SELECT
                        m.gid,
                        m.uid,
                        TO_CHAR(AVG(m.played - LAG(m.played) OVER(PARTITION BY m.gid ORDER BY played)), 'HH24:MI') AS diff
                FROM words_moves m
                JOIN words_games g ON (m.gid = g.gid AND 5 IN (g.player1, g.player2))
                WHERE m.played > CURRENT_TIMESTAMP - INTERVAL '1 week'
                GROUP BY uid
)
UPDATE words_users SET
        avg_score = (SELECT ROUND(AVG(score), 1) FROM words_moves WHERE uid = 5),
        avg_time = diff
FROM last_week_moves
WHERE uid = 5;

ERROR:  42803: aggregate function calls cannot contain window function calls
LINE 5:                         TO_CHAR(AVG(m.played - LAG(m.played)...
                                                       ^
LOCATION:  check_agg_arguments_walker, parse_agg.c:728
(我在上面的语句中使用了硬编码的
uid=5
,但在现实生活中,后者被包装在PL/PgSQL存储函数中,并使用参数
uid=in_uid

数据库似乎不满意
分组依据
,但我需要它用于
AVG(diff)
,因为CTE总是为游戏中的两名玩家提供移动间隔时间:

SELECT
        m.gid,
        m.uid,
        m.played - LAG(m.played) OVER(PARTITION BY m.gid ORDER BY played) AS diff
FROM words_moves m
JOIN words_games g ON (m.gid = g.gid AND 5 IN (g.player1, g.player2))
WHERE m.played > CURRENT_TIMESTAMP - INTERVAL '1 week';

  gid  |  uid  |         diff
-------+-------+-----------------------
 50399 |   774 | ¤
 50608 |  8977 | ¤
 50608 |     5 | 00:39:48.121149
 50608 |  8977 | 00:09:46.221235
 50608 |     5 | 01:35:23.524209
 50608 |  8977 | 09:26:40.794061
 50697 |     5 | ¤
 50697 | 10322 | 02:13:16.502079
 50697 |     5 | 01:47:44.681788
 50697 | 10322 | 00:01:31.597973
 50697 |     5 | 12:11:24.54716
 50697 | 10322 | 12:01:15.078243
 50697 |     5 | 11:52:39.60056
 50697 | 10322 | 00:11:30.491137
 50697 |     5 | 00:14:53.612513
 50697 | 10322 | 01:45:23.940957
 ...
 52469 |     5 | 02:46:29.768655
 52469 |  8550 | 01:16:45.169882
 52469 |     5 | 08:38:00.691552
(59 rows)
有人知道如何更改我的
UPDATE
查询吗

以下是有关的3个表格:

 # \d words_users
                                         Table "public.words_users"
    Column     |           Type           | Collation | Nullable |                 Default
---------------+--------------------------+-----------+----------+------------------------------------------
 uid           | integer                  |           | not null | nextval('words_users_uid_seq'::regclass)
 created       | timestamp with time zone |           | not null |
 visited       | timestamp with time zone |           | not null |
 ip            | inet                     |           | not null |
 fcm           | text                     |           |          |
 apns          | text                     |           |          |
 adm           | text                     |           |          |
 motto         | text                     |           |          |
 vip_until     | timestamp with time zone |           |          |
 grand_until   | timestamp with time zone |           |          |
 banned_until  | timestamp with time zone |           |          |
 banned_reason | text                     |           |          |
 elo           | integer                  |           | not null |
 medals        | integer                  |           | not null |
 coins         | integer                  |           | not null |
 avg_score     | double precision         |           |          |
 avg_time      | text                     |           |          |
Indexes:
    "words_users_pkey" PRIMARY KEY, btree (uid)
Check constraints:
    "words_users_banned_reason_check" CHECK (length(banned_reason) > 0)
    "words_users_elo_check" CHECK (elo >= 0)
    "words_users_medals_check" CHECK (medals >= 0)
Referenced by:
    TABLE "words_chat" CONSTRAINT "words_chat_uid_fkey" FOREIGN KEY (uid) REFERENCES words_users(uid) ON DELETE CASCADE
    TABLE "words_games" CONSTRAINT "words_games_player1_fkey" FOREIGN KEY (player1) REFERENCES words_users(uid) ON DELETE CASCADE
    TABLE "words_games" CONSTRAINT "words_games_player2_fkey" FOREIGN KEY (player2) REFERENCES words_users(uid) ON DELETE CASCADE
    TABLE "words_moves" CONSTRAINT "words_moves_uid_fkey" FOREIGN KEY (uid) REFERENCES words_users(uid) ON DELETE CASCADE
    TABLE "words_reviews" CONSTRAINT "words_reviews_author_fkey" FOREIGN KEY (author) REFERENCES words_users(uid) ON DELETE CASCADE
    TABLE "words_reviews" CONSTRAINT "words_reviews_uid_fkey" FOREIGN KEY (uid) REFERENCES words_users(uid) ON DELETE CASCADE
    TABLE "words_scores" CONSTRAINT "words_scores_uid_fkey" FOREIGN KEY (uid) REFERENCES words_users(uid) ON DELETE CASCADE
    TABLE "words_social" CONSTRAINT "words_social_uid_fkey" FOREIGN KEY (uid) REFERENCES words_users(uid) ON DELETE CASCADE
    TABLE "words_stats" CONSTRAINT "words_stats_uid_fkey" FOREIGN KEY (uid) REFERENCES words_users(uid) ON DELETE CASCADE

# \d words_moves
                                      Table "public.words_moves"
 Column  |           Type           | Collation | Nullable |                 Default
---------+--------------------------+-----------+----------+------------------------------------------
 mid     | bigint                   |           | not null | nextval('words_moves_mid_seq'::regclass)
 action  | text                     |           | not null |
 gid     | integer                  |           | not null |
 uid     | integer                  |           | not null |
 played  | timestamp with time zone |           | not null |
 tiles   | jsonb                    |           |          |
 score   | integer                  |           |          |
 letters | text                     |           |          |
 hand    | text                     |           |          |
 puzzle  | boolean                  |           | not null | false
Indexes:
    "words_moves_pkey" PRIMARY KEY, btree (mid)
    "words_moves_gid_played_idx" btree (gid, played DESC)
    "words_moves_uid_idx" btree (uid)
Check constraints:
    "words_moves_score_check" CHECK (score >= 0)
Foreign-key constraints:
    "words_moves_gid_fkey" FOREIGN KEY (gid) REFERENCES words_games(gid) ON DELETE CASCADE
    "words_moves_uid_fkey" FOREIGN KEY (uid) REFERENCES words_users(uid) ON DELETE CASCADE
Referenced by:
    TABLE "words_scores" CONSTRAINT "words_scores_mid_fkey" FOREIGN KEY (mid) REFERENCES words_moves(mid) ON DELETE CASCADE

# \d words_games
                                      Table "public.words_games"
  Column  |           Type           | Collation | Nullable |                 Default
----------+--------------------------+-----------+----------+------------------------------------------
 gid      | integer                  |           | not null | nextval('words_games_gid_seq'::regclass)
 created  | timestamp with time zone |           | not null |
 finished | timestamp with time zone |           |          |
 player1  | integer                  |           | not null |
 player2  | integer                  |           |          |
 played1  | timestamp with time zone |           |          |
 played2  | timestamp with time zone |           |          |
 state1   | text                     |           |          |
 state2   | text                     |           |          |
 reason   | text                     |           |          |
 hint1    | text                     |           |          |
 hint2    | text                     |           |          |
 score1   | integer                  |           | not null |
 score2   | integer                  |           | not null |
 chat1    | integer                  |           | not null |
 chat2    | integer                  |           | not null |
 hand1    | character(1)[]           |           | not null |
 hand2    | character(1)[]           |           | not null |
 pile     | character(1)[]           |           | not null |
 letters  | character(1)[]           |           | not null |
 values   | integer[]                |           | not null |
 bid      | integer                  |           | not null |
 friendly | boolean                  |           |          |
Indexes:
    "words_games_pkey" PRIMARY KEY, btree (gid)
    "words_games_player1_coalesce_idx" btree (player1, COALESCE(finished, 'infinity'::timestamp with time zone))
    "words_games_player2_coalesce_idx" btree (player2, COALESCE(finished, 'infinity'::timestamp with time zone))
Check constraints:
    "words_games_chat1_check" CHECK (chat1 >= 0)
    "words_games_chat2_check" CHECK (chat2 >= 0)
    "words_games_check" CHECK (player1 <> player2)
    "words_games_score1_check" CHECK (score1 >= 0)
    "words_games_score2_check" CHECK (score2 >= 0)
Foreign-key constraints:
    "words_games_bid_fkey" FOREIGN KEY (bid) REFERENCES words_boards(bid) ON DELETE CASCADE
    "words_games_player1_fkey" FOREIGN KEY (player1) REFERENCES words_users(uid) ON DELETE CASCADE
    "words_games_player2_fkey" FOREIGN KEY (player2) REFERENCES words_users(uid) ON DELETE CASCADE
Referenced by:
    TABLE "words_chat" CONSTRAINT "words_chat_gid_fkey" FOREIGN KEY (gid) REFERENCES words_games(gid) ON DELETE CASCADE
    TABLE "words_moves" CONSTRAINT "words_moves_gid_fkey" FOREIGN KEY (gid) REFERENCES words_games(gid) ON DELETE CASCADE
    TABLE "words_scores" CONSTRAINT "words_scores_gid_fkey" FOREIGN KEY (gid) REFERENCES words_games(gid) ON DELETE CASCADE
你似乎想要:

WITH last_week_moves AS (
      SELECT m.uid,
             (MAX(m.played) - MIN(m.played)) / NULLIF(COUNT(*) - 1, 0) as avg_diff,
             AVG(score) as avg_score
      FROM words_moves m JOIN
           words_games g
           ON m.gid = g.gid AND 5 IN (g.player1, g.player2)
      WHERE m.played > CURRENT_TIMESTAMP - INTERVAL '1 week'
      GROUP BY m.uid
     )
UPDATE words_users wu
    SET avg_score = lwm.avg_score,
        avg_time = TO_CHAR(avg_diff, 'HH24:MI')
FROM last_week_moves lwm
WHERE wu.uid = lwm.uid AND
      wu.uid = 5;
请注意,这简化了
diff
计算,因此不需要
lag()

您可以看到这些是等效的: 价值差异 1. 4 3 九五

差异的平均值明显为4。这是
((4-1)+(9-4))/2
。您可以看到“4”被取消,因此它实际上是
(9-1)/2
。这一观察概括了。

您似乎想要:

WITH last_week_moves AS (
      SELECT m.uid,
             (MAX(m.played) - MIN(m.played)) / NULLIF(COUNT(*) - 1, 0) as avg_diff,
             AVG(score) as avg_score
      FROM words_moves m JOIN
           words_games g
           ON m.gid = g.gid AND 5 IN (g.player1, g.player2)
      WHERE m.played > CURRENT_TIMESTAMP - INTERVAL '1 week'
      GROUP BY m.uid
     )
UPDATE words_users wu
    SET avg_score = lwm.avg_score,
        avg_time = TO_CHAR(avg_diff, 'HH24:MI')
FROM last_week_moves lwm
WHERE wu.uid = lwm.uid AND
      wu.uid = 5;
请注意,这简化了
diff
计算,因此不需要
lag()

您可以看到这些是等效的: 价值差异 1. 4 3 九五


差异的平均值明显为4。这是
((4-1)+(9-4))/2
。您可以看到“4”被取消,因此它实际上是
(9-1)/2
。这一观察概括了。

不确定语法,但由于uid位于WHERE,您至少不需要group by。
group by
UPDATE
语句中无效。那么,如何使用
AVG
结果为
UPDATE
提供数据?为什么不直接计算CTE中的平均值<代码>上周移动为(选择平均值(差异),从()更新…?不幸的是,这会产生另一个语法错误:聚合函数调用不能包含窗口函数调用。请查看更新的问题不确定语法,但由于uid位于WHERE,您至少不需要group by。
group by
更新
语句中无效。如何向上馈送
日期
平均值
结果一起?为什么不直接计算CTE中的平均值?
与上周移动为(选择平均值(差异),从()更新…
?不幸的是,这会产生另一个语法错误:聚合函数调用不能包含窗口函数调用请查看更新的问题我很惊讶您的代码如何产生与我的
滞后相同的值-使用代码…您能解释
(MAX(m.play)-MIN(m.play))/NULLIF(COUNT(*)-1,0吗
trick,它是如何工作的?我很惊讶你的代码如何产生与我的
LAG
相同的值-使用代码…你能解释一下
(MAX(m.played)-MIN(m.played))/NULLIF(COUNT(*)-1,0)
trick,它是如何工作的吗?