IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

DB2- COUNT function


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
geethi_nair

New User


Joined: 20 Nov 2007
Posts: 2
Location: US

PostPosted: Sat Mar 01, 2008 5:18 am
Reply with quote

Hi,

I have Table1 with 22 rows and Table2 with 14 rows. I need to find how many rows of Table1 satisfies the condition Table1.var1 = Table2.var1.

SELECT COUNT(A.VAR1)
FROM Table1 A
,Table2 B
WHERE A.var1 = B.var1

This query returned 308. Please help.

Thanks in advance,
Back to top
View user's profile Send private message
Craq Giegerich

Senior Member


Joined: 19 May 2007
Posts: 1512
Location: Virginia, USA

PostPosted: Sat Mar 01, 2008 6:10 am
Reply with quote

geethi_nair wrote:
Hi,

I have Table1 with 22 rows and Table2 with 14 rows. I need to find how many rows of Table1 satisfies the condition Table1.var1 = Table2.var1.

SELECT COUNT(A.VAR1)
FROM Table1 A
,Table2 B
WHERE A.var1 = B.var1

This query returned 308. Please help.

Thanks in advance,


What is the problem?
Back to top
View user's profile Send private message
Craq Giegerich

Senior Member


Joined: 19 May 2007
Posts: 1512
Location: Virginia, USA

PostPosted: Sat Mar 01, 2008 6:15 am
Reply with quote

If all the rows in table1 match all the row in TABLE2 (ie there is only 1 var1 in each table) then 308 is what you should get.
Back to top
View user's profile Send private message
geethi_nair

New User


Joined: 20 Nov 2007
Posts: 2
Location: US

PostPosted: Tue Mar 04, 2008 5:03 am
Reply with quote

Then how is it possible if I want to find the number of rows in Table1 that have a matching Var1 in Table2. (I tried all the joins. But don't work).

The following query did give correct value.

Select *
from Table1
where var1 in (select var1 from Table2)

But I didn't want to use a subquery here. This query seems to be a bit awkward for me because in another occasion I had to write the query as follows:

Select *
from Table1
where var1 in (select var1 from Table2)
and var2 in (select var2 from Table2)

Please help.
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Tue Mar 04, 2008 7:07 am
Reply with quote

Hello,

Please post all of the values of var1 in both tables. Do not show the unique values, but show every row in each table.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts To get the count of rows for every 1 ... DB2 3
No new posts To find whether record count are true... DFSORT/ICETOOL 6
No new posts Calling an Open C library function in... CICS 1
No new posts Validating record count of a file is ... DFSORT/ICETOOL 13
No new posts DATE2 function SYNCSORT 15
Search our Forums:

Back to Top