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 

Count Of COlumns With Conditions

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


Joined: 04 Dec 2002
Posts: 64
Topics: 21

PostPosted: Thu Jul 10, 2003 9:46 am    Post subject: Count Of COlumns With Conditions Reply with quote

I have three columns A, B, C. A is the key. I would like to get a count of the Key A whenever it changes alongwith all counts of B and Counts of C Whenever it is blank associated with the Key. I would like to get it in a single row. Is it possible?

Thanks
Back to top
View user's profile Send private message Send e-mail
Glenn
Beginner


Joined: 23 Mar 2003
Posts: 56
Topics: 3

PostPosted: Thu Jul 10, 2003 2:07 pm    Post subject: Reply with quote

Please describe in detail what you are wanting.
Back to top
View user's profile Send private message
moyeenkhan
Beginner


Joined: 04 Dec 2002
Posts: 64
Topics: 21

PostPosted: Thu Jul 10, 2003 2:50 pm    Post subject: Reply with quote

An example should clarify

Col A COl B Col C\
A1 B1 C1
A1 B1 --
A1 B2 C3
A1 B4 --
A2 ......................

What I need to get is
A1 4 2
in a single row.

4 = Count Of Col B
2 = Count Of non-blank Col C
Back to top
View user's profile Send private message Send e-mail
Himesh
CICS Forum Moderator
CICS Forum Moderator


Joined: 20 Dec 2002
Posts: 80
Topics: 21
Location: Chicago

PostPosted: Thu Jul 10, 2003 3:11 pm    Post subject: Reply with quote

Moyeenkhan,

The following SQL should give you the result.

Code:


SELECT
   A.columnA,
   B.ACountB,
   C.ACountC
FROM
   tableA   A,
   (SELECT
      columnA,
      COUNT(*)   ACountB
   FROM
      tableA
   group by
      columnA
   )      B,
   (SELECT
      columnA,
      count(*)   ACountC
   FROM   
      tableA
   WHERE
      columnC IS NOT NULL
   group by
      columnA
   )      C
WHERE
   A.columnA = B.columnA
AND   B.columnA = C.columnA



Please Note:
ACountB will give the total count of columnA, i.e. occurance of ColumnA
ACountC will give the total count of columnA where columnC is not NULL

Though, in your first post you wanted the count of column A with blank column C, and in your later post you wanted the count of column A with non-blank Column C.
So please change the SQL from "IS NOT NULL" to "IS NULL" if required.

regards,
Himesh
Back to top
View user's profile Send private message Yahoo Messenger
moyeenkhan
Beginner


Joined: 04 Dec 2002
Posts: 64
Topics: 21

PostPosted: Thu Jul 10, 2003 5:59 pm    Post subject: Reply with quote

I found a solution.
SELECT
A
, COUNT(*)
, SUM(CASE WHEN C IS NULL THEN 0 ELSE 1 END)
FROM
TABLE
GROUP
BY A

Many thanks Himesh
Back to top
View user's profile Send private message Send e-mail
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