SQL Server How do I do multiple counts for each distinct manager ID? [closed]

avkwfej4  于 2023-11-16  发布在  其他
关注(0)|答案(1)|浏览(100)

Closed. This question needs details or clarity . It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post .

Closed 2 days ago.
Improve this question

I just need to figure out how to count the number of buildings each manager manages. I think I just need help for the syntax.

-- 5.2.10 Display the MFName, MLName, MSalary, MBdate, and number of buildings that the manager manages for all managers with a salary less than $55,000 

Select MFName, MLName, MSalary, MBDate,     
COUNT(BuildingID) as NoOfBuildManaged    
From Manager, Building
Where Manager.ManagerID = Building.BManagerID    
AND MSalary < 55000    
Group By ManagerID;
ie3xauqp

ie3xauqp1#

WITH
  building_count AS
(
  SELECT
    BManagerID,
    COUNT(*) AS NoOfBuildManaged
  FROM
    Building
  GROUP BY
    BManagerID
)
SELECT
  m.MFName,
  m.MLName,
  m.MSalary,
  m.MBDate,     
  b.NoOfBuildManaged    
FROM
  Manager          AS m
INNER JOIN
  building_count   AS b
    ON m.ManagerID = b.BManagerID    
WHERE
  m.MSalary < 55000    
;

相关问题