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.
| Author |
Topic |
|
mvanwyk
Yak Posting Veteran
99 Posts |
Posted - 2007-01-10 : 15:58:13
|
| Hi there SQLTEAMI have a problem, and need your help.table1 has 1 single field,examplepkiTownIDDATA1234table2 has 1 single field, examplepkiTownIDDATA6789What SQL Query should i run to merge or join these 2 tables into 1The output that i would like it the followingDATA1 62 73 89 9Is this possible? |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-01-10 : 16:12:41
|
| SQL is a relational database -- how does table1 relate to table2 ? from the columns and data you have shown us, it doesn't, so what you are trying to do doesn't make a lot of logical sense unless you can explain a little more.- Jeff |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-10 : 16:15:56
|
| [code]-- prepare sample datadeclare @table1 table (pkiTownID int)insert @table1select 1 union allselect 2 union allselect 3 union allselect 4declare @table2 table (pkiTownID int)insert @table2select 6 union allselect 7 union allselect 8 union allselect 9-- Most versatile wayselect x.pkitownid, y.pkitownidfrom ( select t1.pkitownid, (select count(*) from @table1 as tt1 where tt1.pkitownid <= t1.pkitownid) seq from @table1 as t1 ) xinner join ( select t2.pkitownid, (select count(*) from @table2 as tt2 where tt2.pkitownid <= t2.pkitownid) seq from @table2 as t2 ) y on y.seq = x.seq-- most obvious wayselect t1.pkitownid, t2.pkitownidfrom @table1 t1inner join @table2 t2 on t2.pkitownid = t1.pkitownid + 5-- fastest wayselect pkitownid, pkitownid + 5 pkitownidfrom @table1[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
mvanwyk
Yak Posting Veteran
99 Posts |
Posted - 2007-01-10 : 16:18:02
|
| Thanks for the replyThis is my problem there is no physical relation, only that row 1 in table1 will allways link to table2 row 1 does this make any sense? |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-10 : 16:25:10
|
| Yes, I have already answered you!Read the answers you are given and run them and test them...Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-10 : 16:29:57
|
SQL Server 2005 wayselect x.pkitownid, y.pkitownidfrom ( select pkitownid, row_number() over (order by pkitownid) seq from @table1 ) xinner join ( select pkitownid, row_number() over (order by pkitownid) seq ) y on y.seq = x.seqorder by x.seq Peter LarssonHelsingborg, Sweden |
 |
|
|
mvanwyk
Yak Posting Veteran
99 Posts |
Posted - 2007-01-10 : 16:56:04
|
| Hi Peter Thanks alot, tried your sulotion and worked perfectly.Yet again thanks alot. |
 |
|
|
|
|
|
|
|