Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: TSQL
I have two table A and B,
 
Table A.
col 1    col 2
Row1     A
Row2     A
Row3     A
Row4     A
Row5     A
Row6     A
 
Table B.
col 1    col 2
Row1     B
Row2     B
Row3     B
 
and I need following output
 
Row1     A
Row1     B
Row2     A
Row2     B
Row3     A
Row3     B
 
how can I do this, thanx in advanced...
Posted 11-Sep-12 20:54pm
Comments
gvprabu at 28-Sep-12 5:19am
   
If u want to make thru joins... Its difficult.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi ,
Check this
select tb1.Col1 , tb2.col2 from tableA  as tb1 Inner join
Tableb as tb2 on tb1.col1 = tb2.col1
SQL Joins[^]
http://blog.sqlauthority.com/2009/04/13/sql-server-introduction-to-joins-basic-of-joins/[^]
Best Regards
M.Mitwalli
  Permalink  
Comments
Manas Bhardwaj at 28-Sep-12 5:16am
   
5+
Mohamed Mitwalli at 3-Oct-12 1:44am
   
Thanks Manas :)
gvprabu at 28-Sep-12 5:16am
   
Hi Mohamed Mitwalli,
ur query will give different Output., pls check with sample...
Mohamed Mitwalli at 3-Oct-12 1:45am
   
Thanks and i will check it :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi Check the Following Example ....
 
CREATE TABLE dbo.Tab1 (Col1 VARCHAR(10), Col2 VARCHAR(10))
CREATE TABLE dbo.Tab2 (Col1 VARCHAR(10), Col2 VARCHAR(10))
 
INSERT INTO dbo.Tab1 (Col1, Col2)
SELECT 'Row1','A'
UNION ALL 
SELECT 'Row2','A'
UNION ALL 
SELECT 'Row3','A'
UNION ALL 
SELECT 'Row4','A'
UNION ALL 
SELECT 'Row5','A'
UNION ALL 
SELECT 'Row6','A'
 
INSERT INTO  dbo.Tab2 (Col1, Col2)
SELECT 'Row1','B'
UNION ALL 
SELECT 'Row2','B'
UNION ALL 
SELECT 'Row3','B'
 

SELECT T2.Col1,T2.Col2  
FROM dbo.Tab2 T2 
UNION
SELECT T1.Col1 , T1.Col2
FROM dbo.Tab1 T1 
WHERE T1.Col1 IN (SELECT Col1 FROM dbo.Tab2)
 
 
It will helps u....
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 George Jonsson 175
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 28 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100