I have two tables with similar columns. I would simply like to select both tables, one after another, so that if I have 'x' rows on table1 and 'y' rows on table2, I'd get 'x + y' rows.
You would use UNION [ALL] for this. The tables don't need to have the same column names but you do need to select the same number of columns from each and the corresponding columns need to be of compatible datatypes
SELECT col1,col2,col3 FROM table1
UNION ALL
SELECT col1,col2,col3 FROM table2
UNION ALL is preferrable to UNION where there is a choice as it can avoid a sort operation to get rid of duplicates.
Just to add to what they were saying, you might want to add an Order By. Depends on the version of SQL you're using.
SELECT Col1, Col2, Col3
FROM Table1
UNION
SELECT Col1, Col2, Col3
FROM Table2
ORDER BY Col1
Note that ORDER and GROUP BYs have to go after the last table in the UNION.
select col1,col2,col3 from table1
union
select col1,col2,col3 from table2
Look at the Union operator.
Related
I have two tables:
This is table1:
customer_name|customer_id|customer_phone|customer_birthday|(10 more columns that don't match with table2)
This is table2:
customer_name|customer_id|customer_phone|customer_status|(141 columns)
table1.customer_id is numeric
table2.customer_id is alphanumeric
table2 was imported from an old database and contains legacy customer data
How do I get data from both tables while only specifying the columns I want from each table.
The two tables have no overlapping data.
Please help me with this. I am not sure how to resolve it
Using SQL Server 2019
You could simply combine data from the both tables using UNION:
select customer_name, cast(customer_id as varchar), customer_phone, customer_birthday, null customer_status from table1
union all -- no need to do a useless distinct
select customer_name, customer_id, customer_phone, null as customer_birthday, customer_status from table2
As long as you have same column names, you can make a list of columns and use UNION like below:
SELECT col1, col2, col3, ...
FROM t1
UNION
SELECT col1, col2, col3
FROM t2;
I have 2 tables in Vertica SQL and how can I union all these tables to create tables as in result ? As you can see tables 1 and table 2 have different order of columns:
enter image description here
Just list the columns in the order you want them:
select col1, col2, col3
from table1
union all
select col2, col1, null
from table2;
Use SELECT INTO and UNION ALL would solve your problem
SELECT * INTO TEMP TABLE RESULT FROM TABLE1
UNION ALL
SELECT * FROM TABLE2
I would like to pull all of the columns from my second select statement and put it to the right of all of my columns from my first statement.
I have tried Union and the join commands with no luck.
When I use these they just have what I wanted from my first select statement.
Here is basic code I have.
Select * from MTG_TREND where LINEID='A2' end;
Select * from MTG_TREND where LINEID='B2'
All of the other columns are the same.
Select t1.*,t2.* from MTG_TREND t1
left join (
Select * from MTG_TREND where LINEID='B2'
) as t2
on t1.primarykey=t2.primarykey
where t1.LINEID='A2'
NOTE:
- Make sure you are matching the same number of columns in both SELECT Statement.
- Another thing to remember is you have to have the same matched column (datatype).
- If there are some columns that are not present is the table then just defined the column with null so that you can get it in output from another select statement.
Select col1, col2, col3,...... from MTG_TREND where LINEID='A2'
UNION ALL
Select col1, col2, col3,...... from MTG_TREND where LINEID='B2'
I have several different tables, but they all have 2 columns that are the same name. I want to write a stored procedure that searches one column in all of the tables and returns the result. Ideas? I'm fairly nub when it comes to SQL.
The operation you are looking for is UNION or UNION ALL.
SELECT * FROM (
SELECT col1, col2 FROM table1
UNION ALL
SELECT col1, col2 FROM table2
UNION ALL
SELECT col1, col2 FROM table3
) all_tables
WHERE all_tables.col1 = 'something'
If you use UNION rather than UNION ALL, the database will eliminate duplicate rows that may be in multiple tables. If you know there won't be any duplicates, use UNION ALL since it is generally much faster.
Select myColumn FROM Table1
UNION Select myColumn FROM Table2
UNION Select myColumn FROM Table3
..etc
-- Note all column names have to be the same and have to be in each table for this to work
You wouldn't even need a stored procedure...just use a union query.
select field1, field2 from table1 where table1.field1=criteria
union
select field1, field2 from table2 where table2.field1=criteria
union
select field1, field2 from table3 where table3.field1=criteria
etc...
I have two tables with the same columns, and I need to copy one table's rows to the other table's rows to create one big table with all the values from both tables. Right now I am doing this query to return the same thing:
SELECT col1, col2, col3 from Table1
union
SELECT col1, col2, col3 from Table2
However, it seems horribly inefficient, and on my system is very slow (returns 1210189 records).
May it work to just do:
SELECT col1, col2, col3
INTO Table1
FROM Table2
Start with union all:
select col1, col2, col3 from Table1
union all
select col1, col2, col3 from Table2
Your query is trying to deduplicate things, which would slow it down considerably.
I think the best option is to create a view in sql server, this will optimize the performance of the query:
SELECT col1, col2, col3 from Table1
union all
SELECT col1, col2, col3 from Table2
(As other users said: "union" is used to select distinct values from two tables
where as "union all" is used to select all values including
duplicates from the tables.)
At the same time I would restrict the number of rows I get from the database if i am writing them for a web and if this is giving me problems, with the new functions of Sql Server 2005 row_number(), with this I would page results.
You could use this to fill the second table:
Insert into table2 select * from table1;
Or if you want to be more specific:
Insert into table2(col1, col2, col3) select col1, col2, col3 from table1;
(Note: some DBMSs might require putting parenthesis around the SELECT clause.)
select * into new table(your new table name)
from table1.col1,table1.col2,table2.col1;
here columns can be your required columns .
select * into newtable from table1
union all
select * from table2
Worked well. Guidelines, both tables have exact same column names :)