tags:

views:

511

answers:

1

I need to merge two SELECT statements.

There are two tables in my database, where table A consists of two fields; Type and Counter. Table B consists of two fields; Source and destination.

Table A has three items; 
                   { 1, 5000 }, { 2, 10000 } and { 3, 15000 }. ({Type, Counter})
Table B has two items; 
                   { 5000, 10000 } and { 10000, 150000 }. ({Source, Destination})

That is, table B's values consist of data from table A.

The data I have available for my query are Type in table A (1, 2 and 3).

The end result of a query (the one I'm after) to the database should be one item; { 5000, 10000, 15000 }, where columns should read {Source, Middle, Destination}, respectively. However, I've been unable to create a query that can appear in such format.

I am able to get the data using an INNER JOIN, where the result appear as two items; { 5000, 10000 } and { 10000, 15000 }. (Not simply the content of table B that is, if the tables would consist of other items, as well.)

(There's obviously far more items in both tables than I've displayed above.)

So, how can I write the SQL query to make my result appear as { 5000, 10000, 15000 } (with appropriate column names)?

+2  A: 

From your question, you don't have any use or need for table A - you are simply joining Table B to itself on Source = Destination.

Your query should be

SELECT a.Source, a.Destination, b.Destination
FROM [Table B] as a INNER JOIN [Table B] as b
ON a.Destination = b.Source
Kirk Broadhurst
That seemed to have done it. Thanks. I guess I was staring blind that I needed to have any input, but apparantly not.
Fredrik Ullner
I posted a correct answer, why did you vote it down? I posted it before I saw yours, and I like mine better.
Kirk Broadhurst