简体   繁体   English

SQL Server联接到子查询

[英]Sql Server Join to Sub Query

I have a SQL Server table with call records and I'd like to get the number of calls in total and the total calls answered. 我有一个包含通话记录的SQL Server表,我想获取总计的通话数量和已应答的通话总数。

Here is what the table looks like: 该表如下所示:

 Extension | Status
 -----------------------
 300        Answered
 200        Not Answered
 ....       ...

What's the most efficient way to write a query that would return the extension, number of total calls (count on the entire table) and number of answered calls (count where Status = 'Answered' )? 编写查询以返回扩展名,总呼叫数(在整个表中计数)和已应答呼叫数(在Status = 'Answered'计数)的最有效方法是什么?

I created a subquery and joined to it but it seems kind of inefficient so I have 我创建了一个子查询并加入了它,但是效率似乎很低,所以我有

    SELECT Extension, COUNT(*) AS total, answered.num as totalAnswered 
    FROM calls c INNER JOIN (SELECT Extension, COUNT(*) AS num FROM calls 
    WHERE Status = 'answered') answered ON c.Extension = answered.Extension
    GROUP BY Extension, answered.num

Thanks 谢谢

SELECT DISTINCT Extension
    , COUNT(*) OVER() AS total
    , COUNT(*) OVER(PARTITION BY C.Extension) AS totalAnswered
FROM dbo.calls AS C
WHERE C.Status = 'answered';

You can use windows functions to achieve that. 您可以使用Windows函数来实现。 Should work just fine. 应该工作正常。

By using a case statement inside the count aggregate function for the total answered calls, you can keep your query very simple with a group by . 通过在count聚合函数内部使用case语句来回答总调用数,可以使用group by使查询非常简单。

select extension,
       count(*) as total,
       count(case when status = 'answered' then 'X' end) as totalAnswered
  from calls
 group by extension

I am assuming you are trying to return totals per extension . 我假设您正在尝试返回每个扩展的总数。

EDIT 编辑

I have to admit that your post in its current form is not 100% clear about your intent. 我必须承认,您目前的帖子对您​​的意图不是100%清楚的。 The query you posted implies that you want the counts to be per extension . 您发布的查询意味着您希望对每个扩展进行计数。 If that's the case, then the above query will work great. 如果真是这样,那么上面的查询将很好地工作。

But in the text of your post you say: 但是在您的帖子文字中您会说:

number of total calls (count on the entire table) 总通话次数(在整个表格中计数)

... which seems to imply something different: that you don't want the counts to per extension . ...这似乎暗示着一些不同:您希望每个扩展名都计数。

For completeness, here is the query you can use if you want the counts to be global to the entire table, instead of per extension : 为了完整起见,如果您希望计数对整个表都是全局的,而不是按每个扩展名 ,那么可以使用以下查询:

select distinct extension,
                count(*) over () as total,
                count(case when status = 'answered' then 'X' end) over () as totalAnswered
  from calls

And, if for some reason, you need a combination of both kinds of counts, then you can use something like this: 并且,如果由于某种原因,您需要将两种计数结合使用,则可以使用以下方法:

  select extension,
         count(*) as totalPerExtension,
         count(case when status = 'answered' then 'X' end) as totalAnsweredPerExtension,
         totalGlobal,
         totalAnsweredGlobal
    from (select *,
                 count(*) over () as totalGlobal,
                 count(case when status = 'answered' then 'X' end) over () as totalAnsweredGlobal
            from calls) c
   group by extension, totalGlobal, totalAnsweredGlobal
SELECT Extention, 
       COUNT(*) as total, 
       SUM(answered) OVER (PARTITION BY Extention) as answered
FROM (
   SELECT Extention,
          CASE WHEN Status = 'answered' THEN 1 ELSE 0 END as answered
) T

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM