首页 > 解决方案 > 2 在一个中选择语句

问题描述

起初,很抱歉英语不是很好。我有下一个情况:我有 2 个 SQL 选择语句:

(
select 
    calldate, src, dst, disposition, uniqueid 
from cdr 
where 
    calldate>curdate() 
    and (length(cnum)>5 or cnum='5555' or cnum='8888')
    and (length(dst)>5 or dst='1111')
group by uniqueid
)

(
select 
    calldate, src, dst, disposition, uniqueid 
from cdr 
where 
    calldate>curdate() and (length(cnum)>5 or cnum='5555' or cnum='8888')
    and (length(dst)>5 or dst='1111') 
    and disposition='ANSWERED'
)

它们之间的唯一区别是第二条语句显示 disposition=ANSWERED 的所有行,同时第一条语句显示所有行

我想用一个条件加入这两个语句:如果在第一个语句中 disposition=ANSWERED 或如果在第一个语句中 disposition=NO ANSWER 但在第二个条件中 disposition=ANSWERED - 然后在我的最后一个语句中 disposition=ANSWERED (如果它在这两个陈述中都是 NO ANSWER 它是 NO ANSWER)。

我写了这个最后的声明:

select a.calldate as 'Время', case a.src 
when '4952763333' then 'clinic     '
    else a.src
end
as 'From where',
case a.dst
when '1111' then 'clinic     '
    else a.dst
end
as 'Destination',
case a.disposition 
when (a.disposition='ANSWERED' and b.disposition='ANSWERED') then 'ANSWERED'
when (a.disposition='NO ANSWER' and b.disposition='ANSWERED') then 'ANSWERED'
when (a.disposition<>'ANSWERED' and b.disposition<>'ANSWERED') then 'NO ANSWER'
end 
as 'Answer',
case a.src
when '4958885555' then 'Outcoming'
    else 'Incoming'
end
as 'Type Call'
from (
    select 
        calldate, src, dst, disposition, uniqueid 
    from cdr 
    where 
        calldate>curdate() 
        and (length(cnum)>5 or cnum='5555' or cnum='8888')
        and (length(dst)>5 or dst='1111')
    group by uniqueid
) a 
left join (
    select 
        calldate, src, dst, disposition, uniqueid 
    from cdr 
    where 
        calldate>curdate() 
        and (length(cnum)>5 or cnum='5555' or cnum='8888')
        and (length(dst)>5 or dst='1111') 
        and disposition='ANSWERED'
) b on a.uniqueid=b.uniqueid;

但是在最后的声明中,我有一个问题:它总是显示为“已回答”。我该如何解决这个问题,我认为这个问题靠近案例“a.disposition”。在这里我不明白 SQL 可以与 a.disposition 和 b.disposition 一起使用(可能是我没有正确制定)。

最后,我想补充一点,它是带有 Asterisk 数据库的词。我会很高兴得到任何答案。

标签: mysql

解决方案


我的意思是你的case陈述有误。因为b.disposition可能是唯一的'ANSWERED',或者NULL您可以像这样更改它:

...
case
  when 
    (a.disposition='ANSWERED' and b.disposition is not null) then 'ANSWERED'
  when
    (a.disposition='NO ANSWER' and b.disposition is not null) then 'ANSWERED'
  when (a.disposition<>'ANSWERED' and b.disposition is null) then 'NO ANSWER'
  else 'UNKNOWN'
end as 'Answer',
...

推荐阅读