Saturday, 14 September 2013

How to query count with 0 rows

How to query count with 0 rows

My query is to get the count of total rows, along with the columns. An
example table, which has to be a temp table is:
temp(a1,a2)
I need to query a1,a2, and total number of rows.
SELECT COUNT(*) as TotalRow, a1, a2 from #temp group by a1,a2
The query works fine for table with rows >=1. However, for some special
case, this table has zero row. In that case, TotalRow does not return a
value (it should be zero). How can I get that query work for all cases
including zero row? Thanks.

No comments:

Post a Comment