简体   繁体   English

SQL Server 2012:如何从多个表联接中获取计数组

[英]SQL Server 2012 : how to get count group by from multiple table joins

There's an invoice table, with the person who has created the invoice. 有一个发票表,其中包含创建发票的人。 A person can belong to multiple offices, only one main office per person but same person can have multiple roles per office. 一个人可以属于多个办公室,每个人只能有一个主要办公室,但同一个人每个办公室可以有多个角色。

declare @person table (personid int)
declare @office table (officeid int, officename varchar(10))
declare @personoffice table (personid int, officeid int, mainoffice bit, personrole varchar(10))
declare @invoice table (personid int)

insert into @person values (1), (2), (3), (4)
insert into @office values (1, 'office1'), (2, 'office2'), (3, 'office3'), (4, 'office4')
insert into @personoffice values (1, 1, 1, 'role1'), (1, 1, 1, 'role2'), (1, 2, 0, 'role1'), (1, 3, 0, 'rolex'), (2, 2, 1, 'role1'), (2, 2, 1, 'role2'), (2, 3, 0, 'rolex'), (3, 3, 1, 'role1'), (3, 4, 0, 'role2')
insert into @invoice values (1), (1), (1), (2), (2), (3), (3), (3), (3), (3)

So for this example we have 3 persons, they belong to multiple offices but only one main office each but some persons have multiple roles per office. 因此,在此示例中,我们有3个人,他们属于多个办公室,但每个办公室仅一个主办公室,但每个办公室中有些人具有多个角色。 They have each created multiple invoices. 他们每个都创建了多个发票。

I can get the number of invoices per person with: 我可以通过以下方式获得每人的发票数:

select 
    i.personid, 
    count(*) InvoiceCountByPerson
from 
    @invoice i
inner join 
    @person p on p.personid = i.personid
group by 
    i.personid

which returns: 返回:

personid    InvoiceCountByPerson
-------------------------------- 
    1               3
    2               2
    3               5

I need to get number of invoices by main office name. 我需要按总公司名称获取发票数量。 Person1 whose main office is office1 created 3 invoices, Person2 whose main office is office2 created 2 invoices, and Person3 whose main office is office3 created 5 invoices so expected result: 主要办公室是office1的Person1创建了3张发票,主要办公室是office2的Person2创建了2张发票,主要办公室是office3的Person3创建了5张发票,因此预期结果:

officename  InvoiceCountByOfficeName 
------------------------------------
office1              3
office2              2
office3              5

This doesn't work: 这不起作用:

select 
    o.officename,
    count(*) InvoiceCountByOfficeName
from 
    @invoice i
inner join 
    @person p on p.personid = i.personid
inner join 
    @personoffice po on po.personid = p.personid AND po.mainoffice = 1
inner join 
    @office o on o.officeid = po.officeid
group by 
    o.officename

as it returns: 返回:

officename  InvoiceCountByOfficeName 
-------------------------------------
office1                 6
office2                 4
office3                 5

As the same person has multiple mainoffice = 1 records with different roles, I need to have some sort of distinct on the @personoffice join. 由于同一个人具有多个具有不同角色的mainoffice = 1记录,因此我需要在@personoffice联接上具有某种不同的形式。 Millions of invoices too so need to take performance into consideration. 数以百万计的发票也是如此,因此需要考虑性能。

You are so close... All you had to do is use a derived table instead of using the @personoffice table directly: 您是如此亲密...您所要做的就是使用派生表而不是直接使用@personoffice表:

select 
    o.officename,
    count(*) InvoiceCountByOfficeName
from 
    @invoice i
inner join 
    @person p on p.personid = i.personid
inner join 
    (
        select distinct personid, officeid
        from @personoffice
        where mainoffice = 1
    )
     po on po.personid = p.personid 
inner join 
    @office o on o.officeid = po.officeid
group by 
    o.officename

Results: 结果:

officename InvoiceCountByOfficeName
---------- ------------------------
office1    3
office2    2
office3    5
    select 
        o.officename,
        count(*) InvoiceCountByOfficeName
    from 
        @invoice i
    inner join 
        @person p on p.personid = i.personid
    inner join 
        (
        select distinct personid,officeid,mainoffice from @personoffice
        ) po on po.personid = p.personid AND po.mainoffice = 1
    inner join 
        @office o on o.officeid = po.officeid
    group by 
        o.officename

Thanks 谢谢

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

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