SQL Server和/或查询

时间:2017-03-17 10:58:07

标签: sql-server

下面的查询仍在TC.ContractPartner列中返回0,有人可以解释原因吗?

我尝试使用'0',以防零是文本

select distinct tc.ID
, tc.[Contact Partner]
, so.BusinessPartnerNumber
, tc.[Actual Date From]
, SO.ServiceOrder
, SO.ServiceProduct
, so.SOCreatedOn

from AB.TradComplaints_Temp as TC

left Join main.Tbl_ServiceOrder as SO on SO.BusinessPartnerNumber = tc.[Contact Partner]

where tc.[Contact Partner] is not null
and tc.[Actual Date From] > so.SOCreatedOn
and  so.SOCreatedOn > (tc.[Actual Date From]-90)
and so.ServiceOrder is not null

or tc.[Contact Partner] <> 0
and tc.[Actual Date From] > so.SOCreatedOn
and  so.SOCreatedOn > (tc.[Actual Date From]-90)
and so.ServiceOrder is not null
order by tc.ID 

2 个答案:

答案 0 :(得分:1)

or更改为and?:

and so.ServiceOrder is not null
and tc.[Contact Partner] <> 0

答案 1 :(得分:0)

如果你想要结果

so.ServiceOrder is not null or tc.[Contact Partner] <> 0    

然后将这些论文归类于braket

select distinct tc.ID
, tc.[Contact Partner]
, so.BusinessPartnerNumber
, tc.[Actual Date From]
, SO.ServiceOrder
, SO.ServiceProduct
, so.SOCreatedOn

from AB.TradComplaints_Temp as TC

left Join main.Tbl_ServiceOrder as SO on SO.BusinessPartnerNumber = tc.[Contact Partner]

where tc.[Contact Partner] is not null
and tc.[Actual Date From] > so.SOCreatedOn
and  so.SOCreatedOn > (tc.[Actual Date From]-90)
and (so.ServiceOrder is not null

or RTRIM(LTRIM(tc.[Contact Partner])) <> '0')
and tc.[Actual Date From] > so.SOCreatedOn
and  so.SOCreatedOn > (tc.[Actual Date From]-90)
and so.ServiceOrder is not null
order by tc.ID