Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Vaba_
Starting Member
2 Posts |
Posted - 2007-08-13 : 07:25:09
|
I have a very poorly designed database (sorry, I can't redesign it) and I should get some information with sql.Database has 5 tables and each of them has one column.Each table has the same amount of rows.I have a table which has same columns as those 5 tables.Now I should get the information from those 5 tables into this one table.The difficulty is that those 5 tables don't have anything that joins them together. Only thing is that they have the same amount of rows.Example:Table1:column_name1123Table2:column_name2acbTable3:column_name3asdsdfdfgTable4:column_name4123476549876Table5:column_name5yyn Result should be (table already exists, I should get the data):My_Table:column_name1 column_name2 column_name3 column_name4 column_name51 a asd 1234 y2 b sdf 7654 y3 c dfg 9876 n |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2007-08-13 : 07:52:18
|
| If the tables are in the order you want then you can do something like this, which gives you something to join onselect a.col1,b.col1,c.col1,d.col1,e.col1from(select 'rownum' = row_number() over (order by col1),col1 from @table1) ainner join(select 'rownum' = row_number() over (order by col1),col1 from @table1) bon a.rownum = b.rownuminner join(select 'rownum' = row_number() over (order by col1),col1 from @table1) con a.rownum = c.rownuminner join(select 'rownum' = row_number() over (order by col1),col1 from @table1) don a.rownum = d.rownuminner join(select 'rownum' = row_number() over (order by col1),col1 from @table1) eon a.rownum = e.rownumor use the IDENTITY function similarly. But when you finish you will have now have 6 tables that don't link up instead of 5.Jim |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-08-13 : 07:58:36
|
[code]DECLARE @table1 TABLE( column_name1 int)INSERT INTO @table1 SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3DECLARE @table2 TABLE( column_name2 CHAR(1))INSERT INTO @table2 SELECT 'a' UNION ALL SELECT 'c' UNION ALL SELECT 'b'DECLARE @table3 TABLE( column_name3 varchar(3))INSERT INTO @table3 SELECT 'asd' UNION ALL SELECT 'sdf' UNION ALL SELECT 'dfg'DECLARE @table4 TABLE( column_name4 int)INSERT INTO @table4 SELECT 1234 UNION ALL SELECT 7654 UNION ALL SELECT 9876DECLARE @table5 TABLE( column_name5 CHAR(1))INSERT INTO @table5 SELECT 'y' UNION ALL SELECT 'y' UNION ALL SELECT 'n'SELECT t1.column_name1, t2.column_name2, t3.column_name3, t4.column_name4, t5.column_name5FROM ( SELECT row = row_number() OVER (ORDER BY column_name1), column_name1 FROM @table1 ) t1INNER JOIN ( SELECT row = row_number() OVER (ORDER BY column_name2), column_name2 FROM @table2 ) t2 ON t1.row = t2.rowINNER JOIN ( SELECT row = row_number() OVER (ORDER BY column_name3), column_name3 FROM @table3 ) t3 ON t1.row = t3.rowINNER JOIN ( SELECT row = row_number() OVER (ORDER BY column_name4), column_name4 FROM @table4 ) t4 ON t1.row = t4.rowINNER JOIN ( SELECT row = row_number() OVER (ORDER BY column_name5 DESC), column_name5 FROM @table5 ) t5 ON t1.row = t5.row/*column_name1 column_name2 column_name3 column_name4 column_name5 ------------ ------------ ------------ ------------ ------------ 1 a asd 1234 y 2 b dfg 7654 y 3 c sdf 9876 n*/[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-08-13 : 07:59:22
|
damn ! KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
Vaba_
Starting Member
2 Posts |
Posted - 2007-08-13 : 08:38:40
|
| Ok, thanks for quick reply! |
 |
|
|
|
|
|
|
|