尝试在新创建的计算列上使用sum()。求和函数无法识别calc列名称。这是我的代码
select name, is_open,
CASE WHEN text like '%perfect%' or '%amazing%' or '%happy%' or '%delicious%'or '%fabulous%'or '%fantastic%'or '%kind%' THEN 1
WHEN text like '%hate%'or '%horrible%'or '%bad%'or '%angry%'or '%fantastic%'or '%expensive%'or '%disgusting%' THEN -1
END sentiment_rating,
sum(sentiment_rating) as sum
from review as r left join
business as b
on b.id = r.business_id
where is_open is not Null and sentiment_rating is not Null
group by name
order by name ASC
答案 0 :(得分:1)
同时选择未聚合的列和聚合的列都没有意义。因此,只需xyz/
表达式:
sum()
我不确定是要每select name, is_open,
sum(CASE WHEN text like '%perfect%' or text like '%amazing%' or text like '%happy%' or text like '%delicious%' or text like '%fabulous%' or text like '%fantastic%' or text like '%kind%' THEN 1
WHEN text like '%hate%' or text like '%horrible%' or text like '%bad%' or text like '%angry%' or text like '%fantastic%'or '%expensive%' or text like '%disgusting%' THEN -1
END) as sum_sentiment_rating
from review as r left join
business as b
on b.id = r.business_id
where is_open is not Null and sentiment_rating is not Null
group by name, is_open
order by name ASC;
行还是每name
/ name
行。我假设是后者,并将is_open
添加到is_open
。
答案 1 :(得分:0)
您所有的LIKE
条件在语法上都是错误的。
您不能拥有:
text like '%perfect%' or '%amazing%'....
正确的语法是:
text like '%perfect%' or text like '%amazing%' ....
然后,您必须直接对计算得出的CASE
列求和,并在分组列中包含is_open
:
select name, is_open,
SUM(CASE
WHEN text like '%perfect%' or text like '%amazing%' or text like '%happy%' or text like '%delicious%' or text like '%fabulous%' or text like '%fantastic%' or text like '%kind%' THEN 1
WHEN text like '%hate%' or text like '%horrible%' or text like '%bad%' or text like '%angry%' or text like '%fantastic%' or text like '%expensive%' or text like '%disgusting%' THEN -1
END) as sumrating
from review as r left join
business as b
on b.id = r.business_id
where is_open is not Null and sentiment_rating is not Null
group by name, is_open
order by name ASC