尝试编写有关作业问题的SELECT语句。我添加了一个SUM函数,但不确定是否可以那样使用。以及由于表彼此远离而具有3个JOIN。老实说,如果我当前的任何代码不正确,请告诉我。但是我的主要重点是如何限制表中的“停产”字段。
问题是:从销售数据模型中提取一条语句,以显示客户的名字,客户的电子邮件地址,产品名称和总单位,其中总单位是库存单位和订单单位的总和。将记录限制为不间断的订单(即不间断字段中的“ N”)。为SELECT子句中的每个字段创建一个别名,包括计算列的“总单位”。为FROM子句中的每个表创建并使用别名。按总单位排序。
SELECT customerFN AS “First Name”, customerEmail AS “Email”, productName AS “Product Name”,
(SUM(unitsInStock)+SUM(unitsonorder)) AS "Total Units"
FROM customer c
JOIN order o
ON c.customerID = o.customerID
JOIN orderLine l
ON o.orderID = l.orderID
JOIN product p
ON l.productCode = p.productCode
WHERE
在WHERE子句之后没有任何内容,因为代码不完整。该代码是不完整的,因为我认为我需要某种WHERE子句来限制“停产”字段。我只是不知道该怎么做。我已经查询了Where子句,并且我认为它将使用> <或!=。但是我不知道该功能。
答案 0 :(得分:1)
在我看来,您需要在DISCONTINUED = 'N'
子句中添加WHERE
,或者换句话说
SELECT c.customerFN AS "First Name",
c.customerEmail AS "Email",
p.productName AS "Product Name",
SUM(p.unitsInStock) + SUM(p.unitsonorder) AS "Total Units"
FROM customer c
JOIN order o
ON c.customerID = o.customerID
JOIN orderLine l
ON o.orderID = l.orderID
JOIN product p
ON l.productCode = p.productCode
WHERE p.DISCONTINUED = 'N'
ORDER BY SUM(p.unitsInStock) + SUM(p.unitsonorder)
此外,您似乎正在使用某种文字处理程序来创建代码,因为您的原始代码具有花哨的打开和关闭引号字符(即“和”),而不是ASCII引号字符(即“)。这些文字处理器生成的字符将不会被我大多数(任何?)编程语言或开发工具所接受。例如,看看您的原始问题-请注意,“ First Name”和“ Email”没有突出显示为问题中的字符串,但“总单位”是吗?那是因为只有最后一个被双引号字符包围了。我建议您坚持使用普通的ASCII编辑器,例如Windows上的记事本,或任何数以千计的编程工具编辑器工具在那里,您会更加快乐。
好运。
答案 1 :(得分:1)
调试此查询时,您很可能会遇到几条(不同的)错误消息。另外:在没有表/数据的情况下编写查询可能有些棘手,因为某些方面很容易被忽略。因此,我们将使用一些测试表(包括数据),请参见dbfiddle here。 下面的查询代码将向您显示各种错误,以及修复这些错误所需的SQL。阅读代码中的所有注释。
ORA-00903:无效的表名
在原始查询中,您使用的表名为ORDER。 ORDER是保留字,请参见documentation。避免在命名表时使用它。
ORA-00937:不是单组分组功能
使用有效的表名时,我们可以执行查询。在WHERE子句中添加条件(请参见下文)。由于SELECT和ORDER BY子句中的SUM(),我们得到了错误消息。显然,我们需要在查询中添加GROUP BY子句(这会立即导致下一条错误消息...)
soundToPlay
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" --<- 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
查询有效-未选择任何行!
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
请注意,您可以在不事先将任何行插入表中的情况下构建查询(请参见dbfiddle)。可能需要调整逻辑(等),以使查询完全满足要求。