I have a query like this that returns number of rows for each case in city .
select
case edition_id
when 6 then 'DELHI'
when 50 then 'AHMEDABAD'
when 4 then 'HYDERABAD'
when 25 then 'KOLKATA'
when 51 then 'BANGALORE'
when 5 then 'MUMBAI'
when 24 then 'CHENNAI'
end as CITY,
count(*) as Total
from #tmptab1
group by edition_id
drop table #tmptab1
The result comes out to be like
CITY Total
MUMBAI 1
DELHI 28
CHENNAI 1
KOLKATA 35
AHMEDABAD 3
So if there is no rows returned from a city , that city is omitted in final result
I want result as
CITY Total
MUMBAI 1
DELHI 28
CHENNAI 1
KOLKATA 35
AHMEDABAD 3
BANGALORE 0 -- if no result from bangalore display zero.
How to do this ?
I tried
case count(*)>0 then count(*) else 0 end as Total
but it does not work
The SQL COUNT() function returns the number of rows in a table satisfying the criteria specified in the WHERE clause. It sets the number of rows or non NULL column values. COUNT() returns 0 if there were no matching rows.
It's all about the JOIN type. Using the suitable JOIN is crucial when you want to include zeros in the COUNT() aggregate. If you now use the aggregate function COUNT() , like in the code above, it will not count the NULL values and the result will be zero.
COUNT(*) will count the number of rows, while COUNT(expression) will count non-null values in expression and COUNT(column) will count all non-null values in column. Since both 0 and 1 are non-null values, COUNT(0)=COUNT(1) and they both will be equivalent to the number of rows COUNT(*) .
SQL SELECT COUNT() can be clubbed with SQL WHERE clause. Using the WHERE clause, we have access to restrict the data to be fed to the COUNT() function and SELECT statement through a condition.
I would insert the cities into a temporary table, then do a LEFT JOIN with the grouping query as follows:
CREATE TABLE #cities (edition_id INT, city VARCHAR(16))
INSERT INTO #cities VALUES(6, 'DELHI')
INSERT INTO #cities VALUES(50, 'AHMEDABAD')
INSERT INTO #cities VALUES(4, 'HYDERABAD')
INSERT INTO #cities VALUES(25, 'KOLKATA')
INSERT INTO #cities VALUES(51, 'BANGALORE')
INSERT INTO #cities VALUES(5, 'MUMBAI')
INSERT INTO #cities VALUES(24, 'CHENNAI')
select
c.city 'City',
ISNULL(t.Total, 0) 'Total'
from
#cities c
LEFT JOIN (
SELECT
edition_id, count(*) as Total
#tmptab1
GROUP BY edition_id
) AS t
ON c.edition_id = t.edition_id
drop table #tmptab1
drop table #cities
BTW, it would make sense to have #cities
as a normal table so that you don't need to create it everytime the query runs.
The problem is that you are grouping by edition_id. If there is no edition_id in your result then it can't count it.
What you can do instead is select all the cities out with their edition id, left join it to the counts and then do an isnull:
WITH CITIES AS
(
SELECT 6 AS edition_id, 'DELHI' As CityName
UNION
SELECT 50, 'AHMEDABAD'
UNION
....
)
SELECT c.cityname, isnull(counts.total,0) as total
FROM CITIES
LEFT JOIN (SELECT edition_id, count(*) as Total #tmptab1 group by edition_id) counts ON counts.edition_id = CITIES.edition_id
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With