MySQL 错误 1111 - 嵌套 window 函数时组函数的使用无效

MySQL Error 1111 - Invalid use of group function when nesting window functions

我正在努力创建关于 answers table 的 SQL 报告:

id | created_at
1  | 2018-03-02 18:05:56
2  | 2018-04-02 18:05:56
3  | 2018-04-02 18:05:56
4  | 2018-05-02 18:05:56
5  | 2018-06-02 18:05:56

输出为:

weeks_ago | record_count (# of rows per weekly cohort) | growth (%)
-4        | 21                                         |  22%
-3        | 22                                         | -12%
-2        | 32                                         |   2%
-1        |  2                                         |  20%
 0        | 31                                         |   0%

我的查询当前出错:

1111 - Invalid use of group function

我做错了什么?

SELECT  floor(datediff(f.created_at, curdate()) / 7) AS weeks_ago,
                count(DISTINCT f.id) AS "New Records in Cohort",
                100 * (count(*) - lag(count(*), 1) over (order by f.created_at)) / lag(count(*), 1) over (order by f.created_at) || '%' as growth
FROM answers f
WHERE f.completed_at IS NOT NULL
GROUP BY weeks_ago
HAVING count(*) > 1;

你不能使用lag包含COUNT聚合函数,因为当你使用聚合函数包含聚合函数时它是无效的。

你可以尝试使用子查询来实现。

SELECT weeks_ago,
       NewRecords "New Records in Cohort",
      100 * (cnt - lag(cnt, 1) over (order by created_at)) / lag(cnt, 1) over (order by created_at) || '%' as growth
FROM (
    SELECT floor(datediff(f.created_at, curdate()) / 7) AS weeks_ago, 
           COUNT(*) over(partition by weeks_ago order by weeks_ago) cnt,
           count(DISTINCT f.id) NewRecords,
           f.created_at
    FROM answers f
) t1

我想你想找到 运行 所有行的计数 排除 当前行。我认为您可以按如下方式放弃 LAG 函数:

SELECT
    COUNT(*) OVER (ORDER BY f.created_at ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) x, -- running count before current row
    COUNT(*) OVER (ORDER BY f.created_at) y -- running count including current row

你可以随心所欲地除法和乘法。


没有。你只需要分开 GROUP BYLAG OVER:

WITH cte AS (
    SELECT
        FLOOR(DATEDIFF(created_at, CURDATE()) / 7) AS weeks_ago,
        COUNT(DISTINCT id) AS new_records
    FROM answers
    WHERE 1 = 1 -- todo: change this
    GROUP BY weeks_ago
    HAVING 1 = 1 -- todo: change this
)
SELECT
    cte.*,
    100 * (
        new_records - LAG(new_records) OVER (ORDER BY weeks_ago)
    ) / LAG(new_records) OVER (ORDER BY weeks_ago) AS percent_increase
FROM cte

Fiddle