BigQuery external queries with union all - google-bigquery

I want to show data in BigQuery from two external queries
I'm trying to use UNION ALL, but I'm getting the data as join
This is what I'm trying to do:
SELECT *
FROM (
EXTERNAL_QUERY("connection_path1", "query1")
UNION ALL
EXTERNAL_QUERY("connection_pat2", "query2")
)
I'm getting this error:
Syntax error: Expected keyword JOIN but got keyword UNION at
How can I achieve UNION ALL in big-query?

Use below instead
SELECT * FROM EXTERNAL_QUERY("connection_path1", "query1")
UNION ALL
SELECT * FROM EXTERNAL_QUERY("connection_pat2", "query2")

Related

BQ function to transform caron character from a string to character without caron

I am trying to get rid of a caron characters (č,ć,š,ž) and transform them to c,c,s,z :
with my_table as (
select 'abcčd sštuv' caron, 'abccd sstuv' no_caron union all
select 'uvzž', 'uvzz' union all
select 'ijkl cČd', 'ijkl cCd' union ALL
select 'Ćdef', 'Cdef'
)
SELECT *
FROM my_table
I was trying to get rid of them with
SELECT *,
regexp_replace(caron, r'š', 's') as no_caron
FROM my_table
but I think this is inefficient. I know there is an option to write your on function as described
here, but I have no idea how to use it in my case.
Thanks in advance!
Use below
SELECT *, regexp_replace(normalize(caron, NFD), r"\pM", '') output
FROM my_table
if applied to sample data in your question - output is

BigQuery , Union issue / Error: Syntax error: Expected keyword ALL or keyword DISTINCT but got keyword SELECT at [16:3]

Currently, I'm trying to connect 4 tables together and display only 5 columns and union them all to one table.
This is the query:
SELECT
id,
platform,
url,
profileImageUrl,
name
FROM (
SELECT
f.id AS id,
f.platform AS platform,
f.url AS url,
f.profileImageUrl AS profileImageUrl,
f.name AS name
FROM
`l.p.link_f_main` AS f UNION
SELECT
i.id AS id,
i.platform AS platform,
i.url AS url,
i.profilePicture AS profileImageUrl,
i.fullName AS name
FROM
`l.p.link_i_main` AS i UNION
SELECT
t.id AS id,
t.platform AS platform,
t.url AS url,
t.profileImageUrl AS profileImageUrl,
t.name AS name
FROM
`l.p.link_t_main` AS t UNION
SELECT
y.id AS id,
y.platform AS platform,
y.url AS url,
y.profileImageUrl AS profileImageUrl,
y.name AS name
FROM
`l.p.link_y_main` AS y ) as main
This is the error:
Error: Syntax error: Expected keyword ALL or keyword DISTINCT but got keyword SELECT at [16:3]
What am I doing incorrectly?
Answer:
1) For Big Query have to use UNION ALL instead of UNION
2) Datatypes for all union columns should be the same.
-
I had an issue that one on the columns was STRING - INT64, INT64,
INT64, STRING
So I change the rest columns to STRING as well, to make it work.
From documentation: UNION { ALL | DISTINCT }. So you have to use either UNION ALL or UNION DISTINCT (which is exactly what the error message says). As per HoneyBadger user mentioned this is the correct explanation.

How to define temporary table values in a subquery in Oracle

I am approaching this issue from a non DBA perspective, as in I do not have permissions to create new tables for the database. I am trying to work around this by using a subquery in Oracle kind of like this sudo code:
With temptable as ('col1name', 'col2name', 1,'a',2,'b')
Select * from temptable where col1name = 1
With the temptable looking like
Col1name | Col2name
1 a
2 b
And the output being row 1. I know it is not the easiest way to do it, but it is all I can think of to accomplish my task until I can get the admin to approve a new table. I have searched a lot but I can't find an answer. Is there a simple way to define temporary table data like this?
I would just do this as:
with temptable as (
select 1 as col1name, 'a' col2name from dual union all
select 2, 'b' from dual
)
Select *
from temptable
where col1name = 1;
As an alternative to a CTE (common table expresssion) as suggested by Gordon, you can also use a query as an old-school inline view.
For example:
SELECT tt.col1name
, tt.col2name
FROM ( SELECT 1 AS col1name, 'a' AS col2name FROM DUAL
UNION ALL SELECT 2, 'b' FROM DUAL
UNION ALL SELECT 3, 'c' FROM DUAL
) tt
WHERE tt.col1name = 1
ORDER
BY tt.col1name

sql select on multiple db's

I got around 18 db's. All these db's have the same structure. I want to query all these db's once to get my results.
Example:
ShopA
ShopB
ShopC
These db's got all the table article (and also the same rows).
How do I get all articles in one result with a WHERE?
I thought:
select *
from shopa.dbo.article
shopb.dbo.article
shopc.dbo.article
where color = 'red'
Did someone got an idea?
Have you considered doing a UNION ALL?
So something like:
SELECT 'a' AS Shop, *
FROM shopa.dbo.article
WHERE color = 'red'
UNION ALL
SELECT 'b' AS Shop, *
FROM shopb.dbo.article
WHERE color = 'red'
UNION ALL
SELECT 'c' AS Shop, *
FROM shopc.dbo.article
WHERE color = 'red'
Or, with a CTE (if you RDBMS supports it)
;WITH allstores AS (
SELECT 'a' AS Shop, *
FROM shopa.dbo.article
UNION ALL
SELECT 'b' AS Shop, *
FROM shopb.dbo.article
UNION ALL
SELECT 'c' AS Shop, *
FROM shopc.dbo.article
)
SELECT *
FROM allstores
WHERE color = 'red'
you could use UNION
if you can simply select the db names you could also use a cursor select with OPENQUERY on a dynamically created string insert into a temp table and select from that
You can create a View wich is populated from your select as this:
CREATE VIEW view_name AS
SELECT * FROM shopa.dbo.article
UNION
SELECT * FROM shopb.dbo.article
UNION
SELECT * FROM shopc.dbo.article
Then you can try to run a query by the View
Select * from view_name
where color = 'red'
Then if you want write another query with another condition, you don't write another big query with union or other code.
You can just write a query on a VIEW

Using UNION with Sequel

I want to define a SQL-command like this:
SELECT * FROM WOMAN
UNION
SELECT * FROM MEN
I tried to define this with the following code sequence in Ruby + Sequel:
require 'sequel'
DB = Sequel::Database.new()
sel = DB[:women].union(DB[:men])
puts sel.sql
The result is (I made some pretty print on the result):
SELECT * FROM (
SELECT * FROM `women`
UNION
SELECT * FROM `men`
) AS 't1'
There is an additional (superfluous?) SELECT.
If I define multiple UNION like in this code sample
sel = DB[:women].union(DB[:men]).union(DB[:girls]).union(DB[:boys])
puts sel.sql
I get more superfluous SELECTs.
SELECT * FROM (
SELECT * FROM (
SELECT * FROM (
SELECT * FROM `women`
UNION
SELECT * FROM `men`
) AS 't1'
UNION
SELECT * FROM `girls`
) AS 't1'
UNION
SELECT * FROM `boys`
) AS 't1'
I detected no problem with it up to now, the results seem to be the same.
My questions:
Is there a reason for the additional selects (beside sequel internal procedures)
Can I avoid the selects?
Can I get problems with this additional selects? (Any Performance issue?)
The reason for the extra SELECTs is so code like DB[:girls].union(DB[:boys]).where(:some_column=>1) operates properly. You can use DB[:girls].union(DB[:boys], :from_self=>false) to not wrap it in the extra SELECTs, as mentioned in the documentation.