计算行组的百分比

时间:2013-12-19 16:59:39

标签: sql-server sql-server-2005 aggregate-functions

对不起,我找不到这个问题更好的标题,英语不是我的母语! 我需要提出这个输出:

SalesRep | Year | Month | Country | Call % 
--------   ----   -----   -------   ------
Name1      2012       1  USA          10.1 
Name1      2012       1  UK           12.6 
Name1      2012      12  USA          15.7 
Name1      2012      12  France       38.0 
Name1      2013       1  Spain        11.5      
Name1      2013       1  Angola       23.2
Name1      2013      12  Norway       20.1 
Name1      2013      12  Italy        20.9 
Name2      2012       1  Spain         9.4

SORRY !!!我忘了提到百分比是根据每个销售代表的总呼叫计算的。所以,基本上我需要按国家/地区对每个salesrep进行的调用百分比细分。因此,计算的基础不是所有salesrep的调用总数,而是每个salesrep调用的次数。

我有一个查询,它返回呼叫计数,但我无法使呼叫百分比起作用:

set dateformat dmy
select u.first_name + ' ' + u.last_name as SalesRep, year(act.date_start) as Year, 
    month(act.date_start) as Month, acc.billing_address_country as Country, Count(*)
from accounts acc
    inner join activities act
        on acc.id = act.parent_id
    inner join users u
        on act.assigned_user_id = u.id
where act.deleted = 0 and acc.deleted = 0 and act.project_id = 'some_id' 
    and act.parent_type = 'Account' and act.activity_type = 'Call'
    and act.status = 'Held' and act.date_start >='01-01-2012 00:00:01' 
    and act.date_start <='01-12-2013 00:00:01'
group by u.first_name + ' ' + u.last_name, year(act.date_start), month(act.date_start), acc.billing_address_country
order by u.first_name + ' ' + u.last_name, year(act.date_start), month(act.date_start)

你能帮帮我吗?

提前谢谢!

2 个答案:

答案 0 :(得分:2)

Wrap the query并使用windowing functions:

WITH t AS (
    select u.first_name + ' ' + u.last_name as SalesRep, year(act.date_start) as Year, 
        month(act.date_start) as Month, acc.billing_address_country as Country, Count(*) [CallCount]
    from accounts acc
        inner join activities act
            on acc.id = act.parent_id
        inner join users u
            on act.assigned_user_id = u.id
    where act.deleted = 0 and acc.deleted = 0 and act.project_id = 'some_id' 
        and act.parent_type = 'Account' and act.activity_type = 'Call'
        and act.status = 'Held' and act.date_start >='01-01-2012 00:00:01' 
        and act.date_start <='01-12-2013 00:00:01'
    group by u.first_name + ' ' + u.last_name, year(act.date_start), month(act.date_start), acc.billing_address_country
)
SELECT
  [SaleRep],[Year],[Month],[Country]
  100.0*[CallCount] / SUM([CallCount]) OVER(PARTITION BY [SaleRep],[Year],[Month]) AS [Percentage]
FROM t

SUM([CallCount]) OVER(PARTITION BY [SaleRep],[Year],[Month])计算多行的小计,而不使用GROUP BY。这使您可以在计算中将聚合与非聚合混合,这对于百分比来说是完美的。

答案 1 :(得分:0)

问题的第一部分是获取查询中可用的每个销售人员总数。我会使用额外的连接来做到这一点:

inner join (select act.assigned_user as user_id, count(*) as total 
            from activities 
            where -- ... replicate activity constraints
            group by) salesperson_totals on u.id = salesperson_totals.user_id

这意味着您现在可以使用总数,因此您可以使用正常的聚合函数计算出百分比 - 将其添加到您的选择查询中:

, 100 * COUNT(*) / salesperson_totals.total AS Percentage

您可能还需要将COUNT(*)salesperson_totals.total强制转换为浮点数,以便计算起作用。