dbTalk Databases Forums  

Making Count(*) return zero

comp.databases.theory comp.databases.theory


Discuss Making Count(*) return zero in the comp.databases.theory forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Guto
 
Posts: n/a

Default Making Count(*) return zero - 12-09-2009 , 03:01 PM






Hi There!

is there a way to make the count(*) return zero . because the default
behavoir is that it will skip this value if there is no records so a
simple query like this

select userid,count(*) as count from users where userid in (select val
from sometable)
group by userid

if userid 1 has no records . it wont be returned in the query
instead i want it to show zero so
userid count
1 0
2 3
3 1
4 0

Any hints?
Thanks!
Guto.

Reply With Quote
  #2  
Old   
Vadim Tropashko
 
Posts: n/a

Default Re: Making Count(*) return zero - 12-09-2009 , 03:27 PM






On Dec 9, 12:01*pm, Guto <gutom... (AT) gmail (DOT) com> wrote:
Quote:
Hi There!

is there a way to make the count(*) return zero . because the default
behavoir is that it will skip this value if there is no records so a
simple query like this

select userid,count(*) as count from users where userid in (select val
from sometable)
group by userid

if userid 1 has no records . it wont be returned in the query
instead i want it to show zero so
userid count
1 0
2 3
3 1
4 0

Any hints?
Thanks!
Guto.
From "SQL Design Patterns"

<quote><soapbox>Hugh Darwen’s Summarize

Hugh Darwen argued that group by with aggregation is an operator that
requires two arguments, in general. The idea of introducing such an
operator in SQL never caught on. Yet, in each practical situation it
might be useful to double check if writing group by clause as a one-
or two- argument operator is more appropriate.
</soapbox>

In SQL:
select deptno,
(select count(*) from Emp e
where e.deptno = d.deptno)
from Dept d
</quote>

My understanding evolved a little bit since then. One can argue that
aggregation with grouping is domain dependent operation, so that it
would return all the values in the domain, the ones not listed in
input relation with count 0. To make it safe, you can join it with
some other relation. This is how QBQL approaches it.

Reply With Quote
  #3  
Old   
Bob Badour
 
Posts: n/a

Default Re: Making Count(*) return zero - 12-09-2009 , 03:27 PM



Guto wrote:

Quote:
Hi There!

is there a way to make the count(*) return zero . because the default
behavoir is that it will skip this value if there is no records so a
simple query like this

select userid,count(*) as count from users where userid in (select val
from sometable)
group by userid

if userid 1 has no records . it wont be returned in the query
instead i want it to show zero so
userid count
1 0
2 3
3 1
4 0

Any hints?
Thanks!
Guto.
Use a scalar subquery instead of just count(*) to calculate the [count]
column.

--
is there something in it for them, like maybe bailouts, if they can
panic us into doing something politically to cover them?

November 19, 2007 - John S Bolton

http://tinyurl.com/y9e4vxh

Reply With Quote
  #4  
Old   
Gints Plivna
 
Posts: n/a

Default Re: Making Count(*) return zero - 12-09-2009 , 04:41 PM



Quote:
if userid 1 has no records . it wont be returned in the query
instead i want it to show zero so
userid count
1 0
2 3
3 1
4 0
Scalar subqueries as already said or left join as follows:
select userid, count(sometable.val)
from users left join sometable
on (users.userid = sometable.val)
group by userid

Gints Plivna
http://www.gplivna.eu

Reply With Quote
  #5  
Old   
Guto
 
Posts: n/a

Default Re: Making Count(*) return zero - 12-10-2009 , 07:12 AM



On Dec 9, 7:41*pm, Gints Plivna <gints.pli... (AT) gmail (DOT) com> wrote:
Quote:
if userid 1 has no records . it wont be returned in the query
instead i want it to show zero so
userid count
1 0
2 3
3 1
4 0

Scalar subqueries as already said or left join as follows:
select userid, count(sometable.val)
from users left join sometable
on (users.userid = sometable.val)
group by userid

Gints Plivnahttp://www.gplivna.eu
Thank you all for your reply, but actually I need to be more precise
on the issue:

1) I have a list of ID
2) I need to return the count() of these ID even if they are 0
3) Its everything on the same table.

My sql sentence is going like this:
select ID,count(*)
from Service
where ID in (51,55,73)
and (InicialDate is null or InicialDate > '09/12/2008')
group by ID
resulting in :
ID Column2
------------ ----------
55 11454
73 1

What I need is :

ID Column2
------------ ----------
51 0
55 11454
73 1


Thank you all.

Reply With Quote
  #6  
Old   
Bob Badour
 
Posts: n/a

Default Re: Making Count(*) return zero - 12-10-2009 , 10:27 AM



Guto wrote:
Quote:
On Dec 9, 7:41 pm, Gints Plivna <gints.pli... (AT) gmail (DOT) com> wrote:

if userid 1 has no records . it wont be returned in the query
instead i want it to show zero so
userid count
1 0
2 3
3 1
4 0

Scalar subqueries as already said or left join as follows:
select userid, count(sometable.val)
from users left join sometable
on (users.userid = sometable.val)
group by userid

Gints Plivnahttp://www.gplivna.eu


Thank you all for your reply, but actually I need to be more precise
on the issue:

1) I have a list of ID
2) I need to return the count() of these ID even if they are 0
3) Its everything on the same table.

My sql sentence is going like this:
select ID,count(*)
from Service
where ID in (51,55,73)
and (InicialDate is null or InicialDate > '09/12/2008')
group by ID
resulting in :
ID Column2
------------ ----------
55 11454
73 1

What I need is :

ID Column2
------------ ----------
51 0
55 11454
73 1

Thank you all.
The dbms cannot makes something out of nothing. You will need another
relation to join against that has all of the ids.

--
is there something in it for them, like maybe bailouts, if they can
panic us into doing something politically to cover them?

November 19, 2007 - John S Bolton

http://tinyurl.com/y9e4vxh

Reply With Quote
  #7  
Old   
Hugo Kornelis
 
Posts: n/a

Default Re: Making Count(*) return zero - 12-11-2009 , 07:05 PM



On Thu, 10 Dec 2009 04:12:33 -0800 (PST), Guto wrote:

(snip)
Quote:
Thank you all for your reply, but actually I need to be more precise
on the issue:

1) I have a list of ID
2) I need to return the count() of these ID even if they are 0
3) Its everything on the same table.

My sql sentence is going like this:
select ID,count(*)
from Service
where ID in (51,55,73)
and (InicialDate is null or InicialDate > '09/12/2008')
group by ID
Hi Guto,

As Bob says, you need a table source for these ID values, otherwise the
DBMS can't produce rows for them. But you can include such a source in
your query:

In Microsoft SQL Server, you can use something this:

WITH MyIDs(ID)
AS (SELECT 51 UNION ALL SELECT 55 UNION ALL SELECT 73)
SELECT s.ID, COUNT(*)
FROM Service AS s
INNER JOIN MyIDs AS m
ON m.ID = s.ID
WHERE m.InicialDate IS NULL
OR m.InicialDate > '20081209'; -- 09/12/2008 is ambiguous!

In other DBMSs, you can use something like this, which is ANSI standard
syntax. I use SQL Server, which does not implement this feature, so I
might have the syntax wrong.

WITH MyIDs(ID)
AS (VALUES ((51), (53), (73))
SELECT s.ID, COUNT(*)
FROM Service AS s
INNER JOIN MyIDs AS m
ON m.ID = s.ID
WHERE m.InicialDate IS NULL
OR m.InicialDate > '20081209'; -- 09/12/2008 is ambiguous!


Best, Hugo

Reply With Quote
  #8  
Old   
TroyK
 
Posts: n/a

Default Re: Making Count(*) return zero - 12-16-2009 , 04:56 PM



On Dec 11, 6:05*pm, Hugo Kornelis
<h... (AT) perFact (DOT) REMOVETHIS.info.INVALID> wrote:
Quote:
On Thu, 10 Dec 2009 04:12:33 -0800 (PST), Guto wrote:

(snip)

Thank you all for your reply, but actually I need to be more precise
on the issue:

1) I have a list of ID
2) I need to return the count() of these ID even if they are 0
3) Its everything on the same *table.

My sql sentence is going like this:
select ID,count(*)
* *from Service
* *where ID in (51,55,73)
* *and (InicialDate is null or InicialDate > '09/12/2008')
group by ID

Hi Guto,

As Bob says, you need a table source for these ID values, otherwise the
DBMS can't produce rows for them. But you can include such a source in
your query:

In Microsoft SQL Server, you can use something this:

WITH MyIDs(ID)
*AS (SELECT 51 UNION ALL SELECT 55 UNION ALL SELECT 73)
SELECT * * s.ID, COUNT(*)
FROM * * * Service AS s
INNER JOIN MyIDs AS m
* * * ON * m.ID = s.ID
WHERE * * *m.InicialDate IS NULL
OR * * * * m.InicialDate > '20081209'; -- 09/12/2008 is ambiguous!

In other DBMSs, you can use something like this, which is ANSI standard
syntax. I use SQL Server, which does not implement this feature, so I
might have the syntax wrong.

WITH MyIDs(ID)
*AS (VALUES ((51), (53), (73))
SELECT * * s.ID, COUNT(*)
FROM * * * Service AS s
INNER JOIN MyIDs AS m
* * * ON * m.ID = s.ID
WHERE * * *m.InicialDate IS NULL
OR * * * * m.InicialDate > '20081209'; -- 09/12/2008 is ambiguous!

Best, Hugo
Actually, SS2K8 does now include support for the syntax, FWIW:

SELECT ID
FROM (VALUES(51),(53),(55)) AS Foo(ID)

Take Care,
TroyK

Reply With Quote
  #9  
Old   
Norbert_Paul
 
Posts: n/a

Default Re: Making Count(*) return zero - 03-15-2010 , 10:27 AM



If you use 'group by' then 'count(*)' can never return zero.
This is for mathematical reasons: 'group by' creates
equivalence classes and count(*) returns each
equivalence class' size. An equivalence class, however,
is not empty, hence, count(*) must be greater or equal
to 1.

You might wish to use 'union':

select id,count(*) as idcount
from ...
group by id
union all
select id, 0 as idcount
from ...

(not tested)

Norbert

Guto wrote:
Quote:
On Dec 9, 7:41 pm, Gints Plivna<gints.pli... (AT) gmail (DOT) com> wrote:
if userid 1 has no records . it wont be returned in the query
instead i want it to show zero so
userid count
1 0
2 3
3 1
4 0

Scalar subqueries as already said or left join as follows:
select userid, count(sometable.val)
from users left join sometable
on (users.userid = sometable.val)
group by userid

Gints Plivnahttp://www.gplivna.eu

Thank you all for your reply, but actually I need to be more precise
on the issue:

1) I have a list of ID
2) I need to return the count() of these ID even if they are 0
3) Its everything on the same table.

My sql sentence is going like this:
select ID,count(*)
from Service
where ID in (51,55,73)
and (InicialDate is null or InicialDate> '09/12/2008')
group by ID
resulting in :
ID Column2
------------ ----------
55 11454
73 1

What I need is :

ID Column2
------------ ----------
51 0
55 11454
73 1


Thank you all.

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.