我有一个存储过程,用于计算我所拥有的未完成(1000)或未取消(1100)的文档数。当我只有这些条件之一时,它会正确地计算数字,但是一旦添加了or,它就会抓住所有内容,而忽略任何逻辑。我必须在这里缺少SQL的一些基本内容
SELECT
[PartnerCoId] as DisplayID
,[PartnerCompanyName] as DisplayName
,Count(*) as DocumentTotal
FROM vwDocuments
where coid = @inputCoid and DocumentType = 'order'
and Status <> 1100
and PartnerCoId <> @inputCoid
group by
[PartnerCoId]
,[PartnerCompanyName]
union all
SELECT [CoId] as DisplayID
,[CompanyName] as DisplayName
,Count(*) as DocumentTotal
FROM vwDocuments
where PartnerCoId = @inputCoid and DocumentType = 'order'
and Status <> 1100
and CoId <> @inputCoid
group by [CoId]
,[CompanyName]
order by [DisplayName]
这将返回未取消状态的文档数。如果我将1100更改为1000,它将返回未完成状态的文档数。一旦我将查询更新为:
and (Status <> 1100 or Status <> 1000)
它破坏了逻辑。
有什么想法吗?我已经尝试了许多不同的查询逻辑组合,但无法弄清楚这一点。
与其使用布尔逻辑进行搏斗,不如使用not in
:
and Status not in (1100, 1000)
它更容易阅读和理解,因为它实际上是英语,并且因为它是一个语句,所以您也不需要在括号中加上括号。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句