如何解决如何避免在SQL
我想通过跟踪count
sql
个产品编号
但我遇到了以下错误。
ORA-00936: Missing Expression
我的SQL哪里出了问题。
如果有人有意见,请告诉我。
谢谢
select t.customer,count(case when left(t.product,2)='AD' then 1 end) as A,1)='F' then 1 end) as B,1)='H' then 1 end) as C,from table t
left join table2 t2
on t.customer = t2.customer
where t2.type='VLI'
group by t.customer
解决方法
Oracle没有LEFT()
功能,而MySQL有,请使用SUBSTR()
。并在第四行末尾删除逗号(即错字)
SELECT t.customer,COUNT(CASE
WHEN SUBSTR(t.product,1,2) = 'AD' THEN
1
END) AS A,1) = 'F' THEN
1
END) AS B,1) = 'H' THEN
1
END) AS C
FROM yourtable t
LEFT JOIN table2 t2
ON t.customer = t2.customer
WHERE t2.type = 'VLI'
GROUP BY t.customer
,
第count(case when left(t.product,1)='H' then 1 end) as C,
行中有多余的逗号。
删除最后一个逗号,该错误将消失。
我建议LIKE
:
select t.customer,sum(case when t.product like 'AD%' then 1 else 0 end) as A,sum(case when t.product like 'F%' then 1 else 0 end) as B,sum(case when t.product like 'H%' then 1 else 0 end) as C
from table t left join
table2 t2
on t.customer = t2.customer and t2.type='VLI'
group by t.customer;
注意:
- 使用
like
,您不必担心子字符串函数的参数与匹配字符串的长度匹配。很久很久以前,我就知道那里的不一致问题。 - 在这种情况下,我更喜欢
sum()
,胜过count()
,但这主要是出于审美目的。 - 您正在使用
left join
,但在where
子句中进行了过滤。这会将联接变成内部联接。将join
更改为内部联接,或将条件移至on
子句(这样做)。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。