【问题标题】:Taking output of union in different column in postgres在 postgres 的不同列中获取联合的输出
【发布时间】:2017-05-11 19:34:48
【问题描述】:

我在一家拨号器公司工作,我们有一个通话记录表,其中有不同的列。

现在我想获取一个可以计算的输出:

已接通的呼出总数

Select count(*) 
from call_history 
where system_disposition = 'CONNECTED' and call_type = 'OUTBOUND' 

系统检测到的总 AMD

Select count(*) 
from call_history 
where system_disposition = 'CONNECTED'   

AGent 检测到的总 AMD

Select count(*) 
from call_history 
system_disposition='CONNECTED' and call_id in (
    select call_id 
    from udh 
    where disposition_code = 'Answering machine'
)

现在,如果我使用联合命令,所有结果都进入一列,使用交叉连接会给出重复输出,即限制 2 给出每个条目的输出两次。

请帮我做一个查询,它可以告诉我:

出站连接总数 | AMD by SYSTEM | AMD 代理 |泄漏为 (AMD Agent/(Total))

【问题讨论】:

  • 欢迎来到 Stack Overflow。我们将帮助您解决问题,但不会为您编写代码。这个想法是尝试自己,并在困难的事情上获得帮助。阅读stackoverflow.com/help/how-to-ask了解更多信息

标签: mysql sql-server postgresql


【解决方案1】:

您可以合并结果并在单个列中选择每个结果,如下所示:

Select sum(r1) as result1, sum(r2) as redult2, sum(r3) as result3 from(
Select count(*) as r1, 0 as r2,0 as r3 from call_history where system_disposition='CONNECTED' and call_type='OUTBOUND'
Union
Select 0 as r1,count(*) as r2, 0 as r3 from call_history where system_disposition='CONNECTED')
Union
Select 0 as r1, 0 as r2,count(*) as r3 from call_history system_disposition='CONNECTED' and call_id in (select call_id from udh where disposition_code='Answering machine')
)

【讨论】:

    【解决方案2】:

    对于 Postgresql

    select *, "AMD by AGent"::numeric/"Total outbound conected" as "Leakage"
    from (
        select
            count(call_type = 'OUTBOUND' or null) as "Total outbound conected",
            count(*) as "AMD by SYSTEM",
            count(exists (
                select 1
                from udh
                where
                    disposition_code = 'Answering machine' and
                    call_id = call_history.call_id
            ) or null) as "AMD by AGent"
        from call_history
        where system_disposition = 'CONNECTED'
    ) s
    

    在 9.4+ 中有 aggregate filter

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多