Hello,
I'm terrible at SQL. I do not know if what I am trying to do is possible. But, because of our data structure, I need to solve this problem this way or do a massive architectural change.
I am trying to count the number of 'Provinces' (a.k.a States) for a Country. However, there are just a few Provinces that need to be ignored from the count. Because of this, I am trying to retrieve a list of countries, with a count of the provinces in each country.
As an example, I need to query for the United States, and ignore 'Washington D.C.' from the count. The reason why is because by our requirements, Washington D.C. is not a state. Here is what I am trying at the moment (it does not work):
SELECT
c.Name AS 'CountryName',
ISNULL(COUNT(p.[ID]), 0) as 'ProvinceCount'
FROM
Country c LEFT OUTER JOIN [Province] p ON p.[CountryID]=c.[ID]
WHERE
c.[ID]=@idParameter and
p.[Name] <> 'Washington D.C.'
As you can imagine, this query does not return any results when the idParameter matches that of the United States.
How do I get the correct count while figuring in exceptions? Thank you very much for your help.