I can unify two tables via:
select * from project.dataset.ourtable1
union all
select * from project.dataset.ourtable2
But what if I have thousands of tables, and I want to unify all which have name starting with ourtable?
I can get all such tables via:
select table_id from project.dataset.__TABLES__
where starts_with(table_id,'ourtable')
which returns a column of tables with table_id starting with ourtable.
How do I perform union all on all of them?
To rephrase the question: I am looking for the equivalent of
select * from project.dataset.ourtable1
union all
select * from project.dataset.ourtable2
union all
.
.
.
union all
select * from project.dataset.ourtable9999
in BigQuery.
A similar thread: here, but it is for SQL-Server, not BQ.
You can use a wildcard:
select * from `project.dataset.ourtable*`
Related
I can unify two tables via:
select * from project.dataset.ourtable1
union all
select * from project.dataset.ourtable2
But what if I have thousands of tables, and I want to unify all which have name starting with ourtable?
I can get all such tables via:
select table_id from project.dataset.__TABLES__
where starts_with(table_id,'ourtable')
which returns a column of tables with table_id starting with ourtable.
How do I perform union all on all of them?
To rephrase the question: I am looking for the equivalent of
select * from project.dataset.ourtable1
union all
select * from project.dataset.ourtable2
union all
.
.
.
union all
select * from project.dataset.ourtable9999
in BigQuery.
A similar thread: here, but it is for SQL-Server, not BQ.
You can use a wildcard:
select * from `project.dataset.ourtable*`
Beginner's question here... I have a table of tree measurements being 3 fields: - ID, Diameter_1, Diameter_2
& I wish to get to these 3 fields: - ID, DiameterName, DiameterMeasurement
Input and Desired Output
SELECT DISTINCT ID, Diameter_1
FROM tblDiameters
UNION SELECT DISTINCT ID, Diameter_2
FROM tblDiameters;
Though it results in only 2 fields. How may the field: - DiameterMeasurement be brought in?
Many thanks :-)
You were on the right track to use a union. Here is one viable approach:
SELECT ID, 'Diameter_1' AS DiameterName, Diameter_1 AS DiameterMeasurement
FROM tblDiameters
UNION ALL
SELECT ID, 'Diameter_2', Diameter_2
FROM tblDiameters
ORDER BY ID, DiameterName;
I'm new to the community but have referenced it many times in the past. I have an issue I'm trying to overcome in Access, specifically with a SORT BY issue in SQL.
Long story short, I need to create a report based on the results of several different queries. I used a Union query to skirt the "Query is too complex" issue. The results of the query aren't in the order I'd like them, though.
Since this UNION query is not based on one specific table, rather the results of many queries, I'm not able to sort by a specific column header.
I want to sort the results by the way they are written in the SQL statement. Can anyone provide some insight to how to do this? I've attempted several different ways but always end up with an error message. Here's the code, and any help is greatly appreciated.
SELECT [Aqua-Anvil_Total].Expr1
FROM [Aqua-Anvil_Total];
UNION SELECT [Aqua-Reslin_Total].Expr1
FROM [Aqua-Reslin_Total];
UNION SELECT [Aqua_Zenivex_Total].Expr1
FROM [Aqua_Zenivex_Total];
UNION SELECT [Aqualuer_20-20_Total].Expr1
FROM [Aqualuer_20-20_Total];
UNION SELECT [Avalon_Total].Expr1
FROM [Avalon_Total];
UNION SELECT [BVA_13_Total].Expr1
FROM [BVA_13_Total];
UNION SELECT [Deltagard_Total].Expr1
FROM [Deltagard_Total];
UNION SELECT [Envion_Total].Expr1
FROM [Envion_Total];
UNION SELECT [Scourge_18-54_Total].Expr1
FROM [Scourge_18-54_Total];
UNION SELECT [Zenivex_E20_Total].Expr1
FROM [Zenivex_E20_Total];
This uses union all instead of union, so if you are using union to remove duplicates, there would be more work to do after this.
select Expr1
from (
select [Aqua-Anvil_Total].Expr1, 0 as sort
from [Aqua-Anvil_Total]
union all select [Aqua-Reslin_Total].Expr1, 1 as sort
from [Aqua-Reslin_Total]
union all select [Aqua_Zenivex_Total].Expr1, 2 as sort
from [Aqua_Zenivex_Total]
union all select [Aqualuer_20-20_Total].Expr1, 3 as sort
from [Aqualuer_20-20_Total]
union all select [Avalon_Total].Expr1, 4 as sort
from [Avalon_Total]
union all select [bva_13_Total].Expr1, 5 as sort
from [bva_13_Total]
union all select [Deltagard_Total].Expr1, 6 as sort
from [Deltagard_Total]
union all select [Envion_Total].Expr1, 7 as sort
from [Envion_Total]
union all select [Scourge_18-54_Total].Expr1, 8 as sort
from [Scourge_18-54_Total]
union all select [Zenivex_E20_Total].Expr1, 9 as sort
from [Zenivex_E20_Total]
) as u
order by u.sort
I'll do my best to summarize what I am having trouble with. I never used much SQL until recently.
Currently I am using SQL Server 2012 at work and have been tasked with trying to find oddities in SQL tables. Specifically, the tables contain similar information regarding servers. Kind of meta, I know. So they each share a column called "DB_NAME". After that, there are no similar columns. So I need to compare Table A and Table B and produce a list of records (servers) where a server is NOT listed in BOTH Table A and B. Additionally, this query is being ran against an exception list. I'm not 100% sure of the logic to best handle this. And while I would love to get something "extremely efficient", I am more-so looking at something that just plain works at the time being.
SELECT *
FROM (SELECT
UPPER(ta.DB_NAME) AS [DB_Name]
FROM
[CMS].[dbo].[TABLE_A] AS ta
UNION
SELECT
UPPER(tb.DB_NAME) AS [DB_Name]
FROM
[CMS].[dbo].[TABLE_B] as tb
) AS SQLresults
WHERE NOT EXISTS (
SELECT *
FROM
[CMS].[dbo].[TABLE_C_EXCEPTIONS] as tc
WHERE
SQLresults.[DB_Name] = tc.DB_NAME)
ORDER BY SQLresults.[DB_Name]
One method uses union all and aggregation:
select ab.*
from ((select upper(name) as name, 'A' as which
from CMS.dbo.TABLE_A
) union all
(select upper(name), 'B' as which
from CMS.dbo.TABLE_B
)
) ab
where not exists (select 1
from CMS.dbo.TABLE_C_EXCEPTION e
where upper(e.name) = ab.name
)
having count(distinct which) <> 2;
SQL Server is case-insensitive by default. I left the upper()s in the query in case your installation is case sensitive.
Here is another option using EXCEPT. I added a group by in each half of the union because it was not clear in your original post if DB_NAME is unique in your tables.
select DatabaseName
from
(
SELECT UPPER(ta.DB_NAME) AS DatabaseName
FROM [CMS].[dbo].[TABLE_A] AS ta
GROUP BY UPPER(ta.DB_NAME)
UNION ALL
SELECT UPPER(tb.DB_NAME) AS DatabaseName
FROM [CMS].[dbo].[TABLE_B] as tb
GROUP BY UPPER(tb.DB_NAME)
) x
group by DatabaseName
having count(*) < 2
EXCEPT
(
select DN_Name
from CMS.dbo.TABLE_C_EXCEPTION
)
Currently I have UNION's between 3 different select statements giving me three rows. What I'm needing is to modify this to have the union inside the from clause so that I can generate more columns (if I understand the functionality).
Basically what is going on is that the existing framework is designed and built to have a single row of data returned to it and will require heavy modification to handle a multi-row result set (everything is getting parsed to xml before being passed to the front-end).
My biggest issue (I believe) is being able to differentiate between the three sub selects inside the primary.
Guarantees in the select
1) Each select inside the from will only produce a single row result set.
2) All result sets from inside of the from will have same column count and column names (inherent of union I believe)
For example...
I currently have
SELECT * FROM A
UNION
SELECT * FROM B
UNION
SELECT * FROM C
Doing it this way produces a three row result set.
What I'm wanting if possible is....
SELECT cost as CurrentSelectedCost, /* from first select */
cost as PreviousCost, /* from second select */
cost as NextCost /* from third select */
FROM (
SELECT * FROM A
UNION
SELECT * FROM B
UNION
SELECT * FROM C
)
Now I'm guessing that I will need to alias the different select statements that are within the from clause, but I'm having issues getting that to function. The examples that I've found on here didn't seem to address the need to have all select statements inside of from differentiated. If this has been answered on here a link will suffice no reason to re-invent the wheel (I may just not know the terminology to search for) Also the database is a DB2 instance running on an iSeries
You need to unify the columns retrieved from the three SELECT statements.
SELECT CurrentSelectedCost,
PreviousCost,
NextCost
FROM (
SELECT cost as CurrentSelectedCost, 0 as PreviousCost, 0 as NextCost FROM A
UNION
SELECT 0 as CurrentSelectedCost, cost as PreviousCost, 0 as NextCost FROM B
UNION
SELECT 0 as CurrentSelectedCost, 0 as PreviousCost, cost as NextCost FROM C
) as COSTS
SELECT (SELECT COLNAME FROM A) CurrentSelectedCost,
(SELECT COLNAME FROM B) PreviousCost,
(SELECT COLNAME FROM C) NextCost
FROM DUAL
i dont think you need a union. there does not appear to be any join condition so maybe this will do it:
SELECT A.cost as CurrentSelectedCost, /* from first select */
B.cost as PreviousCost, /* from second select */
C.cost as NextCost /* from third select */
FROM A,B,C