|
sql
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Get the Count function to return zero instead of nothing at all?
If no rows match the criteria, I want the Count function to return a zero
instead of nothing at all. How do I do that? Eg: this query returns nothing at all because nobody has a lastname of 'xxx'. I want it to return a count of zero. SELECT LastName, COUNT(LastName) AS CountNames FROM dbo.People GROUP BY LastName HAVING (LastName = N'xxx') It does returns zero
CREATE TABLE #tmp(c int) SELECT COUNT(c) FROM #tmp Show quoteHide quote "gocat2005" <gocat2***@discussions.microsoft.com> wrote in message news:E79B8223-5830-423A-A41D-9CFD59ACD086@microsoft.com... > If no rows match the criteria, I want the Count function to return a zero > instead of nothing at all. How do I do that? Eg: this query returns > nothing > at all because nobody has a lastname of 'xxx'. I want it to return a count > of > zero. > > SELECT LastName, COUNT(LastName) AS CountNames > FROM dbo.People > GROUP BY LastName > HAVING (LastName = N'xxx') Not if you add a filter
HAVING (c = 2) Show quoteHide quote "Uri Dimant" wrote: > It does returns zero > CREATE TABLE #tmp(c int) > > SELECT COUNT(c) FROM #tmp > > > > > > "gocat2005" <gocat2***@discussions.microsoft.com> wrote in message > news:E79B8223-5830-423A-A41D-9CFD59ACD086@microsoft.com... > > If no rows match the criteria, I want the Count function to return a zero > > instead of nothing at all. How do I do that? Eg: this query returns > > nothing > > at all because nobody has a lastname of 'xxx'. I want it to return a count > > of > > zero. > > > > SELECT LastName, COUNT(LastName) AS CountNames > > FROM dbo.People > > GROUP BY LastName > > HAVING (LastName = N'xxx') > > > You cannot do that the way you have the query structured, because no
rows exist for that name (and a group for the name value cannot be created). But you can do something like this: SELECT COALESCE(( SELECT COUNT(LastName) AS CountNames FROM dbo.People GROUP BY LastName HAVING LastName = N'xxx'), 0) AS cnt; Hi Plamen
That looks promising...I just need to work out how to pass the filter criteria to the query from a webpage. This may be difficult as the Lastname column is not available when I use coalesc. Is there a way of including several columns? Show quoteHide quote "Plamen Ratchev" wrote: > You cannot do that the way you have the query structured, because no > rows exist for that name (and a group for the name value cannot be > created). But you can do something like this: > > SELECT COALESCE(( > SELECT COUNT(LastName) AS CountNames > FROM dbo.People > GROUP BY LastName > HAVING LastName = N'xxx'), 0) AS cnt; > > -- > Plamen Ratchev > http://www.SQLStudio.com > You can create a stored procedure with parameter "@last_name
NVARCHAR(30)" (change to correct length), call the stored procedure from your web page passing the parameter. Then the query can be like this (note you can add more columns to the second subquery and then use them in the final SELECT): SELECT T.LastName, COALESCE(P.CountNames, 0) AS cnt FROM (SELECT @last_name) AS T(LastName) LEFT JOIN ( SELECT LastName, COUNT(*) AS CountNames FROM dbo.People GROUP BY LastName) AS P ON T.LastName = P.LastName; The query result needs to show lastname and count of lastname eg:
Smith 0 Show quoteHide quote "gocat2005" wrote: > If no rows match the criteria, I want the Count function to return a zero > instead of nothing at all. How do I do that? Eg: this query returns nothing > at all because nobody has a lastname of 'xxx'. I want it to return a count of > zero. > > SELECT LastName, COUNT(LastName) AS CountNames > FROM dbo.People > GROUP BY LastName > HAVING (LastName = N'xxx')
Other interesting topics
How to Set Read-Only Permissions on All Objects in a Schema
security in SQL 2008 after moving to a domain? Creating a table on a particular filegroup? FlatFile Encryption using FILESTREAM To UDF or not to UDF Updation of data in Active/Active cluster Populating an IDentity and and timestamp field? Transfer Data in Code Table design for optimal performance ? sp_MSget_repl_commands (Distribution agent constantly running and locking) |
|||||||||||||||||||||||