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

curious

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

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. Millions of invoices too so need to take performance into consideration.

Zohar Peled

You are so close... All you had to do is use a derived table instead of using the @personoffice table directly:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get COUNT(*) from one partition of a table in SQL Server 2012?

From Dev

SQL Server 2012: Multiple select count(column1) in a single query from different table causing errors

From Dev

SQL - Get row count of table from a few joins away

From Dev

SQL Multiple table JOINS, GROUP BY and HAVING

From Dev

Get count of children from each parent.SQL Server 2012

From Dev

How to get the count of a join table in sql server?

From Dev

Multiple table joins with aggregate (mssql / sql server)

From Dev

How to get all columns from first table when there is no matching column in SQL Server 2012

From Dev

How to get all columns from first table when there is no matching column in SQL Server 2012

From Dev

Multiple sql joins from same reference table

From Dev

How to copy table from MS SQL Server 2012 to MonetDB

From Dev

How to delete a filegroup from a partitioned table (SQL Server 2012)

From Dev

SQL how to use multiple joins on the same table

From Dev

Multiple counts on the same SQL Server 2012 table

From Dev

How do I get multiple COUNT with multiple JOINS and multiple conditions?

From Dev

SQL Server multiple joins

From Dev

How to group by 3 columns -and- get a count in SQL Server?

From Dev

How to get category count using group by in sql server?

From Dev

get a count of each value from every column in a table SQL Server

From Dev

Get records count from sql server table in the most optimized way

From Dev

Get count of multiple table records with group by function

From Dev

Get Multiple COUNTS Multiple JOINS from Schedule table

From Dev

How to count records faster querying linked DB2 server from SQL SERVER 2012

From Dev

SQL Server: Multiple table joins with one or more WHERE clauses

From Dev

SQL Server: Multiple table joins with one or more WHERE clauses

From Dev

Get count for multiple fields using group by in SQL

From Dev

Dynamic PIVOT table SQL Server 2012 with COUNT() and SUM() and a TOTAL column

From Dev

How to use an SQL Query with multiple joins and count with hibernate

From Dev

Get values from multiple sql server table by one query

Related Related

  1. 1

    How to get COUNT(*) from one partition of a table in SQL Server 2012?

  2. 2

    SQL Server 2012: Multiple select count(column1) in a single query from different table causing errors

  3. 3

    SQL - Get row count of table from a few joins away

  4. 4

    SQL Multiple table JOINS, GROUP BY and HAVING

  5. 5

    Get count of children from each parent.SQL Server 2012

  6. 6

    How to get the count of a join table in sql server?

  7. 7

    Multiple table joins with aggregate (mssql / sql server)

  8. 8

    How to get all columns from first table when there is no matching column in SQL Server 2012

  9. 9

    How to get all columns from first table when there is no matching column in SQL Server 2012

  10. 10

    Multiple sql joins from same reference table

  11. 11

    How to copy table from MS SQL Server 2012 to MonetDB

  12. 12

    How to delete a filegroup from a partitioned table (SQL Server 2012)

  13. 13

    SQL how to use multiple joins on the same table

  14. 14

    Multiple counts on the same SQL Server 2012 table

  15. 15

    How do I get multiple COUNT with multiple JOINS and multiple conditions?

  16. 16

    SQL Server multiple joins

  17. 17

    How to group by 3 columns -and- get a count in SQL Server?

  18. 18

    How to get category count using group by in sql server?

  19. 19

    get a count of each value from every column in a table SQL Server

  20. 20

    Get records count from sql server table in the most optimized way

  21. 21

    Get count of multiple table records with group by function

  22. 22

    Get Multiple COUNTS Multiple JOINS from Schedule table

  23. 23

    How to count records faster querying linked DB2 server from SQL SERVER 2012

  24. 24

    SQL Server: Multiple table joins with one or more WHERE clauses

  25. 25

    SQL Server: Multiple table joins with one or more WHERE clauses

  26. 26

    Get count for multiple fields using group by in SQL

  27. 27

    Dynamic PIVOT table SQL Server 2012 with COUNT() and SUM() and a TOTAL column

  28. 28

    How to use an SQL Query with multiple joins and count with hibernate

  29. 29

    Get values from multiple sql server table by one query

HotTag

Archive