1

假设我在一个呼叫中心工作,我关闭了 10 个电话,但一天打开了 20 个电话。“真实”的数字实际上是-10。即使目标是关闭 10 次调用,工作人员也失败了,因为打开了 20 次调用。

我想写一个 SQL 报告来反映这一点。但我的问题似乎是我无法从总计数中计算数字。

SELECT workername                       AS Name, 
       (SELECT Count(closeddate) 
        FROM   mybanksupport 
        WHERE  closeddate = NULL)       OPENCALLS, 
       (SELECT Count(closeddate) 
        FROM   mybanksupport 
        WHERE  closeddate = NOT NULL)   CLOSEDCALLS, 
       (SELECT opencalls - closedcalls) REALCALLS 
FROM   mybanksupport 

简而言之,我想计算 2 列计数值,然后使用该值生成另一个名为 Real Calls 的计算列

4

2 回答 2

2

COUNT只计算值,即它忽略NULLs。此属性可用于简化您的表达式:

SELECT workername, closedCalls, totalCalls - closedCalss AS openCalls
FROM   (SELECT   workername, COUNT(closeddate) AS closedcalls, COUNT(*) totalCalls
        FROM     mybanksupport
        GROUP BY workername) t
于 2013-11-02T12:26:35.387 回答
0

写为子查询然后你可以使用你想要的字段形式

Select
  workername
, OPENCALLS
, CLOSEDCALLS
, (OPENCALLS - CLOSEDCALLS)  REALCALLS
From
(
SELECT workername                       AS Name, 
       (SELECT Count(closeddate) 
        FROM   mybanksupport 
        WHERE  closeddate = NULL)       OPENCALLS, 
       (SELECT Count(closeddate) 
        FROM   mybanksupport 
        WHERE  closeddate = NOT NULL)   CLOSEDCALLS, 
FROM   mybanksupport 
) T1
于 2013-11-02T12:25:03.683 回答