Hi everyone
Before I forget - we are on V7r1
Here is my situation
When an order is placed, a customer record is created from the name and address of the person placing that order.
This creates a unique customer number - unless its recognized that this particular customer has already placed an order (How that recognition works is not part of this situation)
However - the person placing the order COULD have already placed an order in the past - but the name and/or address slightly different
Todays order was placed by John Smith of a certain address
He could have placed a past order as J Smith
Or even Johnathan Smith
In these instances, these 3 customer records (and their statistics) need to be merged into one
That is happening, and we keep an audit trail of what old customer number was merged into what "NEW" customer number
This data is in a file with 2 fields
Old customer number
New customer number
With the customer record with the OLD customer number being deleted and the customer record with the New customer number being updated from the OLD customer number
Here is my situation
There are records where the NEW customer number has been merged into another customer number and then THAT new custome number is then merged into yet another one
Here is an example
Old Cust # New Cust #
1 2
2 3
3 6
7 10
6 24
27 30
94 110
110 225
My aim is to pare this down to the following
Old Cust # New Cust #
1 24
7 10
27 30
94 225
I created queries to take the new customer number as the old customer number and see if that had been merged into another different new customer number
I then took the results of that and repeated the process
I then took the results of that and repeated the process
I then took the results of that and repeated the process
Etc.
It took 5 iterations, but the problem is - what happens when and if I need a 6th, and then a 7th
That's when I remembered reading about recursive sql
I found the following web sites
https://www.ibm.com/developerworks/ibmi/library/i-db2connectby/index.html
http://downloads.teradata.com/forum/database/help-with-recursive-query
From the last web site I created the following SQL - the problem was that it was running for OVER 8 hours (281,000 records on the file cstmerge)
I don't know if there is anything wrong with the below SQl
I am hoping someone can see something that is wrong or can explain whats missing or needs changing
WITH RECURSIVE cte
(CMOLDCST#, CMNEWCST#, CMLSTCST#)
AS
(
SELECT CMOLDCST#, CMNEWCST#, CMNEWCST# as CMLSTCST#
FROM prodfa.cstmerge
WHERE CMOLDCST# <> CMNEWCST#
UNION ALL
SELECT t.CMOLDCST#, t.CMNEWCST#, cte.CMLSTCST#
FROM cte
INNER JOIN prodfa.cstmerge AS t
ON cte.CMOLDCST# = t.CMNEWCST#
WHERE t.CMOLDCST# <> t.CMNEWCST#
)
SELECT * FROM cte
ORDER BY 3,1
Alan Shore
E-mail : ASHORE@xxxxxxxx
Phone [O] : (631) 200-5019
Phone [C] : (631) 880-8640
'If you're going through hell, keep going.'
Winston Churchill
As an Amazon Associate we earn from qualifying purchases.