调试此查询时,您很可能会遇到多个(不同的)错误消息。另外:在没有表/数据的情况下编写查询可能有点棘手,因为某些方面很容易被忽略。因此,我们将使用一些测试表(包括数据),请参阅dbfiddle here。
下面的查询代码将向您显示您可能会看到的各种错误 - 以及修复它们所需的 SQL。阅读代码中所有的cmets。
关系模型(缩短 id 列名)
ORA-00903:无效的表名
在您的原始查询中,您使用了一个名为 ORDER 的表。 ORDER 是保留字,请参阅documentation。命名表时避免使用它。
ORA-00937:不是单组函数
当使用有效的表名时,我们可以执行查询。在 WHERE 子句中添加一个条件(见下文)。由于 SELECT 和 ORDER BY 子句中的 SUM(),我们收到错误消息。显然,我们需要在查询中添加一个 GROUP BY 子句(这会立即导致下一条错误消息...)
SELECT
c.fn AS "First Name",
c.email AS "Email",
p.productName AS "Product Name",
SUM(p.unitsInStock) + SUM(p.unitsonorder) AS "Total Units" --<- ORA-00937
FROM customer c
JOIN orders o ON c.id = o.id -- table name fixed
JOIN orderLine l ON o.id = l.orderid
JOIN product p ON l.productCode = p.code
WHERE p.DISCONTINUED = 'N' --<- condition
ORDER BY SUM(p.unitsInStock) + SUM(p.unitsonorder) --<- ORA-00937
;
-- ORA-00937: not a single-group group function
--
-- Cause: A SELECT list cannot include both a group function,
-- such as AVG, COUNT, MAX, MIN, SUM, STDDEV, or VARIANCE,
-- and an individual column expression,
-- unless the individual column expression is included in a GROUP BY clause.
-- see:
-- https://docs.oracle.com/cd/B10501_01/server.920/a96525/e900.htm
ORA-00979:不是 GROUP BY 表达式
SELECT
c.fn AS "First Name",
c.email AS "Email",
p.productName AS "Product Name",
SUM(p.unitsInStock) + SUM(p.unitsonorder) AS "Total Units"
FROM customer c
JOIN orders o ON c.id = o.id
JOIN orderLine l ON o.id = l.orderid
JOIN product p ON l.productCode = p.code
WHERE p.DISCONTINUED = 'N'
group by p.productname --<- causes ORA-00979
ORDER BY SUM(p.unitsInStock) + SUM(p.unitsonorder)
;
-- ORA-00979: not a GROUP BY expression
--
-- Make sure that columns ( listed in the SELECT clause ) that are _not_ "aggregated"
-- are listed in the GROUP BY clause, otherwise you may get an ORA-00937.
查询有效 - 未选择任何行!
SELECT
c.fn AS "First Name",
c.email AS "Email",
p.productName AS "Product Name",
SUM(p.unitsInStock) + SUM(p.unitsonorder) AS "Total Units"
FROM customer c
JOIN orders o ON c.id = o.id
JOIN orderLine l ON o.id = l.orderid
JOIN product p ON l.productCode = p.code
WHERE p.DISCONTINUED = 'N'
group by p.productname, c.fn, c.email
ORDER BY SUM(p.unitsInStock) + SUM(p.unitsonorder)
;
-- result
no rows selected
收尾工作
SELECT
c.fn AS "First Name",
c.email AS "Email",
p.productName AS "Product Name",
SUM(p.unitsInStock) + SUM(p.unitsonorder) AS "Total Units"
FROM customer C
JOIN orders O ON C.id = O.customerid --<- JOIN condition amended
JOIN orderLine L ON O.id = L.orderid
JOIN product P ON L.productCode = P.code
WHERE p.DISCONTINUED = 'N'
group by p.productname, c.fn, c.email
ORDER BY "Total Units" --<- you can use the column alias here - instead of SUM(...)
fetch first 10 rows only -- optional
;
-- result
First Name Email Product Name Total Units
Annnora agrime3@hc360.com Beans 117
Arleen adahlback5@sciencedaily.com Beans 117
Gwendolin gwalder0@unblog.fr Beans 117
Tomkin tdeignan2@eepurl.com Beans 117
Alard aaimeric9@cyberchimps.com Beans 117
Jeremiah jolland4@dagondesign.com Beans 117
Melita meason8@cnbc.com Beans 117
Stanwood slanphere6@walmart.com Beans 117
Bobbi bfireman1@bbb.org Beans 117
Idelle ililly7@pen.io Beans 117
10 rows selected.
请注意,您无需事先在表中插入任何行即可构建查询(请参阅dbfiddle)。您可能需要调整逻辑(等),以便查询完全满足要求。