SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 JOIN
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

daidaluus
Yak Posting Veteran

69 Posts

Posted - 05/11/2012 :  11:03:25  Show Profile  Reply with Quote
i have 2 tables with the following data:

id1 value1
--- ------
1 1000
2 1000
3 1000
4 2000
5 2000

and

id2 value2
--- -----
101 1000
102 1000
103 2000
104 2000
105 2000

now i need a query to return this result set:

id1 value1 id2 value2
--- ----- --- ------
1 1000 101 1000
2 1000 102 1000
3 1000 NULL NULL
4 2000 103 2000
5 2000 104 2000
NULL NULL 105 2000

jsmith8858
Dr. Cross Join

USA
7423 Posts

Posted - 05/11/2012 :  11:05:48  Show Profile  Visit jsmith8858's Homepage  Reply with Quote
Are you really using SQL Server 2000? The answer is much simpler if you are on 2005 or newer.

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

visakh16
Very Important crosS Applying yaK Herder

India
47157 Posts

Posted - 05/11/2012 :  15:55:10  Show Profile  Reply with Quote

SELECT id1, value1, id2, value2
FROM
(
SELECT *,COALESCE((SELECT COUNT(*) FROM Table1 WHERE value1= t.value1 AND id1<t.id1),0) +1 AS Rn
FROM table1 t
)t1
FULL OUTER JOIN
(
SELECT *,COALESCE((SELECT COUNT(*) FROM Table2 WHERE value2= t.value2 AND id2<t.id2),0) +1 AS Rn
FROM table2 t
)t2
ON t2.Rn = t2.Rn
AND t2.value2 = t1.value1



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000