Author |
Topic |
ovince
Starting Member
32 Posts |
Posted - 2006-11-14 : 00:36:09
|
hi All,I have 3 tables and I would just like to join them into one, matching 3 columns. Something like thistable1========pk1a1a2a3and other columnstable2========pk2b1b2b3and other columnstable3========pk3c1c2c3and other columnsIn the output I should have ONLY data wherea1=b1=c1 a2=b2=c2 a3=b3=c3 I am sure this is easy but would like to be sure that I am doing it correctly and in a most efficient way.ThanksOliver |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-11-14 : 00:38:00
|
This is just a case of simple joins. Tell us what have you done so far.Harsh AthalyeIndia."Nothing is Impossible" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-14 : 00:47:07
|
The only way I can think of that this requirements will workquote: In the output I should have ONLY data wherea1=b1=c1 a2=b2=c2 a3=b3=c3
is following-- Stage the correct combinaction of first three columnsDECLARE @Stage TABLE ( col1 INT, col2 INT, col3 INT )INSERT @Stage ( col1, col2, col3 )SELECT col1, col2, col3FROM ( SELECT a1 col1, a2 col2, a3 col3 FROM Table1 UNION ALL SELECT b1, b2, b3 FROM Table2 UNION ALL SELECT c1, c2, c3 FROM Table3 ) wGROUP BY col1, col2, col3HAVING COUNT(*) > 1-- Do the workSELECT t1.<collist>FROM Table1 t1INNER JOIN @Stage s ON s.col1 = t1.col2 AND s.col2 = t1.col2 AND s.col3 = t1.col3UNION ALLSELECT t2.<collist>FROM Table1 t2INNER JOIN @Stage s ON s.col1 = t2.col2 AND s.col2 = t2.col2 AND s.col3 = t2.col3UNION ALLSELECT t3.<collist>FROM Table1 t3INNER JOIN @Stage s ON s.col1 = t3.col2 AND s.col2 = t3.col2 AND s.col3 = t3.col3 Peter LarssonHelsingborg, Sweden |
 |
|
ovince
Starting Member
32 Posts |
Posted - 2006-11-14 : 00:48:44
|
I did something like thisSELECT *.t1, *.t2, *.t3 FROM Table1 t1, Table2 t2, Table3 t3WHEREt1.a1 = t2.b1 ANDt2.b1 = t3.c1 AND--t1.a2 = t2.b2 ANDt2.b2= t3.c2 AND--t1.a3 = t2.b3 ANDt2.b3 = t3.c3 |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-14 : 00:54:53
|
[code]SELECT t1.<collist>, t2.<collist>, t3.<collist>FROM Table1 t1INNER JOIN Table2 t2 ON t2.col1 = t1.col1 AND t2.col2 = t1.col2 AND t2.col3 = t1.col3INNER JOIN Table3 t3 ON t3.col1 = t2.col1 AND t3.col2 = t2.col2 AND t3.col3 = t2.col3[/code]Peter LarssonHelsingborg, Sweden |
 |
|
ovince
Starting Member
32 Posts |
Posted - 2006-11-14 : 01:23:30
|
thanks for reply,Do you have an idea how to export the output of Studio Management in ASCII format together with the column names? |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-14 : 01:30:27
|
Right click the results windows and choose Save Result As... ?Peter LarssonHelsingborg, Sweden |
 |
|
ovince
Starting Member
32 Posts |
Posted - 2006-11-14 : 01:42:06
|
yes but with 'Save Res As' I steel do not have column names in the saved files. Is there some option that has to be adjusted? How do you deal with this? |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-14 : 01:44:33
|
You set that option in Tools -> Option -> Query Results -> SQL Server -> Results to Gridand "Include column headers..."Peter LarssonHelsingborg, Sweden |
 |
|
ovince
Starting Member
32 Posts |
Posted - 2006-11-14 : 01:52:22
|
thanks Peso... |
 |
|
|