Count of table rows from a joined table?

I have the following working store procedure:

SELECT M.*,MS.Status, C.Country, U.Title, U.Firstname, U.Surname FROM Meeting M 
	LEFT JOIN Country C ON C.ID = M.CountryID 
	LEFT JOIN [User] U on U.ID = M.UseriD 
	LEFT JOIN MeetingStatus MS on MS.ID = M.StatusID 
	(@MeetingID = -1 OR M.ID = @MeetingID) AND
	(@InitiativeID = -1 OR M.InitiativeID = @InitiativeID) AND
	(@UserID = -1 OR UserID = @UserID) AND 
	(@MDate = '' OR M.MeetingDate = @MDate) AND
	(@PostalCode = '' OR M.PostalCode = @PostalCode) AND 
	(@CountryID = -1 OR M.CountryID = @CountryID) AND
	(@Longitude = 0 OR M.Longitude = @longitude) AND
	(@Latitude = 0 OR M.Latitude = @Latitude) AND  
	(@Deleted = -1 OR M.Deleted = @Deleted)

Open in new window

Which pulls back a list of meetings  perfectly. The thing is; I need to add in the below SQL statement as a new column("AttendeeCount") in the above stored procedure, so we can see how many attendees there are for each meeting;

SELECT COUNT(MA.ID) FROM MeetingAttendee MA WHERE MA.MeetingID = @MeetingID

Open in new window

Obviously, when added in to the stored procedure "MA.MeetingID = M.ID"

How do I do this...
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shaun KlineLead Software EngineerCommented:
You can add the query directly to the SELECT portion:
SELECT M.*,MS.Status, C.Country, U.Title, U.Firstname, U.Surname,
  (SELECT COUNT(MA.ID) FROM MeetingAttendee MA WHERE MA.MeetingID = M.ID) AS MeetingCount

Open in new window

This is called a correlated query.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Since @MeetingID is a single value, try an uncorrrelated subquery
SELECT blah, blah, blah, (SELECT COUNT(ID) FROM MeetingAttendee WHERE MA.MeetingID = @MeetingID ) as aattendee_count
FROM blah
  LEFT JOIN blah blah blah

Open in new window

If it weren't for needing a single @MeetingID, a JOIN would have also worked to get all meetings and attendee counts.
SELECT blah, blah, blah, 
FROM blah
   JOIN (SELECT MeetingID, COUNT(ID) FROM MeetingAttendee GROUP BY MeetingID) ac ON m.MeetingID = ac.MeetingID

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.