Solved Count by one Query (1 Viewer)

Sarah.M

Member
Local time
Today, 14:26
Joined
Oct 28, 2021
Messages
335
Hi, Plz note, My VBA is blocked by IT for security reason (Cybersecurity).
I can use only Expression builder, Macros, Queries, Tables, Forms and Reports only.
------------------------------------------------
I have
EmployeeT
PhoneT
SectionT
I want to Count
How many employee in each Section? and
How many phone for each employee?
all of them in one Query 👀
I tried to count the Section it works but If I add phone fields, I got wrong result :cautious: :(o_O
please help me! 🙏🙋‍♀️
Sample Attachment
 

Attachments

  • CountSectionAndPhone.accdb
    740 KB · Views: 90

plog

Banishment Pending
Local time
Today, 06:26
Joined
May 11, 2011
Messages
11,646
You cannot do this in one query because you are counting at different levels (By employee and By section). What you can do is generate a report with multiple sections and get your data all on one report.

So, build a query for each dataset you want, make sure it shows correct data. Then build a report based on each query, then build another report that uses both those reports as subreports.
 

XPS35

Active member
Local time
Today, 13:26
Joined
Jul 19, 2022
Messages
159
As said, you can't actually do this in one query. If you really want to, you can try something like:
Code:
SELECT SectionName AS [Section/Employee], Count(EmployeeID) AS [Employees/Phones]
FROM SectionT RIGHT JOIN EmployeeT ON SectionT.SectionID = EmployeeT.SecIDFK
GROUP BY SectionT.SectionName
UNION 
SELECT FullName, Count(PhoneID) AS CountOfPhoneID
FROM EmployeeT INNER JOIN PhoneT ON EmployeeT.EmployeeID = PhoneT.EmpIDFK
GROUP BY FullName;
 

June7

AWF VIP
Local time
Today, 03:26
Joined
Mar 9, 2014
Messages
5,472
Suggest another field in each SELECT to identify the category of Count data. First SELECT defines data types and field names. Advise not to use punctuation/special characters in naming convention - only exception is underscore.
Code:
SELECT SectionName AS [Section_Employee], Count(EmployeeID) AS [Employees_Phones], "EmpCnt" AS Cat
FROM SectionT RIGHT JOIN EmployeeT ON SectionT.SectionID = EmployeeT.SecIDFK
GROUP BY SectionT.SectionName
UNION
SELECT FullName, Count(PhoneID), "PhoneCnt"
FROM EmployeeT INNER JOIN PhoneT ON EmployeeT.EmployeeID = PhoneT.EmpIDFK
GROUP BY FullName;
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 19:26
Joined
May 7, 2009
Messages
19,244
you can also create forms to show your info
 

Attachments

  • CountSectionAndPhone.accdb
    1.9 MB · Views: 80

Users who are viewing this thread

Top Bottom