How to copy one table to another - sql

I want to copy the contents of table1 into table2, but it's not a straight copy, as table2 contains more columns than table 1. The structure is similar to this:
Table1
{
column2
column4
column6
}
Table2
{
column1
column2
column3
column4
column5
column6
}
What I want to do is add every row from table1 to table2, and set default values for the missing columns. Any help would be appreciated.

You can just do an
INSERT INTO xxx
SELECT yyy
And in the select clause, put default values.
INSERT INTO Table2(column1, column2, column3, column4, column5, column6)
SELECT 'horse', column2, 2, column4, 'what ever I want', column6
FROM Table1
So int Table2, all column1 will have 'horse' value.
All column3 will have 2.
Etc.

use INSERT..INTO SELECT statement
INSERT INTO table2 (column2, column4, column6)
SELECT column2, column4, column6
FROM table1
so in this case, columns: column1, column2, column3 will have null values. or whatever default you have set.

Not great at SQL but something like this:
INSERT INTO [Table1] (column2, column4, column6) SELECT (column1, column2, column3) FROM [Table2]
Hope this helps. Link that may be useful, http://www.blackwasp.co.uk/SQLSelectInsert.aspx
vote me I need points :P

Please try
INSERT INTO
Table2 (
column1,
column2,
column3,
column4,
column5,
column6,
)
SELECT
'StaticValue1',
column2,
'StaticValue2'
column4,
'StaticValue3'
column6,
FROM
Table1

To copy a full table into a new table:
SELECT * INTO table2 FROM table1;
http://www.w3schools.com/sql/sql_select_into.asp
To copy a table into an existing table:
INSERT INTO table2
SELECT * FROM table1;
http://www.w3schools.com/sql/sql_insert_into_select.asp

insert into TABLE2
select null colum1, column2,null colum3,column4,null colum5,column6
from((
Select TABLE1.column2, TABLE1.column4, TABLE1.column6
from TABLE1, TABLE2
where TABLE1.primarykey=TABLE2.primarykey))

Related

How to run two queries with the second query using the results from the first

I have tried looking this up, but i am not sure if my terminology is correctly.
My initial query works fine, however i want to do a calculation based on the results from the first query.
Its not possible to do this from the initial query due to the Inner Joins and Group By
I can accomplish this by saving this Query as a View and running my second query against the view. But this is not ideal for the usage.
What is the correct wording for using two queries in such a way, the second using the results from the first.
Example:
Select, column1, column2, column3, column4, SUM(column5) as column5, SUM(column6) as column6, TableB.column7
From TableA
Left Out Join TableB
on TableB.column7 = TableA.column1
Group By column1, column2, column3, column4
Select column1, column2, column3, column4, column5, column6,
(ISNULL (NULLIF(column5 - column6,0) / NULLIF(column5,0),0) * 100) else 0 end as columnGP
from (Previous Query just ran)
How can i store the results as a Variable, how long is the Data held as a variable? I storing as a variable will have a knock on effect from a performance side
You can use the first query as a sub-query in the second one
Select
column1, column2, column3, column4, column5, column6,
(ISNULL (NULLIF(column5 - column6,0) / NULLIF(column5,0),0) * 100) else 0 end as columnGP
from (
Select, column1, column2, column3, column4, SUM(column5) as column5, SUM(column6) as column6, TableB.column7
From TableA
Left Out Join TableB
on TableB.column7 = TableA.column1
Group By column1, column2, column3, column4
) x
To do this enclose the sub-query in braces and give it an alias acting as a table name (here x).
I just copied your queries; however, there seems to be a syntax error in them. else 0 end looks like the end of a missing case-expression.
But you can do the calculation in a single query
Select
column1, column2, column3, column4,
SUM(column5) as sum_column5, SUM(column6) as sum_column6,
TableB.column7,
CASE WHEN SUM(column5) = 0
THEN 0
ELSE (SUM(column5) - SUM(column6)) / SUM(column5) * 100
END AS columnGP
From
TableA
Left Outer Join TableB
on TableB.column7 = TableA.column1
Group By
column1, column2, column3, column4, TableB.column7
Make nested query. So instead of two queries, do one query where "Previous Query just run" is replaced with first query
SELECT column1,
column2,
column3,
column4,
column5,
column6,
(ISNULL (NULLIF(column5 - column6,0) / NULLIF(column5,0),0) * 100) else
0 end as columnGP
FROM (SELECT column1,
column2,
column3,
column4,
SUM(column5) as column5,
SUM(column6) as column6,
TableB.column7
FROM TableA
LEFT OUT JOIN TableB ON TableB.column7 = TableA.column1
GROUP BY column1, column2, column3, column4)

How can I do an INSERT INTO from 3 different tables?

My goal is to fill this table with data:
Table_1
ID_coordinates, ID_text, column3, column4, column5, column6, column7
ID_coordinates is from Table_2:
ID_coordinates, latitude, longitude
ID_text is from Table_3
ID_text, income
column3, column4, column5, column6, column7 are from Table_origin: this table also has columns latitude, longitude and income.
Do I need 3 different insert into statements with select or one insert into statement with joins?
Many thanks for all tips and answers.
One statement with Joins (https://www.w3schools.com/sql/sql_insert_into_select.asp).
If I have followed all the columns on the tables correctly it should be something along these lines:
INSERT INTO table_1 (ID_coordinates, ID_text, column3, column4, column5, column6, column7)
SELECT t2.ID_coordinates, t3.ID_text, o.column3, o.column4, o.column5, o.column6, o.column7
FROM table_origin o, table_2 t2, table_3 t3
WHERE o.latitude = t2.latitude
AND o.longitude = t2.longitude
AND o.income = t3.income

How to delete the rownames column from a table in SQL Server?

I want to transfer rows from one table to another. However, one table has rownames while the other doesn't.
The error I get is:
Incorrect syntax near the word 'from'
How to handle this situation?
You can try:
INSERT INTO Table1 (Column1, Column2, Column3)
SELECT Column1, Column2, Column3
FROM Table2
WHERE Table2.Column1 = 'Test'
IF the columns are same in the both the table :
INSERT INTO table2
SELECT * FROM table1
WHERE condition;
If you want to transfer few rows but the the column names are different :
INSERT INTO table2 (column1, column2, column3) SELECT (column1, column2, column3) FROM table1 WHERE condition;

How to combine two table columns into one column in third table

i have a hive table Android_1 with 6 columns among those 6 columns:
android_id, column1, column2, column3, column4, column5
another table ios_2 with 6 columns among those tables:
ios_id, column1, column2, column3, column4, column5
now i have table combine which has 5 columns among those 5 columns one column is id and rest are column1,column2, column3,column4.now i want to insert the data into the table combine so that both data in the Android_id and ios_id should come under id column in table combine and the data in Android_1 in column1,column2,column3,column4 and the data in ios_2 in column1,column2, column3,column4 shoud come under table combine column1,column2, column3,column4
Sounds to me like you want a UNION -- like this:
SELECT android_id as id, column1, column2, column3, column4, column5
FROM android_table
UNION ALL
SELECT ios_id as id, column1, column2, column3, column4, column5
FROM ios_table

Remove duplicate based on condition

My table value:
COLUMN1 COLUMN2 COLUMN3
WF1 Email 1640
WF1 Email 1641
WF1 Email N/A
WF3 Email N/A
Expected Result:
COLUMN1 COLUMN2 COLUMN3
WF1 Email 1640
WF3 Email N/A
I need to retrieve all records which column2 = 'Email' and if column1 contains duplicate value, i have to choose the record which column3 <> 'N/A'.
I read tutorial about partition by but still not sure how to get the result.
Any help is appreciated.
CREATE TABLE TABLE1
(
COLUMN1 varchar2(20),
COLUMN2 varchar2(20),
COLUMN3 varchar2(20)
);
INSERT INTO TABLE1
(COLUMN1, COLUMN2, COLUMN3)
VALUES
('WF1', 'Email', '1640');
INSERT INTO TABLE1
(COLUMN1, COLUMN2, COLUMN3)
VALUES
('WF1', 'Email', '1641');
INSERT INTO TABLE1
(COLUMN1, COLUMN2, COLUMN3)
VALUES
('WF1', 'Email', 'N/A');
INSERT INTO TABLE1
(COLUMN1, COLUMN2, COLUMN3)
VALUES
('WF3', 'Email', 'N/A');
Try something like this:
SELECT column1, column2, column3
from(
SELECT column1, column2, column3,
row_number() over (partition BY column1, column2 ORDER BY CASE WHEN column3 = 'N/A' THEN 999999999 ELSE to_number(column3) END ) rn
FROM table1)
WHERE rn = 1
Here is a sqlfiddle
select column1,column2,max(case when column3 = 'N/A' then '0' else column3 end) column3
from table1
group by column1,column2;
If column1 can be not unique in the result:
select column1, column3 from table1 t1
where column3 != 'N/A' or
column3 = 'N/A' and not exists
(select * from table1 t2
where t2.column1 = t1.column1 and t2.column3 != 'N/A')
If column1 must be unique in the result:
select column1, column3 from table1 t1
where column3 != 'N/A' and not exists
(select * from table1 t2
where t2.column1 = t1.column1 and t2.column3 != 'N/A' and
to_number(t2.column3, '9999') > to_number(t1.column3, '9999')
) union
select column1, column3 from table1 t1
where column3 = 'N/A' and not exists
(select * from table1 t2 where t2.column1 = t1.column1 and t2.column3 != 'N/A')
Note that we use union instead of union all.