Someone knows how to union two or more materialized views in sql? - sql

select *
from
(materialized_v1
union all
materialized_v2);

You need to select, then union. There is nothing specific about materialized view here:
select * from materialized_v1
union all
select * from materialized_v2;
For this to work both views to have the same number of columns, with the same datatypes. It is far better to enumerate the columns in the select clauses, which gives you a chance to adjust the columns and datatypes if needed:
select col1, col2, col3 from materialized_v1
union all
select col4, col4, col5 from materialized_v2;

Related

Select a lot of columns from two different tables using hive SQL

I have two tables A & B. They share some columns. I want to select the common part of these two tables.
I know the following code is ok:
select col1, col2, ..., colN from A
union all
select col1, col2, ..., colN from B
However, I think the code is not good enough because I write col1, col2, ..., colN twice. It will be painful if I want to update the selected columns.
Does anybody have any other suggestion?
select * from A
union all CORRESPONDING BY (col1, col2, ..., colN)
select * from B
ANSI/ISO SQL-2016, Feature F301, CORRESPONDING in query expressions.

How to define and reuse list of columns in PostgreSQL

As the title says it, I have a huge table with a lot of columns, I need only like half or more of them (this is not the point). The point is I don't want to use * because I am getting too many and is taking a little more time than wanted.
I want to basically write a few UNION and writing all columns every time is taking a lot of space and looks messy. Instead what I am looking for is to declare a list of column names and simply reuse it in each SELECT statement if this is possible.
SELECT col1, col2, ... col_n FROM mytbl
UNION
SELECT col1, col2, ... col_n FROM mytbl
UNION
SELECT col1, col2, ... col_n FROM mytbl
UNION
SELECT col1, col2, ... col_n FROM mytbl
I want to get something like
columns_to_extract = [col1, col2, ... col_n]
SELECT columns_to_extract FROM mytbl
UNION
SELECT columns_to_extract FROM mytbl
UNION
SELECT columns_to_extract FROM mytbl
UNION
SELECT columns_to_extract FROM mytbl
Use some client-specific feature for that.
For example for psql client:
\set foobar 'aid, bid'
select :foobar from pgbench_accounts
union all
select :foobar from pgbench_accounts;

SQL - Transposing rows from some columns in a table to each record in thesame table

I am using a platform which accepts minimal SQL functions to write a SQL code. The UNPIVOT function cannot be used on the platform so I have to do this manually. I am thinking along the line of UNION ALL and then CROSS JOINING (which I attempted but ended up with the wrong record counts. Please see image attached.
Any help / pointer will be highly appreciated!
I don't know how you used UNION ALL but it can be done like this:
select col1, col2, col3 as NewCol from Table1
union all
select col1, col2, col4 from Table1
You could also use an ORDER BY clause, so that rows with the same col1 and col2 appear in subsequent rows:
select col1, col2, NewCol
from (
select col1, col2, col3 as NewCol, 1 as ord from Table1
union all
select col1, col2, col4, 2 from Table1
) t
order by col1, col2, ord
A portable approach uses union all:
select col1, col2, col3 as newcol from mytable
union all
select col1, col2, col4 from mytable
If your database supports lateral joins (also called cross apply in some databases) and values(), this can be simplified:
select t.col1, t.col2, x.newcol
from mytable t
cross join lateral (values(col3), (col4)) x(newcol)
You can use a cross join, but it requires some case logic. The exact syntax depends on the database, but something like this:
select t.col1, t.col2,
(case when n.n = 1 then t.col3 else t.col4 end) as newcol
from t cross join
(select 1 as n union all select 2) n;
To load another table, you would do one of the following:
insert these results into a table that has already been created.
Use select into or create table as depending on the database.
If you care about the ordering, then you can add order by t.col1, t.col2, n.n.
In most cases, a simple union all approach is fine (such as GMB suggests). That approach requires scanning the table twice, which incurs some additional overhead. However, if the "table" is really a complex query or view, then only processing it once is a bigger advantage.

How do I concatenate two similar tables on a result

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.

join two tables into one big table

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