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 |
daidaluus
Yak Posting Veteran
73 Posts |
Posted - 2012-05-11 : 11:03:25
|
i have 2 tables with the following data:id1 value1--- ------1 10002 10003 10004 20005 2000andid2 value2--- -----101 1000102 1000103 2000104 2000105 2000now i need a query to return this result set:id1 value1 id2 value2--- ----- --- ------1 1000 101 1000 2 1000 102 10003 1000 NULL NULL4 2000 103 20005 2000 104 2000NULL NULL 105 2000 |
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2012-05-11 : 11:05:48
|
Are you really using SQL Server 2000? The answer is much simpler if you are on 2005 or newer.- Jeffhttp://weblogs.sqlteam.com/JeffS |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-11 : 15:55:10
|
[code]SELECT id1, value1, id2, value2FROM(SELECT *,COALESCE((SELECT COUNT(*) FROM Table1 WHERE value1= t.value1 AND id1<t.id1),0) +1 AS RnFROM table1 t)t1FULL OUTER JOIN(SELECT *,COALESCE((SELECT COUNT(*) FROM Table2 WHERE value2= t.value2 AND id2<t.id2),0) +1 AS RnFROM table2 t)t2ON t2.Rn = t2.RnAND t2.value2 = t1.value1[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|
|
|