列“”必须出现在GROUP BY子句中或在聚合函数中使用

时间:2019-04-01 19:08:40

标签: sql sequelize.js

我正在尝试运行一个sql查询,该查询连接了一些其他表以填充结果,而且似乎也无法按照我想要的方式使其工作。对SQL来说也是相对较新的

这是我当前的工作查询:

SELECT
    app_step ->> 'appID' AS "App ID",
    count(tasks.task_id) AS "Total Tasks Started",
    count(tasks.failed_reason_detail) AS "Fix Task Count",
    sum(case when tasks.status='running' THEN 1 else 0 end) AS "Running Count",
    avg(tasks.start_date - tasks.end_date) AS "Average Duration" 
FROM tasks
JOIN teams
    ON tasks.team_name = teams.team_name
WHERE
        teams.is_internal_team = false AND
        tasks.start_date between 
            (SELECT TIMESTAMP 'yesterday' AT TIME ZONE 'America/Chicago') AND 
            (SELECT TIMESTAMP 'today' AT TIME ZONE 'America/Chicago') AND
        (app_step IS NOT NULL)
GROUP BY app_step ->> 'appID'
ORDER BY count(app_step ->> 'appID') DESC;

填充的结果如下:

                    App ID                     | Total Tasks Started | Fix Task Count | Running Count | Average Duration 
-----------------------------------------------+---------------------+----------------+---------------+------------------
 pushbot/calculate/v2                          |               42450 |              0 |             0 | -00:00:00.279624
 pushbot/rename-run/v1                         |               40907 |              0 |             0 | -00:00:00.356639

我正在尝试将另一个表中的列添加到结果中 completed_task_app_rollup_by_day.count AS "Completion Count",

SELECT
    app_step ->> 'appID' AS "App ID",
    count(tasks.task_id) AS "Total Tasks Started",
    completed_task_app_rollup_by_day.count AS "Completion Count",
    count(tasks.failed_reason_detail) AS "Fix Task Count",
    sum(case when tasks.status='running' THEN 1 else 0 end) AS "Running Count",
    avg(tasks.start_date - tasks.end_date) AS "Average Duration" 
FROM tasks
JOIN completed_task_app_rollup_by_day
    ON tasks.app_step ->> 'appID' = completed_task_app_rollup_by_day.app_id
JOIN teams
    ON tasks.team_name = teams.team_name
WHERE
        teams.is_internal_team = false AND
        completed_task_app_rollup_by_day.date between 
            (SELECT TIMESTAMP 'yesterday' AT TIME ZONE 'America/Chicago') AND 
            (SELECT TIMESTAMP 'today' AT TIME ZONE 'America/Chicago') AND
        tasks.start_date between 
            (SELECT TIMESTAMP 'yesterday' AT TIME ZONE 'America/Chicago') AND 
            (SELECT TIMESTAMP 'today' AT TIME ZONE 'America/Chicago') AND
        (app_step IS NOT NULL)
GROUP BY app_step ->> 'appID'
ORDER BY count(app_step ->> 'appID') DESC;

哪个产生:column "completed_task_app_rollup_by_day.count" must appear in the GROUP BY clause or be used in an aggregate function

当我将列添加到GROUP BY时-我找不到所需的结果:

EX:

                    App ID                     | Total Tasks Started | Completion Count | Fix Task Count | Running Count | Average Duration 
-----------------------------------------------+---------------------+------------------+----------------+---------------+------------------
 pushbot/rename-run/v1                         |                8612 |                2 |              0 |             0 | -00:00:00.356639
 pushbot/rename-run/v1                         |                6459 |                1 |              0 |             0 | -00:00:00.356639
 email/send/v2                                 |                4470 |                1 |              0 |             0 | -00:00:01.69917
 email/send/v2                                 |                3576 |                2 |              0 |             0 | -00:00:01.69917

注意app_id部分不是唯一的

任何想法如何在我的查询中包括此列而不按该列分组。我试过多次子查询,但是没有运气。谢谢大家

0 个答案:

没有答案