我想创建一个非常复杂的SQL查询,它会根据我的条件生成0/1或TRUE/FALSE。
我的想法是,我有一个客户表,然后我想在不同的表中进行两次检查,如果其中一个是真的,那么我们将返回true,如果不是,则返回FALSE:
我想用CASE做这个。参见以下示例:
select
case
((select count(*) from boughtleads bl where bl.customerid = cu.id)>0)
then 'TRUE'
else 'FALSE'
end
from customers cu
left join leadagents la on la.customerid = cu.id
where cu.vatnumber = '30218124'
这将返回:
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near '>'.
我的挑战是。首先,上面的查询由于某种原因失败,我的另一个问题是我不能使用OR。
我理想的伪代码是:
select
case
(
((select count(*) from boughtleads bl where bl.customerid = cu.id)>0)
OR
((select count(*) from leadnotifications ln where ln.leadagentid = la.id)>))
then 'TRUE'
else 'FALSE'
end
from customers cu
left join leadagents la on la.customerid = cu.id
where cu.vatnumber = '30218124'
有什么办法可以解决这个问题吗?