473,394 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

UNION Problem on DB2 Ver 6

Hi all,
I have a two tables with same structure in DB2 Ver 6 on OS/390
1- CurrPay(CustomerId, BnkId, BranchCode, PayDate, Amount)
for Current Period Customers Payments
2- PayHist(CustomerId, BnkId, BranchCode, PayDate, Amount)
for customer payments history

I need to find sum/count of All Payments for each bankId & PayDate &
BranchCode with one query.
I write query for each table like this:

A:
SELECT BnkId, BranchCode, PayDate, sum(Amount) as Amnt, count(*) as
Cnt
FROM CurrPay
GROUP BY BnkId, BranchCode, PayDate

B:
SELECT BnkId, BranchCode, PayDate, sum(Amount) as Amnt, count(*) as
Cnt
FROM PayHist
GROUP BY BnkId, BranchCode, PayDate

Each of these queries work correctly but i need grouping on UNION ALL
of these two table
because we have records with same bankId & PayDate & BranchCode in two
tables.

SELECT BnkId, BranchCode, PayDate, sum(Amount) as Amnt, count(*) as
Cnt
FROM
(
SELECT BnkId, BranchCode, PayDate, Amount
FROM CurrPay
UNION ALL
SELECT BnkId, BranchCode, PayDate, Amount
FROM PayHist
)
GROUP BY BnkId, BranchCode, PayDate

But DB2 Ver 6 not support this query.
please help me. thanks.

Feb 3 '07 #1
1 2679
DB2 V6 for OS/390 supports a full outer join which is what you want.
This will give you all of the rows from both tables without requiring
matching rows.

Try:
SELECT COALESCE(a.BnkId,b.BnkId) AS Bank_Id
,COALESCE(a.BranchCode,b.BranchCode) AS Branch_Code
,COALESCE(a.PayDate,b.PayDate) AS Pay_Date
,SUM(COALESCE(a.Amount,0)+COALESCE(b.Amount,0)) AS Amnt
,count(*) AS Cnt
FROM CurrPay a
FULL OUTER JOIN PayHist b
ON a.BnkId = b.BnkId
AND a.BranchCode = b.BranchCode
AND a.PayDate = b.PayDate

GROUP BY COALESCE(a.BnkId,b.BnkId)
,COALESCE(a.BranchCode,b.BranchCode)
,COALESCE(a.PayDate,b.PayDate)

You may not need the COALESCE functions in the SUM but I don't have a
S/390 system available to verify this.

Phil Sherman
Arayeshi wrote:
Hi all,
I have a two tables with same structure in DB2 Ver 6 on OS/390
1- CurrPay(CustomerId, BnkId, BranchCode, PayDate, Amount)
for Current Period Customers Payments
2- PayHist(CustomerId, BnkId, BranchCode, PayDate, Amount)
for customer payments history

I need to find sum/count of All Payments for each bankId & PayDate &
BranchCode with one query.
I write query for each table like this:

A:
SELECT BnkId, BranchCode, PayDate, sum(Amount) as Amnt, count(*) as
Cnt
FROM CurrPay
GROUP BY BnkId, BranchCode, PayDate

B:
SELECT BnkId, BranchCode, PayDate, sum(Amount) as Amnt, count(*) as
Cnt
FROM PayHist
GROUP BY BnkId, BranchCode, PayDate

Each of these queries work correctly but i need grouping on UNION ALL
of these two table
because we have records with same bankId & PayDate & BranchCode in two
tables.

SELECT BnkId, BranchCode, PayDate, sum(Amount) as Amnt, count(*) as
Cnt
FROM
(
SELECT BnkId, BranchCode, PayDate, Amount
FROM CurrPay
UNION ALL
SELECT BnkId, BranchCode, PayDate, Amount
FROM PayHist
)
GROUP BY BnkId, BranchCode, PayDate

But DB2 Ver 6 not support this query.
please help me. thanks.
Feb 4 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Simon Elliott | last post by:
I'd like to do something along these lines: struct foo { int i1_; int i2_; }; struct bar {
6
by: Neil Zanella | last post by:
Hello, I would like to know what the C standards (and in particular the C99 standard) have to say about union initializers with regards to the following code snippet (which compiles fine under...
2
by: Barry Schwarz | last post by:
Given a union of the form union { T1 m1; T2 m2;}obj; where T1 and T2 are different scalar (non-aggregate) types. The C99 standard states that obj.m1 = value; if (obj.m2 ... invokes...
10
by: Denis Pithon | last post by:
Hi, C lovers! I stuck on an union problem Here is snippet of my code .... /* two pointers of function with repsectively one and two argues */ typedef int (*dce_sn_f)(dce_t*);
2
by: Peter Dunker | last post by:
Hi, I will write ANSI C89. Is the following struct defenition correct ? I wrote it with VC6(Windows IDE) and at first no Problem. As I changed a compiler switch to 'no language extension', the...
73
by: Sean Dolan | last post by:
typedef struct ntt { int type; union { int i; char* s; }; }nt; nt n; n.i = 0;
18
by: ranjeet.gupta | last post by:
Dear ALL As we know that when we declare the union then we have the size of the union which is the size of the highest data type as in the below case the size should be 4 (For my case and...
30
by: Yevgen Muntyan | last post by:
Hey, Why is it legal to do union U {unsigned char u; int a;}; union U u; u.a = 1; u.u; I tried to find it in the standard, but I only found that
5
by: wugon.net | last post by:
question: db2 LUW V8 UNION ALL with table function month() have bad query performance Env: db2 LUW V8 + FP14 Problem : We have history data from 2005/01/01 ~ 2007/05/xx in single big...
3
by: SRK | last post by:
Hi, I wanted to use an anonymous union within an structure something like below - struct Test { union { std::string user; //char user; std::string role; //char role;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.