How to UNION ALL tables with tablename satisfying condition in BigQuery? - sql

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

Find ID's not match a list

I cannot say I am so experienced with SQL. Here is my question.
A table TripEvent have millions of rows. It contains a column Bold_ID that is indexed and unique.
So I can have this query
select bold_id from TripEvent where bold_id in (354469477, 354469536, 354469500, 987359)
Result is
354469477
354469536
354469500
as those exists. But I want to reverse it. How can I get a list if id's that don't exists ?
In this case it should return one row
987359
I cannot use NOT in query as that would return all rows in table not match my list.
One way is like this:
SELECT DS.*
FROM
(
VALUES (354469477)
,(354469536)
,(354469500)
,(987359)
) DS (bold_id)
LEFT JOIN TripEvent TE
ON DS.[bold_id] = TE.[bold_id]
WHERE TE.[bold_id] IS NULL;
Of course, there are diff ways to populated the DS. I will recommend to populate the search IDs in a temporary table.
Since you are using a SQL Server DB, you can use EXCEPT:
SELECT bold_id FROM
(
SELECT 354469477 AS bold_id
UNION ALL
SELECT 354469536
UNION ALL
SELECT 354469500
UNION ALL
SELECT 987359
) listofValues
EXCEPT
SELECT bold_id
FROM TripEvent;
OR:
SELECT bold_id FROM
(
VALUES (354469477),
(354469536),
(354469500),
(987359)
) listofValues(bold_id)
EXCEPT
SELECT bold_id
FROM TripEvent;
Have a look in the documentation
Found this query on another places. Seems to work. Thanks for the good response :)
SELECT *
from (values (354469477),(354469536),(354469500),(987359)) as v(id)
WHERE NOT EXISTS (SELECT BOLD_ID FROM TripEvent WHERE TripEvent.BOLD_ID = v.id)

Best way to UNION ALL several tables [duplicate]

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*`

Oracle SQL multiple aliases to one

I'm new in SQL and I have to write script in Oracle SQL.
I made everything except this:
(SELECT sai.TXPTXT FROM ARTIKELBESCHREIBUNG_KATEGORIE ak, SVZARBKATEGORIE_INHALT sai WHERE ROWNUM=1 AND(ak.KATEGORIE=25 AND ak.KATEGORIE_INHALT=sai.TXPINH AND ak.QUAL=dna.QUAL AND FIRMA=20
OR ak.KATEGORIE=26 AND ak.KATEGORIE_INHALT=sai.TXPINH AND ak.QUAL=dna.QUAL AND FIRMA=20)) as **Fit1,**
(SELECT sai.TXPTXT FROM ARTIKELBESCHREIBUNG_KATEGORIE ak, SVZARBKATEGORIE_INHALT sai WHERE ak.KATEGORIE=27 AND ak.KATEGORIE_INHALT=sai.TXPINH AND ak.QUAL=dna.QUAL AND FIRMA=20) as **rise,**
I have to create a new alias with Name PRODUCT which have values from Fit1,rise and 3 more values from another alias (this alias have Name ID was created with CASE Statement).
PRODUCT=Fit1,rise,ID - this is what i need
How can I do that ?
UNION ALL operation would merge the results horizontally
(SELECT txptxt FROM Fit1
UNION ALL SELECT * FROM rise
UNION ALL SELECT * FROM id) as PRODUCT
SELECT table.* would merge the result vertically
(SELECT Fit1.*, rise.*, id.*
FROM Fit1
JOIN rise USING(TXPTXT)
JOIN id USING(TXPTXT)) as PRODUCT

SQL Logic: Finding Non-Duplicates with Similar Rows

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
)

Transforming union to single select with the unions inside from clause

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