【发布时间】:2016-11-02 01:05:51
【问题描述】:
有一个发票表,其中包含创建发票的人。一个人可以属于多个办公室,每个人只能拥有一个主要办公室,但同一个人可以在每个办公室担任多个角色。
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 连接上进行某种区分。数百万张发票也需要考虑性能。
【问题讨论】:
-
每次阅读格式良好的 sql 问题时,我都非常高兴 :-)。 +1 仅用于包含所有相关细节。
-
inner join (select distinct ... from @personoffice)? -
那么您需要发票属于第一个主要办公室吗?
-
@DhruvJoshi 每人只有一个主要办公室。
标签: sql sql-server count sql-server-2012 group-by