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 > DB2 Row Counts

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-14-08, 10:01
Natasha82 Natasha82 is offline
Registered User
 
Join Date: Apr 2008
Posts: 8
DB2 Row Counts

Hello,

I have a DB2 (Mainframe) table called 'GRPTBL' having rows/columns as below;

GRP SUBGRP

CCC C02
AAA A01
BBB B02
BBB B01
AAA A03
CCC C01
BBB B02
CCC C02
AAA A03
AAA A02

My query to find out the number of different 'GRP's looks like

SELECT count(*)
into wk-grp-cnt
FROM grpTbl
WHERE grp in (
select grp
FROM testTbl
GROUP BY GRP)

returns a value of 10 to the wk-grp-cnt instead of 3.

I do appreciate any help to adjust my query to get the expected results.

Thanks in advance.

Natasha.
Reply With Quote
  #2 (permalink)  
Old 04-14-08, 10:23
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
In the outer select, you select all rows from table GRPTBL that have a value in the GRP column, which can also be found in TESTTBL. You have a GROUP BY in the subselect on TESTTBL, and that GROUP BY is useless because you don't have to group anything there. Since all rows qualify in the outer select, you will get a count of how many rows are in the table.

What you probably want to have is:
Code:
SELECT COUNT(*)
INTO   :wk-grp-cnt
FROM   ( SELECT DISTINCT grp
         FROM grptbl )
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #3 (permalink)  
Old 04-14-08, 10:49
Natasha82 Natasha82 is offline
Registered User
 
Join Date: Apr 2008
Posts: 8
Correction

Thanks for your response...

There is a small correction to my initial query;

SELECT count(*)
into wk-grp-cnt
FROM grpTbl
WHERE grp in (
select grp
FROM grpTbl
GROUP BY GRP)

I tried your solution, but getting some syntax error;

Could you please take a look at it... Thanks a lot.
Reply With Quote
  #4 (permalink)  
Old 04-14-08, 11:11
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Would that work, I wonder:
Code:
select count(distinct grp) from GrpTbl
Reply With Quote
  #5 (permalink)  
Old 04-14-08, 11:13
aflorin27 aflorin27 is offline
Registered User
 
Join Date: Apr 2008
Location: Iasi, Romania
Posts: 317
I think you should try something like this:

SELECT count(*)
into wk-grp-cnt
FROM grpTbl
WHERE grp in (
select distinct grp
FROM grpTbl)
GROUP BY grp
Reply With Quote
  #6 (permalink)  
Old 04-14-08, 11:19
Natasha82 Natasha82 is offline
Registered User
 
Join Date: Apr 2008
Posts: 8
Syntax errors...

This is not working... anyway thanks for the reply.

Would that work, I wonder:

select count(distinct grp) from GrpTbl
Reply With Quote
  #7 (permalink)  
Old 04-14-08, 11:36
Natasha82 Natasha82 is offline
Registered User
 
Join Date: Apr 2008
Posts: 8
Getting SqlCode of -811...

Tried your updated query.. Unfortunately getting -811 because the subquery is returning more than 1 row ...(3 rows in this case).

Any other solution... ? Thank you ..- N.


Quote:
Originally Posted by aflorin27
I think you should try something like this:

SELECT count(*)
into wk-grp-cnt
FROM grpTbl
WHERE grp in (
select distinct grp
FROM grpTbl)
GROUP BY grp
Reply With Quote
  #8 (permalink)  
Old 04-14-08, 11:42
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
This will not produce the correct results
Code:
SELECT COUNT(*) FROM ... WHERE grp IN ( ... ) GROUP BY ...
because the count will be computed for each group and you don't get a count of groups.

My query had the problem that it didn't give the temp table a name. Adding a "AS t" at the end will do the trick.

And n_i's approach is the easiest and most straight-forward.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #9 (permalink)  
Old 04-14-08, 11:43
aflorin27 aflorin27 is offline
Registered User
 
Join Date: Apr 2008
Location: Iasi, Romania
Posts: 317
Now I saw that you updated the initial query.
In this case, you should try:

SELECT count(distinct grp)
into wk-grp-cnt
FROM grpTbl
Reply With Quote
  #10 (permalink)  
Old 04-14-08, 12:05
Natasha82 Natasha82 is offline
Registered User
 
Join Date: Apr 2008
Posts: 8
n_i's solution is working !

n_i's solution is giving the correct results on mainframe.. Initially I tried it
using Ms-Access and was getting some syntax errors...

Anyways... Thanks to all of you guys.. I really appreciate your response and support... ! - N.


Quote:
Originally Posted by stolze
This will not produce the correct results
Code:
SELECT COUNT(*) FROM ... WHERE grp IN ( ... ) GROUP BY ...
because the count will be computed for each group and you don't get a count of groups.

My query had the problem that it didn't give the temp table a name. Adding a "AS t" at the end will do the trick.

And n_i's approach is the easiest and most straight-forward.
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