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 2008 Forums
 Transact-SQL (2008)
 Improve union query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Humate
Posting Yak Master

101 Posts

Posted - 02/15/2013 :  12:24:40  Show Profile  Reply with Quote
Hi All,

I'd like to know if there is a better way of producing my result set rather than using the below union:

CREATE TABLE #t1
(PKT1 INT IDENTITY (10,1),
ID INT,
ID2 INT)
CREATE TABLE #t2
 
(PKT2 INT IDENTITY (20,1),
ID INT,
ID2 INT)
INSERT #t1
         ( ID, ID2 )
SELECT 18185, NULL UNION ALL
SELECT 18165, 39875 UNION ALL
SELECT NULL, 39875 UNION ALL
SELECT NULL, 39875 UNION ALL
SELECT 18189, NULL UNION ALL
SELECT NULL, 40001 UNION ALL
SELECT 18163, NULL UNION ALL
SELECT 18495, 40011
 
INSERT #t2
         ( ID, ID2 )
SELECT 18185, NULL UNION ALL
SELECT NULL, 39875 UNION ALL
SELECT 18189, 63751 UNION ALL
SELECT 18495, 40011
 
SELECT * FROM #t1
 
SELECT * FROM #t2
 
SELECT *
FROM #t1
INNER JOIN #t2 ON #t1.ID = #t2.ID
WHERE #t2.ID2 IS NULL
 
UNION ALL
 
SELECT *
FROM #t1
INNER JOIN #t2 ON #t1.ID2 = #t2.ID2
WHERE #t2.ID IS NULL
 
UNION ALL
 
SELECT *
FROM #t1
INNER JOIN #t2 ON #t1.ID = #t2.ID AND #t1.ID2 = #t2.ID2


I need to join ID1 and ID2 to t2 only when one of them exists in t2, then when both ID1 and ID2 exist in t2.

Thanks in advance

James K
Flowing Fount of Yak Knowledge

3323 Posts

Posted - 02/15/2013 :  13:19:33  Show Profile  Reply with Quote
This?
SELECT
	*
FROM
	#t1 t1
	INNER JOIN #t2 t2 ON 
		(t1.ID = t2.ID AND t2.ID2 IS NULL)
		OR 
		(t1.ID2 = t2.ID2 AND t2.ID IS NULL)
		OR
		(t1.ID = t2.ID AND t1.ID2  = t2.ID2);
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 02/15/2013 :  14:29:56  Show Profile  Reply with Quote

SELECT
	*
FROM
	#t1 t1
	INNER JOIN #t2 t2 ON COALESCE(#t1.ID,#t2.ID) = COALESCE(#t2.ID,#t1.ID) 
                          AND COALESCE(#t1.ID2,#t2.ID2) = COALESCE(#t2.ID2,#t1.ID2)


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

Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3323 Posts

Posted - 02/15/2013 :  16:03:22  Show Profile  Reply with Quote
quote:
Originally posted by visakh16


SELECT
	*
FROM
	#t1 t1
	INNER JOIN #t2 t2 ON COALESCE(#t1.ID,#t2.ID) = COALESCE(#t2.ID,#t1.ID) 
                          AND COALESCE(#t1.ID2,#t2.ID2) = COALESCE(#t2.ID2,#t1.ID2)


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



I don't know if that works right when #t1.ID is null and #t2.ID2 is null and the other two are not. In that case it should not evaluate to true.
Go to Top of Page

ScottPletcher
Constraint Violating Yak Guru

USA
269 Posts

Posted - 02/20/2013 :  15:01:25  Show Profile  Reply with Quote

SELECT *
FROM #t1 t1
INNER JOIN #t2 t2 ON 1 = 
    CASE WHEN /*(t1.ID IS NOT NULL AND t1.ID2 IS NOT NULL) OR*/ (t2.ID IS NOT NULL AND t2.ID2 IS NOT NULL)
         THEN CASE WHEN (t1.ID = t2.ID AND t1.ID2 = t2.ID2) THEN 1 ELSE 0 END
    ELSE CASE WHEN
        (t1.ID IN (t2.ID, t2.ID2) OR t1.ID2 IN (t2.ID, t2.ID2)) THEN 1 ELSE 0 END
    END


Edited by - ScottPletcher on 02/20/2013 15:07:39
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.08 seconds. Powered By: Snitz Forums 2000