我有以下脚本,我只想过滤包含in语句的分析师的结果:
select count(distinct t1.ticketid),t2.BuilderAnalystID AS iDS
from ticket as t1 inner join
Timepoint as t2 on t1.ticketid=t2.TicketID
where
(InternalTicketStatusID=4 and
(TicketStatus like '%closed%' or ticketstatus like '%resolved%'))
or
(InternalTicketStatusID<>4 and
(TicketStatus like '%closed%' or ticketstatus like '%resolved%')) AND
SubmissionToDT>='04-01-2017' AND
SubmissionToDT<='07-12-2019' AND
(t2.BuilderAnalystID not like null
and t2.BuilderAnalystID in (1,3,8,9,10,11,12,13,14,15,186,193,233,234,235,236,237,238))
group by t2.BuilderAnalystID
但是,在返回的结果中,我看到的是所有分析师的ID,而不是我包含在IN语句中的ID。我也看到null的ID! 任何线索为什么会这样? 谢谢
答案 0 :(得分:3)
如果一致地格式化代码,您将立即发现问题:
where (InternalTicketStatusID = 4 and
(TicketStatus like '%closed%' or ticketstatus like '%resolved%'
)
) or
--------^ Alert! The following conditions do not apply
(InternalTicketStatusID <> 4 and
(TicketStatus like '%closed%' or ticketstatus like '%resolved%'
)
) AND
SubmissionToDT >= '2017-04-01' AND
SubmissionToDT <= '2019-07-12' AND
(t2.BuilderAnalystID not like null and
t2.BuilderAnalystID in (1,3,8,9,10,11,12,13,14,15,186,193,233,234,235,236,237,238)
)
您的第一个条件(在括号中)未与其他条件连接。括号关闭了。
这就是为什么我用右括号将右括号匹配。
我还固定了日期常量。
答案 1 :(得分:1)
t2.BuilderAnalystID not like null
产生null而不是true
并且在where子句中,只有true和false响应才是逻辑运算
您是说
AND ( t2.BuilderAnalystID is not null
and t2.BuilderAnalystID in (1,3,8,9,10,11,12,13,14,15,186,193,233,234,235,236,237,238))
或更佳
AND t2.BuilderAnalystID in (1,3,8,9,10,11,12,13,14,15,186,193,233,234,235,236,237,238)