7

I have a two tables,Transaction and Master. The transaction table shows a from and to activity id. The master shows the activity id and a name. The from and to id use the same master table to associate the activity id and name. What I would like to do is take the distinct from and to values and display them with the associated name.

The original query was

select distinct a.from, a.to from Transaction a

What I need is something where a.from is followed by b.name and a.to is followed by b.name

I know that I have to have a join but I need the join to apply to each of the distinct a.values.

In concept I would like to do 2 joins with one on each of the a.values but I am not sure how to delineate the from and to values.

2
  • 1
    Join the Master table twice. For example: stackoverflow.com/a/4267979/12601 Commented Aug 5, 2013 at 19:51
  • Can you add a simple example with, say, 3 columns from each table and an example of the output you want from the query? Commented Aug 5, 2013 at 19:52

1 Answer 1

9
select distinct a.from, f.name as FromName, a.to, t.name as ToName 
from Transaction a
join Master F on a.from = f.id
join Master T on a.to = f.id
Sign up to request clarification or add additional context in comments.

1 Comment

This is what Greg suggested also. By giving the same table two aliases I am able to join the two fields independently of each other. Thanks to everyone who helped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.