Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

Require help in DB2 Query

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
View previous topic :: :: View next topic  
Author Message
Shruti Takkar

New User


Joined: 09 Apr 2015
Posts: 5
Location: india

PostPosted: Wed Aug 03, 2016 10:57 pm    Post subject: Require help in DB2 Query
Reply with quote

Hi ,

I have to execute below problem in DB2 query, please suggest some valuable options.

Consider Table A having account details.
I need to count accounts from Table A for multiple scenarios.

I can count accounts using multiple case when conditions like below.
===========================================
select Count(case when t1.a = 1 then 1 else 0 end) as A_COUNT
, Count(case when t1.b = 2 then 1 else 0 end) as B_COUNT
from t1
============================================

My problem here is,, if my account is counted in A_COUNT, it should not get double counted in B_COUNT.

There are multiple when conditions and it is not possible to tweak them to make all conditions mutually exclusive.

Regards,
Shruti
Back to top
View user's profile Send private message

Rohit Umarjikar

Senior Member


Joined: 21 Sep 2010
Posts: 1610
Location: NY,USA

PostPosted: Wed Aug 03, 2016 11:27 pm    Post subject:
Reply with quote

you will have to explain better. tell us the input table and sample values and expected output and then it goes easy for anyone to provide you a solution. why do you not do group by on account number and those t1.a,t1.b and so on?
Back to top
View user's profile Send private message
chandan.inst

Active User


Joined: 03 Nov 2005
Posts: 269
Location: Mumbai

PostPosted: Tue Aug 09, 2016 11:20 am    Post subject: Reply to: Require help in DB2 Query
Reply with quote

is this something you are expecting?

Code:
select
Count(case when (t1.a = 1 and t1.b<> 2) then 1 else 0 end) as A_COUNT
, Count(case when (t1.b = 2 and t1.a<>1) then 1 else 0 end) as B_COUNT
from t1


Thanks,
Chandan
Back to top
View user's profile Send private message
Rohit Umarjikar

Senior Member


Joined: 21 Sep 2010
Posts: 1610
Location: NY,USA

PostPosted: Wed Aug 10, 2016 12:06 am    Post subject:
Reply with quote

Code:
Select innr.type,sum(innr.cnt )
from
(select
    case  when t1.a = 1 then 1 else 0
            when t1.b = 2  then 1 else 0 end  as cnt
    ,case when t1.a = 1 then 'A'
            when t1.b = 2  then 'B' end as type
from table temp; ) as innr
group by innr.type
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
This topic is locked: you cannot edit posts or make replies. Db2 SQL query to convert rows into co... subratarec DB2 5 Thu Nov 17, 2016 4:51 pm
No new posts Two Selects in a single select query Rohit Umarjikar DB2 1 Fri Nov 04, 2016 8:46 pm
No new posts Problem in Running Query via JCL vickey_dw DB2 3 Tue Oct 18, 2016 11:11 pm
No new posts SQL Query optimization. arunsoods DB2 12 Sun Sep 04, 2016 6:27 am
No new posts Query on BLSR Nileshkul JCL & VSAM 2 Sat Aug 13, 2016 5:18 am


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us