Count(*)=0

Using the AdventureWorks database this query gives 3 rows.

select count(addressline1) from person.address
where addressline1='1970 Napa Ct.'
group by addressline1

Open in new window


Suppose I change addressline1 from '1970 Napa Ct.' to 'xxx'.  It will return nothing.
How can I amend the query to return a number zero if the address does not exist?
AlHal2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pcelbaCommented:
Remove GROUP BY from your query and it will return correct results.
You would need GROUP BY only when the column is listed in the SELECT list, e.g.:
select addressline1, count(*) from person.address
where addressline1 LIKE '1970%'
group by addressline1

Open in new window

0
ste5anSenior DeveloperCommented:
As a query can only return (existing) data, you need a second query:

SELECT  ISNULL(
    (
        SELECT  COUNT(*)
        FROM    person.address
        WHERE   addressline1 = '1970 Napa Ct.'
    ), 0) AS Cnt;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pcelbaCommented:
@ste5an Could you please tell when the COUNT(*) returns NULL?
0
AlHal2Author Commented:
Thanks.
0
ste5anSenior DeveloperCommented:
oops..
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.