簡體   English   中英

SQL Server 2012:如何從多個表聯接中獲取計數組

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

有一個發票表,其中包含創建發票的人。 一個人可以屬於多個辦公室,每個人只能有一個主要辦公室,但同一個人每個辦公室可以有多個角色。

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
    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

謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM