ORA-00923:未找到FROM关键字的位置&错误:ORA-01756:引用的字符串未正确终止

时间:2014-10-20 23:17:05

标签: sql oracle nested syntax-error aggregate-functions

我收到了标题为错误消息(首先在sqldeveloper中,第二个在sqlplus中)

以下是我的以下任务代码:

对于customers表中的每个客户,显示客户ID和姓氏。如果客户去年有任何订单,则显示这些订单的平均应付金额以及这些订单的最高金额。如果没有订单,则显示字符串'no orders'。按客户ID排序。

Select 
DISTINCT cust_id As CustID, cust_name_last AS "CustName",
NVL( to_char( AVG( order_price * quantity )),'9999.99'),'no orders') AS
"AverageAmntDue",
NVL( to_char( MAX( order_price * quantity ),'9999.99'),' no orders') AS   
"HighestAmntDue"
From bk_customers
LEFT JOIN bk_order_headers using (cust_id)
LEFT JOIN bk_order_details using (order_id)
Where TO_CHAR (order_date, 'YYYY’) = 2014
Group by cust_id, cust_name_last
Order by cust_id;

编辑:

(以下是对我的回答的尝试编辑,但它应该已经到了这里。) 任务要求的是...... 对于customers表中的每个客户,显示客户ID和姓氏。如果 客户去年有任何订单显示这些订单的平均到期金额 这些订单应付的最高金额。如果没有订单,则显示字符串'no orders'。订购 按客户ID

我的导师告诉我,我应该注意的是一种方法,你需要考虑如何说出你想要的东西: 您希望客户进行_____日期测试___ 和___没有订单的客户测试____。

我所写的内容并未返回具有空值的客户,即<。p>

Select 
cust_id As "CustID"
, cust_name_last AS "CustName"
, NVL(to_char(AVG(order_price * quantity), '9999.99'), 'no orders') AS AverageAmntDue
, NVL(to_char(MAX(order_price * quantity), '99999.99'), 'no orders') AS HighestAmntDue
From bk_customers
LEFT JOIN bk_order_headers using (cust_id)
LEFT JOIN bk_order_details using (order_id)
Where extract(year from order_date) = 2014
Group by cust_id, cust_name_last
ORDER by cust_id;

我现在只是非常沮丧,有人可以提供帮助

1 个答案:

答案 0 :(得分:1)

问题似乎是'YYYY’上的卷曲结束语。

我会将查询写成:

Select cust_id, cust_name_last AS CustName,
       NVL(to_char(AVG(order_price * quantity), '9999.99'), 'no orders') AS AverageAmntDue,
       NVL(to_char(MAX(order_price * quantity), '9999.99'), 'no orders') AS HighestAmntDue
From bk_customers LEFT JOIN
     bk_order_headers
     using (cust_id) LEFT JOIN
     bk_order_details
     using (order_id)
Where extract(year from order_date) = 2014
Group by cust_id, cust_name_last
Order by cust_id;

我还会在列上使用表别名,并考虑仅使用NULL来表示“无订单”,因此 转换为字符是不必要的。