I am trying to count the number of distinct email addresses in a view, but I can't figure out the syntax... Here's what I have:
BEGIN
SELECT COUNT(*) AS UserCount FROM (
SELECT DISTINCT EmailAddress
FROM viewCohortsAuthorizedByContractor
WHERE (ListAccess = 1) AND (ContractorId = @id)
)
END
The inner select works fine, but if I try adding the COUNT, I get the following error: Msg 156, Level 15, State 1, Procedure rptContractorUsersWithListUserCount, Line 23 [Batch Start Line 15] Incorrect syntax near the keyword 'END'.
I'm sure there's a simple solution, I just can't figure it out...
SELECT COUNT(*) AS UserCount FROM (...) as Q. The keyword AS isn't necessary -SELECT COUNT(*) AS UserCount FROM (...) Q. In your case SQLServer think thatENDis your alias. Early SteveB showed is more rightly query for your case.