gpt4 book ai didi

sql - 将GROUP BY与AVG函数一起使用-错误的语法错误

转载 作者:行者123 更新时间:2023-12-03 08:27:16 25 4
gpt4 key购买 nike

SELECT 
AVG (CASE
WHEN ItemPrice LIKE '-%'
THEN NULL
WHEN ItemPrice LIKE '0%'
THEN NULL
ELSE CAST (ItemPrice AS FLOAT)
END)
GROUP BY
HHIncome
FROM
Tacos

SQL返回以下错误:

Error: Incorrect syntax near the keyword 'FROM'.
SQLState: S0001
ErrorCode: 156



有人可以单独解释为什么上面的语句不正确,但嵌套在下面的语句中却可以操作吗?
SELECT  
HHIncome AS Income_Segment,
COUNT(Customer) AS number_of_customers,
AVG (CASE
WHEN ItemPrice LIKE '-%' THEN NULL
WHEN ItemPrice LIKE '0%' THEN NULL
ELSE CAST (ItemPrice AS FLOAT)
END) AS avg_item_price,
COUNT(DISTINCT(ReceiptIDDesc)) AS number_of_orders,
SUM(CAST(ItemPrice AS FLOAT)) AS total_spend
FROM
Tacos
GROUP BY
HHIncome;

最佳答案

问题是GROUP BY和FROM子句的顺序。 GROUP BY必须在FROM [表名]之后。

关于sql - 将GROUP BY与AVG函数一起使用-错误的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47837716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com