×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

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!

*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

Querying multiple fact tables

Querying multiple fact tables

Querying multiple fact tables

(OP)
I am looking to build a query involving multiple fact tables which share a common dimension key - can anyone shed some light on the accurate way to do this?

Eg Customer dimension table has customer_key
Orders fact table has a list of all orders, including the customer_key involved
Returns fact table has a list of all returns, including the customer_key involved.

A query such as
select *
from customer c,
     orders o,
     returns r
where c.customer_key = o.customer_key (+)
and   c.customer_key = r.customer_key (+)


appears to return me one row for each matching row in orders and returns, as opposed to all orders and all returns information for each customer, which is my aim.

Any pointers appreciated

RE: Querying multiple fact tables

What DB are you using (looks like Oracle?)

SQL97 statement that should do this would be...
select *
from customer c,
     left outer join orders o
          on (c.customer_key = o.customer_key )
     left outer join  returns r
          on (c.customer_key = r.customer_key )
where (r.customer_key is not null or o.customer_key is not null)
;

If most of your customers do not have orders and/or returns,
I would advise a multipass solution for performance (depending on your DB and system) to only have the keys that have both
like
insert into mykeys
select distinct customer_key from orders
  union select customer_key from returns
;

then use mykeys instead of customer and omit the where clause.

*NOTE: That a union may not perform and cause you to insert both sets of keys into 1 table (having duplicates) then a select distinct (or group by) into another table to be used for distinct keys for the left outer join (instead of customer)

Hope this helps...sorry if I stated the obvious to you.

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