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

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

Related

SQL self join to eradicate duplicate keys

I have a table with below columns:
Column1
Column2
Column3
A
Hello
NULL
A
NULL
WORLD
I want the above table to transform like below:
Column1
Column2
Column3
A
Hello
WORLD
I'm using Snowflake DataWarehouse. Need help in the above transformation using SQL
select column1,
max(column2) as column2,
max(column3) as column3
from your_table
group by column1;

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

Convert one row into 3 rows and insert into a table

I have a select query with DB link which retrieves 15 columns per row from another DB.
I want to split the row retrieved and insert the data into three different tables.
Example:
SELECT column1, column2, column3, .... , column15 FROM table_x#db_link;
SELECT query result: Columns 01-15
Target Tables:
Table ABC: Columns 01-05;
Table XYZ: Columns 06-10;
Table PQR: Columns 11-15
Kindly suggest a way to do this. I am on Oracle 11g DB.
Thanks!
You are describing insert all:
INSERT ALL
INTO ABC VALUES (column1, column2, column3, column4, column5)
INTO XYZ VALUES (column6, column7, column8, column9, column10)
INTO PQR VALUES (column11, column12, column13, column14, column15)
SELECT column1, column2, column3, .... , column15
FROM table_x#db_link;

How in SQL (Redshift) to select with IF condition?

I have a 5 columns table. Column1 which has values 'A', 'B', 'C' and 'D' and another column2 having int values from 0 to 20, and column3, column4 and column5 have other values, not important for now.
I want to select all records in the table, but not records having in column1 value 'B'. However, records which have in colum1 value 'B', should meet the condition having in column2 a value > 10. If they have in column2 for ex. 15, we can include them in the select.
How to select this?
SELECT
column1
, column2
, column3
, column4
, column5
FROM tableA
WHERE
only_include_values_B_from_column1_IF_it_has_in_column2_value_superior_than_10
If I understand correctly, you want OR:
SELECT column1, column2, column3, column4, column5
FROM tableA a
WHERE column1 <> 'B' OR column2 > 10

How to copy one table to another

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))