If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > count(0) in the query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-23-10, 09:42
db2girl db2girl is offline
∞∞∞∞∞∞
 
Join Date: Aug 2008
Location: Toronto, Canada
Posts: 1,816
count(0) in the query

I'd like to understand the reason why someone would use count(0) or count(some number) in the query. For example:

db2 "select count(JOB) from staff"

1
-----------
35

1 record(s) selected.



db2 "select count(0) from (select count(JOB) from staff)"

1
-----------
1

1 record(s) selected.



db2 "select count(10) from (select count(JOB) from staff)"

1
-----------
1

1 record(s) selected.
Reply With Quote
  #2 (permalink)  
Old 03-23-10, 09:49
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by db2girl View Post
I'd like to understand the reason why someone would use count(0) or count(some number) in the query.
i think the main reason is to make you think

the COUNT() function counts, right?

and the main characteristic of how this function works is that it ignores NULL, yes?

so COUNT(0) and COUNT(1) and COUNT(937) will all return the exact same results for any given query, and the answer that they will return is exactly the number of rows that the query produces

the number of rows that the query produces, of course, depends entirely on which table(s) are involved, and whether there are any WHERE conditions

now see if you can apply this to your sample queries which return 1

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 03-23-10, 10:04
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
I guess in olden days the reasoning might have been that count(1) would result in fewer I/Os than count(*), but optimizers are (now?) smart enough not to access unneeded columns, so that does not matter anymore.
Reply With Quote
  #4 (permalink)  
Old 03-23-10, 17:52
db2girl db2girl is offline
∞∞∞∞∞∞
 
Join Date: Aug 2008
Location: Toronto, Canada
Posts: 1,816
Thank you all. I think I understand this now.

I also found Rudy's article about count(*) - count(1)
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On