0

i've two table :

prgm[id,name]
notes[id,note,id_prgm]

I actually have a request to know some info about prgm :

SELECT p.id, name, AVG(note), COUNT(note)
FROM notes n
    JOIN prgm p
    ON p.id=n.id_prgm
GROUP BY p.id

And that work as expected, but now i want to know the MAX of AVG(note) and of COUNT(note), But if i do MAX(COUNT(note)) it return me an error : Invalid use of group function. I understand why there is this problem, but I don't know how to fix it.

The only way I see, is to do two subrequest and put it inside MAX(), but is there a better way than two subrequest ?

(I'm on MySql with php)

4

3 回答 3

5

最简单的答案是,

SELECT  p.id, name, AVG(note), COUNT(note) totalCount
FROM    notes n
        INNER JOIN prgm p
            ON p.id=n.id_prgm
GROUP   BY p.id
ORDER   BY totalCount DESC
LIMIT   1

但缺点是它不关心具有最高计数的重复记录。另一种选择是,

SELECT  p.id, name, AVG(note), COUNT(note)
FROM    notes n
        INNER JOIN prgm p
            ON p.id=n.id_prgm
GROUP   BY p.id
HAVING  COUNT(note) =
        (
            SELECT  DISTINCT COUNT(note) totalCount
            FROM    notes n
                    INNER JOIN prgm p
                        ON p.id=n.id_prgm
            GROUP   BY p.id 
            ORDER   BY totalCount DESC
            LIMIT   1
        )
于 2013-04-21T15:56:19.113 回答
1

order by您可以使用and对任一列执行此操作limit

SELECT p.id, name, AVG(note), COUNT(note)
FROM notes n
    JOIN prgm p
    ON p.id=n.id_prgm
GROUP BY p.id
order by avg(note) desc
limit 1

要同时执行这两项操作,需要一些子查询。我会这样做:

select t.id, t.name, t.avgnote, t.countnote
from (select max(avgnote) as maxavg, max(countnote) as maxcount
      from (SELECT p.id, name, AVG(note) as avgnote, COUNT(note) as countnote
            FROM notes n
                 JOIN prgm p
                 ON p.id=n.id_prgm
            GROUP BY p.id
           ) t
     ) themax join
     (SELECT p.id, name, AVG(note) as avgnote, COUNT(note) as countnote
      FROM notes n
           JOIN prgm p
           ON p.id=n.id_prgm
      GROUP BY p.id
     ) t
     on t.avgnote = themax.avgnote or t.countnote = themax.countnote
于 2013-04-21T15:58:18.120 回答
0

这是包含一些测试数据的数据库(与 Access、MySQL 和 Oracle 兼容):

CREATE TABLE notes (
  id int NOT NULL,
  note int NOT NULL,
  id_prgm int NOT NULL
);

INSERT INTO notes (id, note, id_prgm) VALUES (1, 4, 1);
INSERT INTO notes (id, note, id_prgm) VALUES (3, 16, 1);
INSERT INTO notes (id, note, id_prgm) VALUES (4, 3, 2);
INSERT INTO notes (id, note, id_prgm) VALUES (5, 4, 2);
INSERT INTO notes (id, note, id_prgm) VALUES (6, 5, 2);

ALTER TABLE notes
 ADD PRIMARY KEY (id);

CREATE TABLE prgm (
  id int NOT NULL,
  name varchar(255) NOT NULL
);

INSERT INTO prgm (id, name) VALUES (0, 'program 1');
INSERT INTO prgm (id, name) VALUES (1, 'program 1');
INSERT INTO prgm (id, name) VALUES (2, 'program 2');

ALTER TABLE prgm
 ADD PRIMARY KEY (id);

您的查询:

SELECT p.id, name, AVG(note), COUNT(note)
FROM notes n
    JOIN prgm p
    ON p.id=n.id_prgm
GROUP BY p.id

返回这些记录:

id  name        AVG(note)   COUNT(note)     
1   program 1   10.0000     2
2   program 2   4.0000      3

如果您只想要 AVG 和 COUNT 列的 MAX,请使用此查询:

select max(avg_note), max(count_note)
from (
  SELECT AVG(note) as avg_note, COUNT(note) as count_note
  FROM notes n, prgm p 
  WHERE p.id=n.id_prgm
  GROUP BY p.id
) temptable

此查询与 MySQL 不兼容,但也与 Access 和 Oracle 兼容,因为我在 FROM table1、table2 WHERE 中更改了 JOIN ON .. 并且我删除了 SELECT 中未包含在 GROUP BY 中的name字段。

由于性能/内存原因,我还删除了 SELECT 中不必要的字段p.id 。

查询将返回:

max(avg_note)   max(count_note)     
10.0000         3
于 2015-07-12T13:27:32.107 回答