Portal | Manuals | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Profile Log in to check your private messages Log in
 
Join query

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

Moderator


Joined: 14 Oct 2005
Posts: 1187
Location: Bangalore,India

PostPosted: Wed Aug 26, 2009 12:19 pm    Post subject: Join query
Reply with quote

Hi All,

I have two tables

Table 1

Code:
Acc_Num   Acct_cde    Amount

1            A          10

1            B          11

3            A          10

3            B          11


Table 2:

Code:
Acc_Num   Acct_cde    Amount

1            C         10

2            A         12

2            A         10

2            C         13

3            B         14


I need a report to get the Acc_num, Acc_code & sum of there amount:

Code:
Acct_num     Acct_cde_A_AMt Acct_cde_B_Amt Acct_cde_C_Amt

1              10           11               10

2              22           -                13           

3              10           25               -


Please advise me the sql for same
Back to top
View user's profile Send private message

Srihari Gonugunta

Active User


Joined: 14 Sep 2007
Posts: 295
Location: Singapore

PostPosted: Wed Aug 26, 2009 12:43 pm    Post subject:
Reply with quote

Hi Ekta,
You can try the following query.

Code:
Select C.Acct_num, C.Acct_cde, sum(C.Amount)
From       
(Select A.Acct_num, A.Acct_cde, A.Amount
From T1 A
Union
Select B.Acct_num, B.Acct_cde, B.Amount
From T2 B) C
Group by C.Acct_num, C.Acct_cde
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Thu Aug 27, 2009 12:47 pm    Post subject:
Reply with quote

You should use UNION ALL. Otherwise there could be a small problem when T1 and T2 have an identical row.

IF the account codes are known (and i suppose so, because you have 3 columns)
AND you need the SQL to deliver the report in that layout (otherwise Srihari's solution is better )

Code:
Select
 c.acct_num
,sum(case when c.acct_cde = 'A' then  c.amount end)
,sum(case when c.acct_cde = 'B' then  c.amount end)
,sum(case when c.acct_cde = 'C' then  c.amount end)
from
 (Select A.Acct_num, A.Acct_cde, A.Amount
    From T1 A
  Union all
  Select B.Acct_num, B.Acct_cde, B.Amount
  From T2 B ) C
Group by C.Acct_num
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
No new posts HEX value search in a DB2 query maxsubrat DB2 2 Wed Oct 04, 2017 3:04 pm
No new posts Create procedure issues -628 when add... chandraBE DB2 1 Mon Sep 18, 2017 12:16 pm
No new posts Join records from 2 files with No Dup... Poha Eater DFSORT/ICETOOL 22 Sun Aug 27, 2017 10:35 pm
No new posts Can we limit length in concatenation ... balaji81_k DB2 7 Tue Aug 22, 2017 2:50 am
No new posts Reduce CPU Times for Join Sort santoshn SYNCSORT 12 Sat Jun 10, 2017 1:40 pm

Facebook
Back to Top
 
Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us