MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Retrieving the count with COUNT(*) and GROUP BY

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
srini_igsi
Beginner


Joined: 12 May 2006
Posts: 41
Topics: 16
Location: Mumbai

PostPosted: Thu Mar 19, 2009 2:39 pm    Post subject: Retrieving the count with COUNT(*) and GROUP BY Reply with quote

Could some body please let me know if there is a way to get the consolidated count for all the groups together instead of one per group.

Code:

SELECT COUNT(*)                                   
FROM DB2T.SOSRSTK_REL   C                 
  INNER JOIN DB2T.SOSRSTK_REL  E         
ON  (  C.CORP          = '05G'                     
AND    C.RELEASE_DATE <= '2009-03-06'             
AND    C.RELEASE_ID    = E.RELEASE_ID )           
  INNER JOIN DB2T.SOSRSTK_REL S         
ON    S.RELEASE_ID    =  C.RELEASE_ID             
AND   S.REL_TYPE      IN  ('1', '2', '3', '4')     
AND   S.REL_STATUS    IN  ('C', 'O', 'P', 'H', 'R')
GROUP BY CORP                                     
        ,C.RELEASE_DATE                           
        ,C.RELEASE_ID                             
        ,C.RT_IND                                 
        ,C.PROD_CODE                               
        ,SUM_TOT_SHRS

_________________
Thanks,
-Srini
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12380
Topics: 75
Location: San Jose

PostPosted: Thu Mar 19, 2009 3:11 pm    Post subject: Reply with quote

srini_igsi,

Just remove the GROUP BY statement
Back to top
View user's profile Send private message Send e-mail Visit poster's website
srini_igsi
Beginner


Joined: 12 May 2006
Posts: 41
Topics: 16
Location: Mumbai

PostPosted: Fri Mar 20, 2009 7:45 am    Post subject: Reply with quote

Sorry Kolusu, I need the count of all the groups together after applying GROUP BY.
_________________
Thanks,
-Srini
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12380
Topics: 75
Location: San Jose

PostPosted: Fri Mar 20, 2009 12:57 pm    Post subject: Reply with quote

srini_igsi,

You need to understand how the the group by works

consider this is your table
Code:

COL1
====

A
A
B
B
C


Code:

SELECT COUNT(*)
  FROM TABLE
 GROUP BY COL1


Will produce
Code:

count 
=====
3
2
1         


Now the total sum of the counts is 6. remove the Group by clause and see the

Code:

SELECT Count(*)
  FROM TABLE


will produce

Code:

count 
=====
6


Do you want to produce 3 or as there are 3 different unique values or do you want the total of all them? If you want 3 then you can use this sql (untested)


Code:

SELECT COUNT(Z.GRPCOUNT)
  FROM (SELECT COUNT(*) AS GRPCOUNT
          FROM DB2T.SOSRSTK_REL   C                 
         INNER JOIN DB2T.SOSRSTK_REL  E         
            ON (C.CORP          = '05G'                     
           AND  C.RELEASE_DATE <= '2009-03-06'             
           AND  C.RELEASE_ID    = E.RELEASE_ID )           
         INNER JOIN DB2T.SOSRSTK_REL S         
            ON S.RELEASE_ID    =  C.RELEASE_ID             
           AND S.REL_TYPE      IN  ('1', '2', '3', '4')     
           AND S.REL_STATUS    IN  ('C', 'O', 'P', 'H', 'R')
         GROUP BY CORP                                     
                 ,C.RELEASE_DATE                           
                 ,C.RELEASE_ID                             
                 ,C.RT_IND                                 
                 ,C.PROD_CODE                               
                 ,SUM_TOT_SHRS) Z
;
Back to top
View user's profile Send private message Send e-mail Visit poster's website
srini_igsi
Beginner


Joined: 12 May 2006
Posts: 41
Topics: 16
Location: Mumbai

PostPosted: Sat Mar 21, 2009 7:31 am    Post subject: Reply with quote

Thank you so much Kolusu. I know how GROUP BY works but I was looking out for the tot of three of them only. I will run this query and let you know the result on Monday.
_________________
Thanks,
-Srini
Back to top
View user's profile Send private message
srini_igsi
Beginner


Joined: 12 May 2006
Posts: 41
Topics: 16
Location: Mumbai

PostPosted: Mon Mar 23, 2009 11:12 am    Post subject: Reply with quote

Thank you so much Kolusu. It's perfectly working. This is the reason why I always trust MVSFORUMS website.

It has made my program logic very simple.
_________________
Thanks,
-Srini
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group