copying fields from multiple tables to create new table - sql

How to create a table by copying only the columns from multiple tables??
eg. t1 with fields (A1,A2,A3)
t2 with fiedls (D1,D2,D3)
t3 with fields (Z1,Z2,Z3)
Now I've to create a new table using only the fields not the values from the above three tables i.e.
new_tbl (A1,A2,A3,D1,D2,D3,Z1,Z2,Z3)
How can I do it??

try this:
create table new_tbl as
select *
from t1
join t1 on 1=2
join t3 on 1=2
the condition 1=2 is always false.. so it will not return any data but the column header..

Related

Replacing the values in a column in select query

I have a table where one of the columns is ids as foreign key to another table. how can I replace the ids with data from the other table inside select query?
You use a join:
select t1.column_one, t2.display_value
from table_one t1
join table_two t2 on t1.fk_column_to_table_two = t2.primary_key_column;

Selecting rowset when value exists in one of 5 tables with different amounts of columns

Using SQL Server, I Need to return the entire row from whatever table contains 'value' in the Filename column (A column each of the tables contain), but the tables do not have the same number of columns, and each table has unique columns with their own specific data types (The only column Name/Type they have in common is the Filename column that I need to check for 'value').
Ideally, I would be able to do something along the lines of:
SELECT * FROM Table1, Table2, Table3, Table4, Table5
WHERE Filename = 'someValue'
Since all tables share the same column name for the Filename.
I have tried using Union but have issues since the number of columns and datatypes of the tables do not align.
I have also tried every combination of JOIN I could find.
I'm sure this could be accomplished with IF EXISTS, but that would be many, many lines of what seems like unnecessary code. Hoping there is a more elegant solution.
Thanks in advance!
You can try to join the tables together. First create temporary table where you store the input. And then join the tables with this temporary to get all records you want. When there is no record for that filename in the table, then you will get NULL values.
create table Table1 (id int,value int);
insert into Table1 values (1,10)
create table Table2 (id int,value int);
insert into Table2 values (1,20)
create table Table3 (id int,value int);
insert into Table3 values (2,30)
Here is the query itself
create table #tmp (id int)
insert into #tmp
values (1)
select t.id, t1.value, t2.value, t3.value from #tmp as t
left join Table1 as t1
on t.id = t1.id
left join Table2 as t2
on t.id = t2.id
left join Table3 as t3
on t.id = t3.id
And this is what you get
id value value value
1 10 20 NULL
this should work too:
EXEC sp_MSforeachtable
#command1='SELECT * FROM ? where filename = ''someValue''',
#whereand='AND o.id in (select object_id from sys.tables where name in (''Table1'',''Table2'',''Table3''))'

Do I need a stored procedure

I have three tables - T1,T2 & T3.
For each row in T1 I need to fetch the all the data from that table and a few other columns from a third table T3 and insert into T2 //Which is partitioned version of T1
Do I need stored procedure for this?
No you don't , it can be done with a simple insert as select:
INSERT INTO T2
SELECT t1.*,t3.col1,t3.col2...
FROM T1
LEFT OUTER JOIN t3
ON(t1.ID? = t3.ID?)
Of course you have to change this query to whatever columns you want, and the join condition to the relations between the tables.

create table to compare rows

In order to be able to use UTL_MATCH.JARO_WINKLER_SIMILARITY in oracle, i would need to have a table(temp table in my case) in following format
a ----------------abc
a ----------------bax
a ----------------tax
b ----------------abc
b ----------------bax
b ----------------tax
c ----------------abc
c ----------------bax
c ----------------tax
I have column in the LEFT coming from one table and the column in RIGHT from another table. My question is how to create table in such format?
I would highly appreciate your help.
If you want empty structure then:
create table test as
select table1.col1, table2.col2
from table1, table2
where 1=0
If you want table with data then:
create table test as
select distinct table1.col1, table2.col2
from table1, table2

How to store a table as two different tables using the AS function

I want to store table1 using the AS function as t1 and t2. How do I do this?
if you want to create table as well
SELECT * INTO table1
FROM t1;
If the table is already created
INSERT INTO Table1
SELECT* FROM t1;
INSERT INTO Table1
SELECT* FROM t2;
How to do INSERT into a table records extracted from another table
http://www.vbforums.com/showthread.php?529221-RESOLVED-create-table-as-select-in-MS-Access