Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Cross Join?

Author  Topic 

akashenk
Posting Yak Master

111 Posts

Posted - 2007-05-18 : 16:14:51
I have two tables that have the same number of fields, but the field data types do not necessarily match up..

Table 1 is called "IDs" and it has a single field...Item_ID varchar(15)

Table2 is called "Submissions" and has a single field... Submission_Count smallint)

These tables have a one to one relationship, in that they will have the same number of records and each record in the IDs table corresponds to a record in the Submissions table.

Furthermore, the order of the records matches up, so the 1st Id record, matches up with the 1st Submission record..so on and so forth, however, these tables aren't designed as relational database tables, in that the Submissions table obviously doesn't have a foreign key field that maps to the Ids table. The reason for this is that these tables are actually temp tables I create as part of a stored procedure. What I'm trying to accomplish is get a single temp table that has the combined records of the two tables. So, for the following sample data

IDs: Id1, Id2, Id3
Submissions: 6, 27, 32

I would like to create a single temp table that has the two fields in it and 3 records

So, the data would look like:

Id1 6
Id2 27
Id3 32

I attempted to do a cross join with the Ids and Submissios tables, but this resulted in the following cartesion product table, which is obviously not what I want.

Id1 6
Id1 27
Id1 32
Id2 6
Id2 27
Id2 32
Id3 6
Id3 27
Id3 32

Can anyone lend me a hand in figuring out how to combine these two tables so that the result includes all the information and has the same number of records as the source tables? Thanks!

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-05-18 : 16:23:28
when you create your temp tables, just also add an identity column to each. then just join your two temp tables on that identity column to get the results you need.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

akashenk
Posting Yak Master

111 Posts

Posted - 2007-05-18 : 17:20:32
Ahhh. thanks.
Go to Top of Page
   

- Advertisement -