复杂的SQL SUM查询

时间:2016-12-09 16:24:06

标签: sql sql-server sum

我正在做一个复杂的查询,它返回每个员工拥有的帐户和客户的数量。它目前看起来像这样:

select e.employee_id, p.first_name, p.last_name, clients.client_count, 
accounts.account_count, properties.property_count
from app.Employee e 
inner join app.Person p on e.person_id = p.person_id
inner join
(
select exc.employee_id, count(*) as client_count from app.client c
inner join 
(
    select employee_id, ecs.client_id from app.EmployeeClient ec
    inner join  
        (select client_id, max(effective_dt_tm) as effective from app.EmployeeClient ec where ec.employee_client_role_cd = 6001 group by client_id) ecs
        on ecs.client_id = ec.client_id and ecs.effective = ec.effective_dt_tm
        where ec.employee_client_role_cd = 6001 
    )exc on exc.client_id = c.client_id
    WHERE c.account_status_cd <> 4057
    group by exc.employee_id 
    )as clients on e.employee_id = clients.employee_id
inner join
(
    select exc.employee_id, count(*) as account_count from app.client c
    inner join app.account a on c.client_id = a.client_id
    inner join 
    (
        select employee_id, ecs.client_id from app.EmployeeClient ec
        inner join  
        (select client_id, max(effective_dt_tm) as effective from app.EmployeeClient ec where ec.employee_client_role_cd = 6001 group by client_id) ecs
        on ecs.client_id = ec.client_id and ecs.effective = ec.effective_dt_tm
        where ec.employee_client_role_cd = 6001 
    )exc on exc.client_id = c.client_id
    where a.active_ind = 1 AND a.account_status_reason_cd IS NULL
    group by exc.employee_id
)as accounts on e.employee_id = accounts.employee_id
inner join
(
    select exc.employee_id, count(*) as property_count from app.client c
    inner join app.account a on c.client_id = a.client_id
    inner join app.AccountProperty ap on a.account_id = ap.account_id
    inner join 
    (
        select employee_id, ecs.client_id from app.EmployeeClient ec
        inner join  
        (select client_id, max(effective_dt_tm) as effective from app.EmployeeClient ec where ec.employee_client_role_cd = 6001 group by client_id) ecs
        on ecs.client_id = ec.client_id and ecs.effective = ec.effective_dt_tm
        where ec.employee_client_role_cd = 6001 
    )exc on exc.client_id = c.client_id
    where ap.account_property_status_cd ! = 4255
    group by exc.employee_id 
)as properties on e.employee_id = properties.employee_id
ORDER BY employee_id

我需要添加一列,将每个员工的所有帐户的总帐单金额相加。我有一个功能已经计算了每个帐户的结算金额,可以使用这样的交叉申请将其添加到查询中:

cross apply(
    select total_billing_amt from [App].[fGetAccountBillingAmounts] (a.account_id,'2016-12-08')
    ) as billing_amounts

我认为这需要进入我计算总帐户的部分,因为这是我提取帐号的唯一地方,如下所示:

inner join
(
    select exc.employee_id, count(*) as account_count, billing_amounts.total_billing_amt from app.client c
    inner join app.account a on c.client_id = a.client_id
    inner join 
    (
        select employee_id, ecs.client_id from app.EmployeeClient ec
        inner join  
        (select client_id, max(effective_dt_tm) as effective from app.EmployeeClient ec where ec.employee_client_role_cd = 6001 group by client_id) ecs
        on ecs.client_id = ec.client_id and ecs.effective = ec.effective_dt_tm
        where ec.employee_client_role_cd = 6001 
    )exc on exc.client_id = c.client_id
    cross apply(
        select total_billing_amt from [App].[fGetAccountBillingAmounts] (a.account_id,'2016-12-08')
    ) as billing_amounts
    where a.active_ind = 1 AND a.account_status_reason_cd IS NULL
    group by exc.employee_id
)as accounts on e.employee_id = accounts.employee_id

这将运行正常,但会返回多个行,并为每个帐户结算总计列出多次相同的员工。我还没有弄清楚如何为每位员工总结一下。任何帮助表示赞赏。

谢谢!

1 个答案:

答案 0 :(得分:0)

你试过了吗?

cross apply(
    select sum(total_billing_amt) as total_billing_amt from [App].[fGetAccountBillingAmounts] (a.account_id,'2016-12-08')
) as billing_amounts

您的交叉申请可能会返回多行?