3

我正在尝试为每个返回每个author_id,author_nameAVG(total)per 。我正在尝试将,和合并到数组中。我知道此查询将返回每个数组,但这很好。authorarticle_groupauthor_idauthor_nameAVG(total)article_group

我最初尝试将AVG(total)(而不是avg_total)放入我的array_agg(). 这导致了一条错误消息,指出我不能嵌套聚合函数

我一直试图找出解决方法,但似乎无法解决。WHERE我尝试在子句中放置一个子查询AS avg_total,但没有奏效。

所以现在我尝试将AS avg_total别名放在子句之前的独立子查询中 FROM,但它仍然无法正常工作。

这是查询:

        SELECT b.article_group_id, b.article_group,
        array_agg('[' || c.author_id || ',' || c.author_name || ',' || avg_total || ']'),
        AVG((SELECT total
        FROM article f
        LEFT JOIN article_to_author w ON f.article_id = w.article_id
        LEFT JOIN author v ON w.author_id = c.author_id 
        LEFT JOIN grade z ON f.article_id = z.article_id
        ) AS avg_total)
        
        FROM article f
        LEFT JOIN article_group b ON b.article_group_id = f.article_group_id 
        LEFT JOIN article_to_author w ON f.article_id = w.article_id
        LEFT JOIN author c ON w.author_id = c.author_id 
        GROUP BY b.article_group_id, b. article_group
        

这是当前的错误消息:

    { error: syntax error at or near "AS"
    at Connection.parseE (Z:\GitFolder\roqq\server\node_modules\pg\lib\connection.js:614:13)
    at Connection.parseMessage (Z:\GitFolder\roqq\server\node_modules\pg\lib\connection.js:413:19)
    at Socket.<anonymous> (Z:\GitFolder\roqq\server\node_modules\pg\lib\connection.js:129:22)
    at Socket.emit (events.js:198:13)
    at Socket.EventEmitter.emit (domain.js:448:20)
    at addChunk (_stream_readable.js:288:12)
    at readableAddChunk (_stream_readable.js:269:11)
    at Socket.Readable.push (_stream_readable.js:224:10)
    at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
  name: 'error',
  length: 92,
  severity: 'ERROR',
  code: '42601',
  detail: undefined,
  hint: undefined,
  position: '430',
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'scan.l',
  line: '1149',
  routine: 'scanner_yyerror' }

这是我的表:

  CREATE TABLE article(
    article_id          SERIAL PRIMARY KEY,
    article_title       VARCHAR (2100),
    article_group_id    INTEGER 
);

    CREATE TABLE article_to_author(
    ata_id      SERIAL PRIMARY KEY,
    article_id  INTEGER,
    author_id   INTEGER
);

    CREATE TABLE author(
    author_id   SERIAL PRIMARY KEY,
    author_name VARCHAR(500)
);

    CREATE TABLE grade(
    grade_id       SERIAL PRIMARY KEY,
    detail         INTEGER,
    s_g            INTEGER,
    total          INTEGER,
    article_id     INTEGER
);

    CREATE TABLE article_group(
    article_group_id SERIAL PRIMARY KEY,
    article_group    VARCHAR(2100)
);
4

2 回答 2

3

在你的问题中,很多事情都不清楚。根据我对您当前查询的了解,试试这个:

with cte as (
  SELECT ag.article_group_id,
         ag.article_group,
         au.author_id, 
         au.author_name, 
         avg(gr.total) as avg_total
    FROM article_group ag
         LEFT JOIN article ar on ar.article_group_id=ag.article_group_id
         LEFT JOIN article_to_author ata ON ar.article_id = ata.article_id
         LEFT JOIN author au ON ata.author_id = au.author_id 
         LEFT JOIN grade gr ON ar.article_id = gr.article_id
   GROUP BY ag.article_group_id, ag.article_group, au.author_id, au.author_name
)
SELECT article_group_id, 
       article_group,
       array_agg('[' || author_id || ',' || author_name || ',' || avg_total || ']')
  FROM cte 
 GROUP BY article_group_id, article_group

你可以改变任何你想要的array_agg

演示

于 2020-08-10T05:41:56.330 回答
0

我认为这是两个层次的聚合。这使您可以准确地计算总体平均值:

SELECT article_group_id, array_agg( (author_id, author_name, avg_grade)) as authors,
       SUM(total_grade) / SUM(num_grade) as group_avg
FROM (SELECT ag.article_group_id, au.author_id, au.author_name,
             AVG(gr.total) as avg_grade,
             SUM(gr.total) as total_grade,
             COUNT(gr.total) as num_grade
      FROM article_group ag LEFT JOIN
           article ar
           ON ar.article_group_id=ag.article_group_id LEFT JOIN
           article_to_author ata
           ON ar.article_id = ata.article_id LEFT JOIN
           author au
           ON ata.author_id = au.author_id LEFT JOIN
           grade gr
           ON ar.article_id = gr.article_id
      GROUP BY ag.article_group_id, au.author_id, au.author_name
     ) a
GROUP BY article_group_id;

请注意,这会将作者聚合为数组而不是字符串。当然,如果您愿意,也可以使用字符串,但数组通常更简洁、用途更广泛。

于 2020-08-10T14:44:56.150 回答