Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Join Multiple Versioned Tables

user621430Aug 25 2014 — edited Aug 26 2014

Hi,

We are working on a data warehousing project and am wondering how to join multiple tables each of which are versioned independently(SCD type 2 with a valid from and valid to date).

For eg, we get our client information (client id , name etc) from one source and the client rating information from another source.  Since the sources are different,we have separate tables for them and each of them gets versioned independently.

Here is my Client table ( with its own valid from and valid to columns).

IDClient NameValid FromValid To
1CitiBank01-JAN-1401-JAN-15
1CitiBank New02-JAN-1501-FEB-15
1CitiBank Newer02-FEB-1501-APR-15

And similarly the Client Rating has the ID and the rating information.

IDRatingValid FromValid To
1Platinum01-JAN-1401-FEB-14
1Premium01-FEB-1401-MAR-15

I would like to merge the above two tables, and present the information in a single view.  I am having some difficulty in determing the valid from and valid to columns.

IDClient NameRatingValid From(Calculated)Valid To (Calculated)
1CitiBankPlatinum01-JAN-1401-FEB-14
1CitiBankPremium01-FEB-1401-JAN-15
1CitiBank NewPremium02-JAN-1501-FEB-15
1CitiBank NewerPremium02-FEB-1501-MAR-15

And this is the query I used to get the above output:

SELECT client.id,

       client.name,

       crm.tier,

       Greatest(client.vld_fm, crm.vld_fm),

       Least(client.vld_to, crm.vld_to)

FROM   client client,

       client_rating crm

WHERE  client.id = crm.id

       AND ( client.vld_fm <= crm.vld_fm

             AND client.vld_fm <= crm.vld_to

             AND client.vld_fm >= crm.vld_fm

             AND client.vld_fm >= crm.vld_to )

        OR ( client.vld_fm BETWEEN crm.vld_fm AND crm.vld_to )

        OR ( client.vld_to BETWEEN crm.vld_fm AND crm.vld_to );



The problem really is we have multiple sources of data (and each with its own versioning) and then the joins become very very complex.  Is there a better way of writing the query ?

Or perhaps a better way of designing our tables?

Thanks for your help.

Anand

This post has been answered by Marwim on Aug 25 2014
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 23 2014
Added on Aug 25 2014
4 comments
1,252 views