Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Dynamic Join help please... tough query

Dynamic Join help please... tough query

Dynamic Join help please... tough query

Hi guys,

I am struggling with query below and hope you guys can help me with this.
I have 2 tables that I need to join which are Sales table and Customer_code_history table as per below.


Sales_Code	Customer_code
ABC	         X-A01
DEF	         X-B01
GHI	         X-C01

Current_Cust_Code	Prev_cust_Code	start_date	end_date
X-A03	                   X-A02	31/01/2016	31/12/9999
X-A02	                   X-A01	20/01/2016	30/01/2016
X-B01	                   NULL	        3/01/2016	31/12/9999
X-C04	                   X-C03	6/03/2016	31/12/9999
X-C03	                   X-C02	31/01/2016	5/03/2016
X-C02	                   X-C01	20/01/2016	30/01/2016 

I am trying to get the new customer code from joining the sales table and customer code history table.
The problem is the customer code from the sales table can exist in prev_cust_code or Current_cust_code in customer code history table

The pseudo code is

if the customer code in sales table exist in current cust code column
then take the current cust code and remove the first 2 characters ("X-")
else if the customer code in sales table not exist in current cust code column but exist in prev code column
then we need to track the current cust code by finding the latest date which is 31/12/9999
and remove the first 2 characters ("X-")

The expected result would be as per below


Sales_Code	Customer_code	New_Customer_Code
ABC	            X-A02	    A03
DEF	            X-B01	    B01
GHI	            X-C01	    C04 

If you could help me with teradata query that would be much appreciated.


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close