arthrex
asked on
SQL Count with Null values
Hi experts,
I have an sql query:
SELECT Count(*) AS NationalParticipantsCount, Country
FROM Participant
GROUP BY Country
HAVING Country = 'Germany'
Now the table Participant is empty in the beginning. So I get an empty response.
How do I modify my statement so it will display NationalParticipantsCount as "0" ?
Thanks a lot
I have an sql query:
SELECT Count(*) AS NationalParticipantsCount,
FROM Participant
GROUP BY Country
HAVING Country = 'Germany'
Now the table Participant is empty in the beginning. So I get an empty response.
How do I modify my statement so it will display NationalParticipantsCount as "0" ?
Thanks a lot
SELECT IsNull( Count(*), 0) AS NationalParticipantsCount, Country
FROM Participant
GROUP BY Country
HAVING Country = 'Germany'
FROM Participant
GROUP BY Country
HAVING Country = 'Germany'
Take what Angel suggested and wrap with IsNull.
SELECT IsNull(Count(*), 0) AS NationalParticipantsCount
FROM Participant
WHERE Country = 'Germany'
Or mastoo's suggestion. Didn't see your post until mine went through, mastoo. :)
Never mind mine :-)
you could UNION it with another sql statement that returns a blank row or a 0 when that condition is met.
That way you get a 0 returned when the table is empty or that group/having condition is not met and when it is, the UNION sql never succeeds.
That way you get a 0 returned when the table is empty or that group/having condition is not met and when it is, the UNION sql never succeeds.
ASKER
Hey guys, thanks for your replies.
Angelll's solution works, but I need the country column in the result.
And mastoo's solution doesn't work for me.
The ISNULL function doesn't hit because the values aren't NULL they are actually empty.
Angelll's solution works, but I need the country column in the result.
And mastoo's solution doesn't work for me.
The ISNULL function doesn't hit because the values aren't NULL they are actually empty.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
And as far as the IsNull goes, the COUNT and other aggregates usually optimize out NULLs anyway -- I just forgot that when posted earlier. What is happening is that if you don't have any records, then you can't display country using the column name. You have to use a literal. If you have mutiple values, I would use a UNION as suggested earlier.
ASKER
Thanks mwvisa1,
that was the solution
that was the solution
You are welcome.
Open in new window