BigQuery - reshape table to vector - vector

I have a table with
zip_client zip_supplier zip_store
12345 56432 42374
35424 null 12345
etc
And I need to get overall unique zips.
Is there a simple way to do it within a query?
Currently I download the result into Sheets and do it there.
If there was some kind of reshape in BigQuery it would be very easy, but I could not find how to do it.
Thanks

Below is for BigQuery Legacy SQL
#legacySQL
SELECT
type, zip
FROM
(SELECT 'client' AS type, zip_client AS zip FROM yourTable),
(SELECT 'supplier' AS type, zip_supplier AS zip FROM yourTable),
(SELECT 'store' AS type, zip_store AS zip FROM yourTable)
WHERE NOT zip IS NULL
if you will have chance or decide to migrate to BigQuery Standard SQL - see below
#standardSQL
SELECT zip.type, zip.zip
FROM yourTable,
UNNEST([STRUCT<type STRING, zip INT64>
('client', zip_client),
('supplier', zip_supplier),
('store', zip_store)]) AS zip
WHERE NOT zip.zip IS NULL

Using standard SQL:
SELECT DISTINCT zip
FROM YourTable
CROSS JOIN UNNEST([zip_client, zip_supplier, zip_store]) AS zip;
You can put any number of columns inside the UNNEST depending on the structure of your table.

Related

Does SQLScript for SAP HANA support the use of INSERT with CTEs (Common Table Expressions)?

I know this isn't a specific bit of code or problem, but I am having trouble with a very similar issue to the person asking this (except theirs is for SQL Server): Combining INSERT INTO and WITH/CTE ...and I can't seem to find it out there on any SAP HANA help forums etc. so thought there may be an expert on here who can just give me a simple yes or no answer.
The SQL statement I am using contains multiple CTEs, but when I try to insert it tells me there is a Syntax error around the word INSERT. It is definitely laid out exactly the same as in the question I've linked above (spent hours checking), and I can post code samples if necessary but I simply want to know whether it is supported first! Thanks
Short answer:
No, CTEs are not supported for INSERT/UPDATE statements.
Longer answer:
SQLScript's INSERT/UPDATE commands are actually "borrowed" SQL commands as the documentation explains.
Checking the documentation for SQL INSERT we find that it supports a subquery as a source of values.
The subquery term is defined as part of the SQL SELECT statement. Checking the documentation for SELECT shows that <subquery> and <with_clause> are different, non-overlapping terms.
This means, that CTEs cannot be used in subqueries and therefore not be part of the subqueries used in INSERT/UPDATE commands.
You can, however, use SQLScript table variables in INSERT statements in your SQLScript blocks, which is very similar to CTEs:
DO BEGIN
te_a := SELECT 10, 'xyz' as VAL from dummy;
te_b := SELECT 20, 'abc' as VAL from dummy;
te_all := SELECT * from :te_a
UNION ALL SELECT * from :te_b;
INSERT INTO VALS
(SELECT * from :te_all);
END;
You can convert the CTE into a Sub-Select statement in many cases
You can use following
insert into city (city, countryid, citycode)
select
city, countryid, citycode
from (
-- CTE Expression as subselect
select * from city
-- end (CTE)
) cte
Instead of using following valid CTE command combined with INSERT (on SQL Server)
with cte as (
select * from city
)
insert into city (city, countryid, citycode)
select
city, countryid, citycode
from cte
SAP HANA includes this posibility, the order of the code is different than SQL Server:
INSERT INTO EXAMPLE (ID)
WITH cte1 AS (SELECT 1 AS ID FROM DUMMY)
SELECT ID FROM cte1;

Merge firebase events on bigquery

I'm trying to bring together the events recorded on firebase for android and ios, but when I try to perform an union on the tables, I get:
Error: Duplicate column names in the result are not supported. Found duplicate(s): user_dim, event_dim
My query (standardSQL):
SELECT
*
FROM
`com_myapp_ANDROID.app_events_*`,
`com_myapp_IOS.app_events_*`
I can select the fields independently and create aliases, but then I won't have a real merge why I would like to import into google data studio
Try below
#standardSQL
SELECT * FROM `com_myapp_ANDROID.app_events_*`
UNION ALL
SELECT * FROM `com_myapp_IOS.app_events_*`
LIMIT 11
comma as a UNION works only in BigQuery Legacy SQL - comma is treated as CROSS JOIN in BigQuery Standard SQL
In BigQuery Standard SQL you must use UNION explicitly

Batch filling in lookups in MS Access

I'm copying quite a lot of data from Excel into Access. The trouble is, I have a lookup field and I must select a value from it for each new row. There are about 1000 rows and I wondered if I can somehow fill in those lookups automatically.
Create a linked table to the Excel. Let's call it ExcelLinked.
Create a query with ExcelLinked and your lookup table, let's call it tblLookupItems.
The query will be:
INSERT INTO TargetTable (SELECT ExcelLinked.*, tblLookupItems.ID FROM ExcelLinked INNER JOIN tblLookupItems ON ExcelLinked.LookupItem = tblLookupItems.LookupItem)
However, if there are values in the Excel file that do not exist in the lookup table, you will have to decide whether you are willing to forgo those rows or use a lookup ID of 0 in which cae you should use a LEFT JOIN in the SQL query.

Sqlite: select same colume from many tabls

Let's say we have tables: t1(_id, url), t2(_id ,url), t3(_id, url), ...
I wanna get all url from tables:
select url from t1 union select url from t2 union select url from t3 union ...
It's fine, but too long if I have 3 more tables. So how can I get all url in all table?
If you need data from all tables, you must name all tables somewhere in the FROM clause. SQLite has no way of knowing wich tables you want to SELECT from if you don't name them.

SQL Server Creating Indexes Catalogs with joined tables

So at the moment I am building a Advanced Search in .NET, and getting the results is just proving a bit slow so was looking at creating indexes on the tables.
I.e went to tables and define full text index.
So now I have my catalog with the 5 tables and selected columns.
But I cant see how this catalog actually joins these tables ?
I.e. in my "slow" stored procedure I could have
select *
from table1
inner join table2 ON table1.id = table2.linkedID
etc for other tables ?
and now I guess I can go
select * from catalogName
but how does catalogName know what columns to join for the inner join etc
You don't query the fultext catalog directly, you use the fulltext functions in your query, like CONTAINS, CONTAINSTABLE, FREETEXT and FREETEXTTABLE:
SELECT field, field, field
FROM table
WHERE CONTAINS(field, 'some text');
Full text has nothing to do with joining tables and if your query is slow because you join 5 tables then FT is unlikely to help at all.

Resources