0

谁能向我解释为什么 dLoad 是 GROUP BY 行中的无效标识符?这在 Mysql 上运行良好,但我无法让它与 Oracle 一起使用。

CREATE OR REPLACE VIEW DoctorsLoad AS
    SELECT dID, gender, specialty, 'Overloaded' AS dLoad
    FROM Doctor D, Examine E
    WHERE D.dID = E.doctor_id
    GROUP BY dID, gender, specialty, dLoad
    HAVING COUNT(*) > 10
    UNION
    SELECT dID, gender, specialty, 'Underloaded' AS dLoad
    FROM Doctor D, Examine E
    WHERE D.dID = E.doctor_id
    GROUP BY dID, gender, specialty, dLoad
    HAVING COUNT(*) <= 10;
4

1 回答 1

3

使用 Oracle RDMS,您不能在GROUP BY子句中使用别名,但对于像 'Overloaded' 这样的文字,您无论如何都不需要将其包含在组中。

CREATE OR REPLACE VIEW DoctorsLoad AS
    SELECT dID, gender, specialty, 'Overloaded' AS dLoad
    FROM Doctor D, Examine E
    WHERE D.dID = E.doctor_id
    GROUP BY dID, gender, specialty
    HAVING COUNT(*) > 10
    UNION ALL
    SELECT dID, gender, specialty, 'Underloaded' AS dLoad
    FROM Doctor D, Examine E
    WHERE D.dID = E.doctor_id
    GROUP BY dID, gender, specialty
    HAVING COUNT(*) <= 10;

我还建议使用UNION ALL避免过滤操作来删除重复行的 a 。

于 2013-02-16T17:17:10.430 回答