不是 "Group By" 表达式

Not a "Group By" expression

当我在 oracle运行 中尝试 运行 这个脚本时,我遇到了 ORA-00979: not a GROUP BY expression 00979. 00000 - "not a GROUP BY expression"

      SELECT m.working_minutes as minutes, sum(at.length_in_minutes)
  FROM medic m 
  inner join appointment a on a.medic_id = m.id
  inner join appointment_type at on at.id = a.appointment_type_id
  group by m.id
  HAVING TO_DATE(a.appointmet_date, 'dd/mm/yyyy') = TO_DATE(sysdate, 'dd/mm/yyyy');

但是当我删除 group by m.id 然后 运行 脚本时

      SELECT m.working_minutes as minutes, sum(at.length_in_minutes)
  FROM medic m 
  inner join appointment a on a.medic_id = m.id
  inner join appointment_type at on at.id = a.appointment_type_id

它给我这个错误:

    ORA-00937: not a single-group group function
    00937. 00000 -  "not a single-group group function"

所以,我不知道该怎么办

您想在 GROUP BY 子句中包括您不作为聚合依据的列,并且您的 HAVING 子句应该是在聚合之前应用的 WHERE 子句。

SELECT m.working_minutes as minutes,
       sum(at.length_in_minutes)
FROM   medic m 
       INNER JOIN appointment a on a.medic_id = m.id
       INNER JOIN appointment_type at on at.id = a.appointment_type_id
WHERE  a.appointmet_date >= TRUNC(SYSDATE)
AND    a.appointmet_date <  TRUNC(SYSDATE) + INTERVAL '1' DAY
GROUP BY
       -- m.id,
       m.working_minutes;

此外,切勿在已经是 DATE 的值上使用 TO_DATE


从你的描述来看,你可能想要的是:

SELECT m.id,
       MAX(m.working_minutes) as minutes,
       COALESCE(SUM(at.length_in_minutes), 0) AS time_worked
FROM   medic m 
       LEFT OUTER JOIN appointment a
       ON (   a.medic_id = m.id
          AND a.appointmet_date >= TRUNC(SYSDATE)
          AND a.appointmet_date <  TRUNC(SYSDATE) + INTERVAL '1' DAY)
       LEFT OUTER JOIN appointment_type at
       ON (at.id = a.appointment_type_id)
GROUP BY
       m.id;

db<>fiddle here