Give the following simple table structure:
Departments PK - DeptID DeptName -------------------------- 1 Department 1 2 Department 2 3 Department 3 4 Department 4 Groups PK - GroupdID DeptID -------------------------- 1 1 2 1 3 3 4 4 5 2 6 3 7 1 8 3 Inventory PK - ItemID GroupID -------------------------- 1 2 2 3 3 8 4 1 5 4 6 5 7 1 8 2 9 2 10 3 11 7
Is there a way without using subqueries (which is easy) where I could get a listing of the departments, the count of the groups in each department, and the count of the inventory in each department?
Example Output:
DeptID DeptName GroupCount ItemCount
-----------------------------------------------------
1 Department 1 3 6
2 Department 2 1 1
3 Department 1 3 3
4 Department 4 1 1
My gut is telling me it's just a simple matter of getting the GROUP BY statements correct, but so far I'm drawing a blank. If it does require the use of subqueries, this isn't a problem. I just wanted to confirm for future reference.
NOTE: Using SQL Server 2000 for this particular problem