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

标签 sql sql-server count sql-server-2012 group-by

有一个发票表,其中包含创建发票的人。一个人可以属于多个办公室,每个人只能有一个主要办公室,但同一个人可以在每个办公室担任多个角色。

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)

因此,在这个例子中,我们有 3 个人,他们属于多个办公室,但每个办公室只有一个主要办公室,但有些人每个办公室有多个角色。他们各自创建了多张发票。

我可以通过以下方式获取每人的发票数量:

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

返回:

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

我需要按主要办公室名称获取发票数量。主要办公室为office1的Person1创建了3张发票,主要办公室为office2的Person2创建了2张发票,主要办公室为office3的Person3创建了5张发票,因此预期结果:

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 
    @personoffice po on po.personid = p.personid AND po.mainoffice = 1
inner join 
    @office o on o.officeid = po.officeid
group by 
    o.officename

返回时:

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

由于同一个人有多个具有不同角色的 mainoffice = 1 记录,我需要在 @personoffice 连接上有某种不同的记录。也有数百万张发票,因此需要考虑性能。

最佳答案

您已经很接近了...您所要做的就是使用派生表,而不是直接使用 @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

结果:

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

关于SQL Server 2012 : how to get count group by from multiple table joins,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38114577/

相关文章:

sql - 连接4张 table

sql - 使用自动生成的列填充另一列

mysql - 将两个查询合并为一个

sql - Oracle在DATE使用LIKE '%'

sql - 在具有多个条件的 SQL 中选择 IF

php - MYSQL,使用多个SUMS,而不是将多个列加在一起

mysql - 如何将两个查询与 case 语句结合起来

mysql - 如何使用 MySQL 根据第一个表中特定列中的值从 2 个表中获取记录数

mysql - 插入多行时如何使用 "ON DUPLICATE KEY UPDATE"增加 MySql 中的字段?

c# - 值不能为空 : connection while testing database