查询同一个表两次以在一个查询中获取不同的数字

时间:2015-09-03 19:53:10

标签: sql sql-server

使用Microsoft SQL Server,Table1有客户请求,table2有解决方案代码(因此我们可以看到客户写入的原因,以及我们为解决问题所做的工作)。有时候销售代表忘记添加解决方案代码,所以我试图运行一个查询来告诉我每个代表的名称,有多少封闭的问题缺少解决方案代码,他们总共关闭了多少问题,以及他们的百分比是多少封闭问题与没有解决方案代码的封闭问题。例如:

Agent Name | Missing Resolution Codes | Total issues closed | % of 
----------------------------------------------------------------------
Mary       |             12           |         120         | 10.00%
Bob        |              8           |         704         |  1.14%

显示缺少解决方案代码的查询效果很好。显示已关闭的总问题的查询效果很好。每当我尝试将它们组合起来时,我要么得到这个:

(使用UNION)

Mary |  12
Mary | 120
Bob  |   8
Bob  | 704

或者如果我尝试from (select...) t1, (select...) t2,我会得到这个:

Mary | 12 | 120
Mary | 12 | 704
Bob  |  8 | 120
Bob  |  8 | 704

如果我在最后添加Group By,我会收到各种错误。

这两个查询本身如下:

缺少解决方案代码:

select i.assigned_to, count(i.assigned_to)
from customer_issues as i
left join resolution_codes as r on i.issue_ID = r.issue_ID
where r.issue_id is null
and i.status='closed'
group by i.assigned_to

完整的已结束问题:

select assigned_to, count(assigned_to)
from customer_issues
where status='closed'
group by assigned_to

感谢您提供的任何帮助!

编辑:只是为了澄清,解决方案代码在不同的表中,因为一个客户问题可能包含几个问题和几个解决方案。因此,虽然customer_issues表确实有一个关闭问题本身的指示,但resolution_codes表显示了客户遇到的不同问题,我们可以解决哪些问题,哪些问题无法解决等等。

3 个答案:

答案 0 :(得分:0)

select t1.assigned_to, t1.missing, t2.total from 
( 
select i.assigned_to as assigned_to, count(i.assigned_to) as missing
from customer_issues as i
left join resolution_codes as r on i.issue_ID = r.issue_ID
where r.issue_id is null
and i.status='closed'
group by i.assigned_to
) t1 INNER JOIN
(
select assigned_to, count(assigned_to) as total
from customer_issues
where status='closed'
group by assigned_to
) t2
ON t1.assigned_to=t2.assigned_to

答案 1 :(得分:0)

由于两个查询之间的唯一区别似乎是左连接和空过滤器,因此您可以在一个查询中完成所有操作。

我认为这应该有效:

;with source as (
   select 
        i.assigned_to as [Agent Name], 
        count(r.issue_id) as [Resolution Codes], 
        count(i.assigned_to) as [Total issues closed]
    from customer_issues as i
    left join resolution_codes as r on i.issue_ID = r.issue_ID
    where i.status='closed'
    group by i.assigned_to
)

select 
    [Agent Name], 
    [Total issues closed]-[Resolution Codes] as [Missing Resolution Codes],
    [Total issues closed],
    ([Total issues closed]-[Resolution Codes])*100.0/[Total issues closed] as [% of]
from source;

Sample SQL Fiddle

答案 2 :(得分:0)

(更新)

显然,没有解析代码不会反映在表customer_issues中的任何位置,而只会在resolution_codes中没有任何与给定问题相对应的行中反映出来。这似乎有点暗淡,但在更大的计划中可能是明智的。无论如何,您可以执行单个聚合查询以一次性收集所需的所有统计信息,然后从结果中计算所有派生的统计信息。

将聚合查询作为内联视图执行非常有用,以便更容易(并且更简洁地)重用两个聚合列:

select
  agg.assigned_to,
  agg.total_closed,
  (agg.total_closed - agg.with_code) as without_code,
  ((agg.total_closed - agg.with_code) / cast(agg.total_closed as float)) as "% without"
FROM (
    select
      ci.assigned_to,
      count(*) as total_closed,
      count(rc.issue_id) as with_code
    from customer_issues ci
      left join resolution_codes rc on ci.issue_ID = rc.issue_ID
    where ci.status='closed'
    group by ci.assigned_to
  ) agg
  ;