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 > Data Access, Manipulation & Batch Languages > ANSI SQL > How to figure out if the number of records is odd?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-16-11, 18:57
dbGuyo dbGuyo is offline
Registered User
 
Join Date: Jul 2011
Posts: 9
Red face How to figure out if the number of records is odd?

Hey guys,

I am going to print 'odd' if the number of records of a table is odd.
Looks so simple but I couldn't so far.

Any idea?
Reply With Quote
  #2 (permalink)  
Old 08-17-11, 02:12
JarlH JarlH is offline
Registered User
 
Join Date: Dec 2008
Location: At work...
Posts: 68
select case when mod(count(*),2) = 1 then 'odd' else 'even' end
from yourtablename
Reply With Quote
  #3 (permalink)  
Old 08-17-11, 02:38
dbGuyo dbGuyo is offline
Registered User
 
Join Date: Jul 2011
Posts: 9
Thank you for trying this.
But aggregation functions cannot be used in Where clause!
Reply With Quote
  #4 (permalink)  
Old 08-17-11, 02:50
JarlH JarlH is offline
Registered User
 
Join Date: Dec 2008
Location: At work...
Posts: 68
select 'odd' from sometablename having mod(count(*),2) = 1
Reply With Quote
  #5 (permalink)  
Old 08-17-11, 04:48
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
JarlH didn't use a WHERE clause at all. Or do you mean you don't want to have any result (empty table) in case you have an even number of rows?
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #6 (permalink)  
Old 08-17-11, 04:53
dbGuyo dbGuyo is offline
Registered User
 
Join Date: Jul 2011
Posts: 9
Thanks guys. Both solutions work fine.
At first glance, I didn't notice that you were using 'case'.

Last edited by dbGuyo; 08-17-11 at 05:06.
Reply With Quote
  #7 (permalink)  
Old 08-17-11, 05:03
JarlH JarlH is offline
Registered User
 
Join Date: Dec 2008
Location: At work...
Posts: 68
Quote:
Originally Posted by dbGuyo View Post
Have you tried it? It doesn't work either.

This is what I get:
Quote:
SQL>create table dbGuyo (col1 int);
SQL>insert into dbGuyo values (1);
SQL>select 'odd' from dbGuyo having mod(count(*),2) = 1;

===
odd

1 row found

SQL>insert into dbGuyo values (2);
SQL>select 'odd' from dbGuyo having mod(count(*),2) = 1;

0 rows found
Isn't that what you want?
Reply With Quote
  #8 (permalink)  
Old 08-17-11, 06:07
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
are you guys sure that MOD() is a valid ANSI function?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 08-17-11, 06:41
JarlH JarlH is offline
Registered User
 
Join Date: Dec 2008
Location: At work...
Posts: 68
Quote:
Originally Posted by r937 View Post
are you guys sure that MOD() is a valid ANSI function?
SQL-2008, Feature T441: ABS and MOD functions

<modulus expression> ::=
MOD <left paren> <numeric value expression dividend> <comma>
<numeric value expression divisor><right paren>
Reply With Quote
  #10 (permalink)  
Old 08-17-11, 06:55
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
thanks jarl... just checking
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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