我在解决此错误时遇到问题,但是当我运行以下查询时,我得到了:
SQL0115 Comparison operator IN not valid
根据我在文档中可以看到的,这是由于CASE语句中的子选择,但是我不确定为什么。这是我要从MySQL迁移到DB2的查询,所以我知道会有一些问题,但是我不明白为什么这会特别引起问题。
基本上,在这种情况下,扩展名将是会话表中的finallycalledpartyno
或callingpartyno
,但我想根据用户表中的哪个来选择其中一个。 / p>
因此,如果用户的扩展名为1234,则在会话表中,每个记录将具有finallycalledpartyno
或callingpartyno
作为1234,这就是我想用作extension
的含义。有什么特别的方法可以解决吗?
SELECT distinct
case
when callingpartyno in (select extension from hn.users where active = 1)
then callingpartyno
when finallycalledpartyno in (select extension from hn.users where active = 1)
then finallycalledpartyno
end as extension
FROM
hn.session a
join hn.call_summary b
on a.notablecallid = b.notablecallid
where
date(a.ts) >= curdate()
and (
callingpartyno in (select extension from hn.users where active = 1)
or finallycalledpartyno in (select extension from hn.users where active = 1)
)
group by extension
样本数据
finallycalledpartyno | callingpartyno | seconds
---------------------------------------------------
1234 123456 20
1234 123456 20
1234 123456 20
123456 1234 10
123456 1234 10
123456 1234 10
4321 123456 20
4321 123456 20
4321 123456 20
123456 4321 20
123456 4321 20
123456 4321 20
所以现在我可以得到2行
Extension | secondsWhereFC | secondsWhereCP
---------------------------------------------
1234 60 30
4321 60 60
答案 0 :(得分:2)
它对您有用吗?
with query_res (finallycalledpartyno, callingpartyno, seconds) as (values
(1234 , 123456, 20)
, (123456 , 4321 , 10)
)
, users (extension, active) as (values
(1234, 1)
, (4321, 1)
)
select q.*, coalesce(c.extension, f.extension)
from query_res q
left join (select distinct extension from users where active=1) c on c.extension=q.callingpartyno
left join (select distinct extension from users where active=1) f on f.extension=q.finallycalledpartyno;