问题
I need to join two tables with exactly the same column names. I will need to rename the columns before the joining step. Each table contains 100+ columns. I wonder if there's any way to add prefix or suffix to rename all the columns, rather than manually change them with AS
. I'm using standard SQL on BigQuery. I put an example in below for illustration. I wonder if there's any function in BigQuery like:
UPDATE
inv
SET
CONCAT(column_name, '_inv') AS column_name
... example ... thank you in advance!
WITH
inv AS (
SELECT
'001' AS company,
'abc' AS vendor,
800.00 AS transaction,
'inv' AS type
UNION ALL
SELECT
'002' AS company,
'efg' AS vendor,
23.4 AS transaction,
'inv' AS type ),
prof AS (
SELECT
'001' AS company,
'abc' AS vendor,
800.00 AS transaction,
'prof' AS type
UNION ALL
SELECT
'002' AS company,
'efg' AS vendor,
23.4 AS transaction,
'prof' AS type )
SELECT
inv.*,
prof.*
FROM
inv
FULL JOIN
prof
USING
(company,
vendor,
transaction)
回答1:
Instead of SELECT inv.*, prof.*
which obviously ends up with Duplicate column names in the result are not supported. ...
use SELECT inv, prof
as shown below
#standardSQL
WITH inv AS (
SELECT'001' AS company,'abc' AS vendor,800.00 AS transaction,'inv' AS type UNION ALL
SELECT'002' AS company,'efg' AS vendor,23.4 AS transaction,'inv' AS type
), prof AS (
SELECT'001' AS company,'abc' AS vendor,800.00 AS transaction,'prof' AS type UNION ALL
SELECT'002' AS company,'efg' AS vendor,23.4 AS transaction,'prof' AS type
)
SELECT inv, prof
FROM inv FULL JOIN prof
USING (company, vendor, transaction)
result :
Row inv.company inv.vendor inv.transaction inv.type prof.company prof.vendor prof.transaction prof.type
1 001 abc 800.0 inv 001 abc 800.0 prof
2 002 efg 23.4 inv 002 efg 23.4 prof
As you can see resulted row now has two structs/records - each holding respective entry from respective table
I think this is your best option here from all practical reasons
来源:https://stackoverflow.com/questions/49942891/rename-all-column-names-for-joining-purpose-in-big-query