Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

sql -Join To get all Records

$
0
0

Hi Experts ,

I am working on SQL-2008
I have Table One and Table Two ..I need to Join then In Such a Way to Get Table Three..
I tried Left,Right and Full Join But Did not Get Desire Result ..
help me On this to ACHIEVE OUTPUT of TABLE THREE using Table One and Table Two..
Below is Sample Script .

DECLARE @TBL_ONE TABLE (ID_ONE INT ,F_NAME NVARCHAR(50),MIDDLE_NAME NVARCHAR(50),L_NAME NVARCHAR(50))
INSERT INTO @TBL_ONE 
SELECT 20,'JOHN','P','DEN' UNION ALL
SELECT 30,'SAM','L','JORDAN'

DECLARE @TBL_TWO TABLE (ID_TW0 INT ,CODE INT ,F_NAME NVARCHAR(50),MIDDLE_NAME NVARCHAR(50),L_NAME NVARCHAR(50))
INSERT INTO @TBL_TWO 
SELECT 21,201,'JOHN','P','DEN' UNION ALL
SELECT 22,202,'JOHN','P','DEN' UNION ALL
SELECT 23,203,'JOHN','','DEN' UNION ALL
SELECT 24,204,'JOHN','','DEN' UNION ALL
SELECT 31,304,'SAM','L','JORDAN' UNION ALL
SELECT 32,305,'SAM','L','JORDAN' 

SELECT * FROM @TBL_ONE
SELECT  * FROM @TBL_TWO
/*below is Format in Which Output is Needed*/
DECLARE @Output_TBL_THREE TABLE (ID_ONE INT ,ID_TWO INT ,CODE INT ,F_NAME NVARCHAR(50),MIDDLE_NAME NVARCHAR(50),L_NAME NVARCHAR(50))
INSERT INTO @Output_TBL_THREE
SELECT 20,21,201,'JOHN','P','DEN' UNION ALL
SELECT 20,22,202,'JOHN','P','DEN' UNION ALL
SELECT 20,23,203,'JOHN','P','DEN' UNION ALL
SELECT 20,24,204,'JOHN','P','DEN' UNION ALL
SELECT 30,31,304,'SAM','L','JORDAN' UNION ALL
SELECT 30,32,305,'SAM','L','JORDAN' 

SELECT * FROM @Output_TBL_THREE

NOTE : Table One and Table Two Should be Joined On F-Name,L_name and Middle_Name (All Three Columns)







Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>