Combining multiple Unions Queries - is this the best way? - sql

I've got some SQL code working at the moment but I need to do another union. I want to ensure what I do is correct. Currently, this is what I have:
select * from (select * from stametadata
union select * from stajjmetadata)
I want to union on another table. Would this be correct?
select * from (select * from stametadata
union select * from stajjmetadata
union select * from staggmetadata)
Is this the neatest way to do it?

You can chain unions. To be honest, you don't even need the outer query:
SELECT * FROM stametadata
UNION
SELECT * FROM stajjmetadata
UNION
SELECT * FROM staggmetadata

Related

How to Union Multiple Tables with same column names on Google Big Query?

I am trying to create a union with 3 tables that have the same column names. However, the queries tested seems not to be working.
The first query that I have used is the following:
Select * FROM table1
UNION ALL
SELECT * FROM table2
SELECT * FROM table3
UNION ALL
The second query used is the following:
SELECT *
FROM
(select * from table_1),
(select * from table_2),
(select * from table_3)
Both of them are not working for me. Please someone can help me with this?
This should work if columns are identical:
SELECT * FROM table1 UNION ALL
SELECT * FROM table2 UNION ALL
SELECT * FROM table3

SQL statement to return non-intersection records

I was recently asked this question and was a little stumped so I want to ask the experts...
Given two tables A & B, I want to return all the values from A and B that do not overlap. Think of two overlapping circles; how do we return all the data that is NOT in the overlapping center section? And, I had to use ANSI Standard SQL rather than Oracle syntax.
Assuming we want everything exclusive to both A & B, my answer was
select *
from A
cross join B
minus
(select a.common_column from a
intersect
select b.common_column)
Does this look correct, or even close? If it is correct, is there a more efficient way to do this?
BTW - my solution was soundly rejected....
Thank you!
Given the tables A and B, you are looking for (A U B) - (A & B). In other words, you need A union B minus their intersection. Remember A and B must be union-compatible for this query to work. I would do:
(select * from A
union
select * from B
)
minus
(select * from A
intersect
select * from B
)
May be full outer join?
select coalesce(A.col, B.col)
from A full outer join B on A.col = B.col
where A.col is null or B.col is null;
For computing a set symmetric difference, you can use a combination of MINUS and UNION ALL:
select * from (
(select * from A
minus
select * from B)
union all
(select * from B
minus
select * from A)
)
Your query was rejected because it is syntactically incorrect: the number of columns differ and it confuses cross join and union all. However, I think you have the right idea for solving this.
You can easily fix this:
(select *
from A
union all
select *
from B
) minus
(select *
from A
intersect
select *
from B
);
That is, combine everything using union all and then subtract the rows that occur in both tables.
Of course, if there is a single id, then you can use the id with join and other operations.
Just like Frank Schmitt answered in the meantime:
Here it is including a data example:
WITH
table_a(name) AS (
SELECT 'From_A_1'
UNION ALL SELECT 'From_A_2'
UNION ALL SELECT 'From_A_3'
UNION ALL SELECT 'From_A_4'
UNION ALL SELECT 'From_A_5'
UNION ALL SELECT 'From_BOTH_6'
UNION ALL SELECT 'From_BOTH_7'
UNION ALL SELECT 'From_BOTH_8'
)
,
table_b(name) AS (
SELECT 'From_B_1'
UNION ALL SELECT 'From_B_2'
UNION ALL SELECT 'From_B_3'
UNION ALL SELECT 'From_B_4'
UNION ALL SELECT 'From_B_5'
UNION ALL SELECT 'From_BOTH_6'
UNION ALL SELECT 'From_BOTH_7'
UNION ALL SELECT 'From_BOTH_8'
)
(SELECT * FROM table_a EXCEPT SELECT * FROM table_b)
UNION ALL
(SELECT * FROM table_b EXCEPT SELECT * FROM table_a)
ORDER BY name
;
name
From_A_1
From_A_2
From_A_3
From_A_4
From_A_5
From_B_1
From_B_2
From_B_3
From_B_4
From_B_5
You will need to select all the data from both tables, except where they overlap, and then combine the data with a union. The code provided should work for your example.
SELECT *
FROM
(
SELECT * FROM Table1
EXCEPT SELECT * FROM Table2
)
UNION
SELECT *
FROM
(
SELECT * FROM Table2
EXCEPT SELECT * FROM Table1
)
Hope this helps.

How to union many tables with same name but different schema without dynamic sql?

Microsoft SQL Server
[dbo].[MyTable]
[schema1].[MyTable]
[schema2].[MyTable]
[schema3].[MyTable]
I want to union them. For example:
select * from [*].[MyTable]
Can I do that?
Something like this
select * from [dbo].[MyTable]
UNION
select * from [schema1].[MyTable]
UNION
select * from [schema2].[MyTable]
UNION
select * from [schema3].[MyTable]

Join two tables with geometry column into one

I have two tables that are exactly the same. I want to join them together into one large dataset. I tried simply SELECT-INTO query but got an error...
SELECT * INTO dbo.ParkingBay
FROM (SELECT * FROM dbo.ParkingBay_Old
UNION
SELECT * FROM dbo.ParkingBay_New) AS PARKING_BAY;
The error is:
The geometry data type cannot be selected as DISTINCT because it is
not comparable.
The UNION performs a DISTINCT on the combined result set.
UNION ALL eliminates this DISTINCT step, but would create the possibility of dupes in the result.
If you are OK with dupe possibility, then try this
SELECT * INTO dbo.ParkingBay
FROM (SELECT * FROM dbo.ParkingBay_Old
UNION ALL
SELECT * FROM dbo.ParkingBay_New) AS PARKING_BAY;
It looks like ALL solves everything:
SELECT * INTO dbo.ParkingBay
FROM (SELECT * FROM dbo.ParkingBay_Old
UNION ALL
SELECT * FROM dbo.ParkingBay_New) AS PARKING_BAY;

Access append query inconsistent

I have a table creation query (combine2) which takes a query (combine) and makes it a table (linegraph). This table must reflect changes in the associated query. Currently I have assigned a macro to run the append query to reflect changes.
The problem is that no matter how the append query is called it sometimes but not always copies all the data. Sometimes it wont import the first 100 or so rows. How can I get an updating table that always matches my query?
Append Query: Combine2
INSERT INTO linegraph SELECT * FROM Combine;
Query to turn into table: Combine
SELECT * FROM Month1calc UNION
SELECT * FROM Month2calc UNION
SELECT * FROM month3calc UNION
SELECT * FROM Month4calc UNION
SELECT * FROM Month5calc UNION
SELECT * FROM Month6calc UNION
SELECT * FROM Month7calc UNION
SELECT * FROM Month8calc UNION
SELECT * FROM Month9calc UNION
SELECT * FROM Month10calc UNION
SELECT * FROM Month11calc UNION
SELECT * FROM Month12calc UNION
SELECT * FROM Month13calc UNION
SELECT * FROM Month14calc UNION
SELECT * FROM Month15calc UNION
SELECT * FROM Month16calc UNION
SELECT * FROM Month17calc UNION
SELECT * FROM Month18calc;
`
Try changing UNION to UNION ALL.
When you use UNION, the query only returns unique rows (i.e.: duplicates are removed). When you use UNION ALL, duplicates are not removed.