SQL 过滤器小部件必须出现在 GROUP BY 子句中或用于聚合函数中

SQL filter widgets Must appear in the GROUP BY clause or be used in an aggregate function

我正在尝试在元数据库上创建过滤器小部件。我的 {{breakdown}} 过滤器有问题,当我输入 'year' 而不是 {{breakdown}} 时,它可以完美地为我提供年度细分报告。但是当我使用 {{breakdown}} 然后在小部件中输入年份时,我得到了错误:

column "analytics_moduleattemptanalyticsview.created" must appear in the GROUP BY clause or be used in an aggregate function Position: 27

有谁知道如何解决这个问题?我想把它作为一个小部件,这样我也可以看到月份和季度

 SELECT date_trunc({{breakdown}}, CAST("public"."analytics_moduleattemptanalyticsview"."created" AS timestamp)) AS "created", 
    "public"."analytics_moduleattemptanalyticsview"."is_complete" AS "is_complete", 
    count(*) AS "count"
    FROM "public"."analytics_moduleattemptanalyticsview"
    LEFT JOIN "public"."auth_user" "User" ON "public"."analytics_moduleattemptanalyticsview"."user_id" = "User"."id" 
    LEFT JOIN "public"."companies_company" "Companies Company" ON ((floor((("public"."analytics_moduleattemptanalyticsview"."user_company_id" - 0.0) / 2000)) * 2000) + 0.0) = "Companies Company"."id"
    WHERE "User"."is_staff" = FALSE
    [[and {{company_id}} and {{company_type}} and {{platform}}]]
    GROUP BY date_trunc({{breakdown}}, CAST("public"."analytics_moduleattemptanalyticsview"."created" AS timestamp)), "public"."analytics_moduleattemptanalyticsview"."is_complete"
    ORDER BY date_trunc({{breakdown}}, CAST("public"."analytics_moduleattemptanalyticsview"."created" AS timestamp)) ASC, "public"."analytics_moduleattemptanalyticsview"."is_complete" ASC

您可以将此查询写得非常简单易读:

SELECT 
    date_trunc({{breakdown}} as brk_down, 
    CAST(t1."created" AS timestamp)) AS created, 
    t1.is_complete, 
    count(*) AS counts 
FROM "public"."analytics_moduleattemptanalyticsview" t1
LEFT JOIN "public"."auth_user" us ON t1.user_id = us.id 
LEFT JOIN "public"."companies_company" cc ON ((floor(((t1."user_company_id" - 0.0) / 2000)) * 2000) + 0.0) = cc.id
WHERE us.is_staff = FALSE
[[and {{company_id}} and {{company_type}} and {{platform}}]]
GROUP BY brk_down, created, t1.is_complete
ORDER BY brk_down, created ASC, t1.is_complete asc