我有桌子:
我的目标是创建一个查询,以返回“ 4K Ultra HD TV”子类别中产品的平均质量和价格,且价格超过4000美元。我不能只返回Score_ID,我需要返回值的平均值(这是Score表中的一个字段)。
我已经为此工作了几个小时,这就是我的想法。但是,我不知道如何解决该错误。我相信这与子查询中的AVG函数或我使用SELECT的方式有关。
COLUMN PID FORMAT 999
COLUMN AVG_QUALITY FORMAT 99999999999
COLUMN AVG_VALUE FORMAT 99999999999
SELECT R.PRODUCT_ID AS PID,
AVG_QUALITY = (SELECT AVG(SQU.SCORE_VALUE)
FROM SCORE SQU
INNER JOIN SCORE SQU ON SQU.SCORE_ID = R.SCORE_QUALITY
WHERE R.SCORE_QUALITY = SQU.SCORE_ID),
AVG_VALUE = (SELECT AVG(SVA.SCORE_VALUE)
FROM SCORE SVA
INNER JOIN SCORE SVA ON SVA.SCORE_ID = R.SCORE_VALUE
WHERE R.SCORE_VALUE = SVA.SCORE_ID)
FROM REVIEW R
INNER JOIN PRODUCT P ON P.PRODUCT_ID = R.PRODUCT_ID
INNER JOIN PRODUCTSUBCATEGORY PS ON PS.PRODUCT_ID = P.PRODUCT_ID
INNER JOIN SUBCATEGORY S ON S.SUBCAT_ID = PS.SUBCAT_ID
WHERE S.NAME = '4K Ultra HD TVs'
AND P.PRICE > 4000;
AVG_QUALITY = (SELECT AVG(SQU.SCORE_VALUE)
*
ERROR at line 2:
ORA-00923: FROM keyword not found where expected
答案 0 :(得分:0)
错误指向=
,而不是AVG
或子选择。
您似乎正在尝试提供所选值的名称。语法不是NAME = EXPR
,而是EXPR AS NAME
(就像您在第一列R.PRODUCT_ID AS PID
中一样)。
因此
AVG_QUALITY = (SELECT AVG(SQU.SCORE_VALUE)
FROM SCORE SQU
INNER JOIN SCORE SQU ON SQU.SCORE_ID = R.SCORE_QUALITY
WHERE R.SCORE_QUALITY = SQU.SCORE_ID)
应该说
(SELECT AVG(SQU.SCORE_VALUE)
FROM SCORE SQU
INNER JOIN SCORE SQU ON SQU.SCORE_ID = R.SCORE_QUALITY
WHERE R.SCORE_QUALITY = SQU.SCORE_ID)
AS AVG_QUALITY
答案 1 :(得分:0)
所以我只是重新格式化了您的查询以提高(我个人)的可读性,并更改了别名的分配方式:
SELECT R.PRODUCT_ID AS PID
, ( SELECT AVG(SQU.SCORE_VALUE)
FROM SCORE AS SQU -- This is SQU
INNER JOIN SCORE AS SQU -- This is SQU
ON SQU.SCORE_ID = R.SCORE_QUALITY
WHERE R.SCORE_QUALITY = SQU.SCORE_ID) AS AVG_QUALITY
, ( SELECT AVG(SVA.SCORE_VALUE)
FROM SCORE AS SVA -- This is SVA
INNER JOIN SCORE AS SVA -- This is SVA
ON SVA.SCORE_ID = R.SCORE_VALUE
WHERE R.SCORE_VALUE = SVA.SCORE_ID) AS AVG_VALUE
FROM REVIEW AS R
INNER JOIN PRODUCT AS P
ON P.PRODUCT_ID = R.PRODUCT_ID
INNER JOIN PRODUCTSUBCATEGORY AS PS
ON PS.PRODUCT_ID = P.PRODUCT_ID
INNER JOIN SUBCATEGORY AS S
ON S.SUBCAT_ID = PS.SUBCAT_ID
WHERE S.NAME = '4K Ultra HD TVs'
AND P.PRICE > 4000;
问题是,在每个子查询中,您两次使用相同的表,并且两次创建完全相同的别名,这使数据库感到恐慌。我在显示确切位置的代码中留下了评论。
因此,只需创建查询中唯一的别名,您就会保持健康。