使用分组依据进行查看会导致AVG别名上的标识符无效

时间:2018-11-05 22:46:47

标签: sql oracle

Oracle说我有一个无效的标识符,我看过其他解决方案说要封装在一个子查询中,但到目前为止我还无法解决。我在想联接可能与它有关。

我试图创建一个显示供应商名称的视图,并为每个供应商提供创建发票和付款之间的平均时间(每个供应商有多张发票),但前提是该平均值大于或。等于1.5。然后,我将列表按此平均值降序排列。

CREATE OR REPLACE VIEW Vend_Date_Avgs AS
SELECT V.Vendor_Name, AVG(I.Invoice_Due_Date - I.Invoice_Date) AS Avg_Between
FROM Vendors V, Invoices I
WHERE V.Vendor_ID = I.Vendor_ID
GROUP BY V.Vendor_Name
HAVING Avg_Between >= 1.5
ORDER BY Avg_Between DESC;

任何帮助,我们将不胜感激!

1 个答案:

答案 0 :(得分:0)

Oracle在having子句中无法识别列别名:

CREATE OR REPLACE VIEW Vend_Date_Avgs AS
    SELECT V.Vendor_Name, AVG(I.Invoice_Due_Date - I.Invoice_Date) AS Avg_Between
    FROM Vendors V JOIN
         Invoices I
         ON V.Vendor_ID = I.Vendor_ID
    GROUP BY V.Vendor_Name
    HAVING AVG(I.Invoice_Due_Date - I.Invoice_Date) >= 1.5
    ORDER BY AVG(I.Invoice_Due_Date - I.Invoice_Date) DESC;

请注意使用正确的JOIN语法。