我是 SQL 新手,我有以下 SQL 查询
select catalogid, numitems, allitems - numitems ignoreditems
from (
select i.catalogid,"
sum(case when (ocardtype in ('PayPal','Sofort') OR
ocardtype in ('mastercard','visa') and
odate is not null) AND NOT EXISTS (select CAST(booked AS INT) FROM bookedordersids b where b.booked = o.orderid)
then numitems
else 0 end) numitems,
sum(numitems) allitems
from orders o
join oitems i on i.orderid=o.orderid
group by i.catalogid
) X
它给了我以下错误
Cannot perform an aggregate function on an expression containing an aggregat or a subquery
当我删除以下行时,它工作正常
AND NOT EXISTS (select CAST(booked AS INT) FROM bookedordersids b where b.booked = o.orderid)
但做这个检查很重要;我怎样才能解决这个问题?