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 > condition on count

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-29-09, 02:22
rpkulkarni rpkulkarni is offline
Registered User
 
Join Date: Nov 2009
Posts: 11
condition on count

Hello DB2 gurus,

I am really new to DB2 and SQL programming and facing problems in small-small issues like mentioned below. Kindly help me in resolving the issue.

I have person and city as two columns in the table1 and I want to get cities with count(person) > 1000.
The query is like this :
"select city, count(*) from table1 where count(*) > 1000 group by city"
But I cannot write condition like this in where clause.
How to write this where clause correctly?

Thanks,
rpkulkarni
Reply With Quote
  #2 (permalink)  
Old 12-29-09, 05:03
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Use HAVING. Like this....

select city, count(*) from table1 group by city HAVING count(*) > 1000

Last edited by tonkuma; 12-29-09 at 05:07.
Reply With Quote
  #3 (permalink)  
Old 12-29-09, 06:12
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Have a look at the sequence how SQL statements are processed. You will find that a WHERE clause is applied to the values of a single row only. Thus, having an aggregation function there doesn't make any sense. HAVING clauses (as tonkuma suggested) are applied after any GROUP BY clause and apply to a whole group of rows. (If there is no GROUP BY clause, you have implicitly a single group containing all rows resulting from the SELECT ... FROM ... WHERE steps.)
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #4 (permalink)  
Old 12-29-09, 06:13
rpkulkarni rpkulkarni is offline
Registered User
 
Join Date: Nov 2009
Posts: 11
Thanks a lot

The question is answered
Reply With Quote
  #5 (permalink)  
Old 12-29-09, 06:25
rpkulkarni rpkulkarni is offline
Registered User
 
Join Date: Nov 2009
Posts: 11
Thank you Tonkuma and Knut Stolze for your replies.

Regards,
rpkulkarni
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