重命名所有列名称以便在大查询

时间:2018-04-20 13:34:56

标签: sql google-bigquery

我需要连接两个具有完全相同列名的表。我需要在加入步骤之前重命名列。 每个表包含100多列。我想知道是否有任何方法可以添加前缀或后缀来重命名所有列,而不是使用AS手动更改它们。我在BigQuery上使用标准SQL。我在下面举了一个例子来说明。我想知道BigQuery中是否有任何函数:

UPDATE
  inv
SET
  CONCAT(column_name, '_inv') AS column_name

......例子......提前谢谢你!

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 个答案:

答案 0 :(得分:4)

而不是 SELECT inv.*, prof.*显然最终使用Duplicate column names in the result are not supported. ... 使用 SELECT inv, prof,如下所示

   
#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)

结果:

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     

正如您所看到的那样,结果行现在有两个结构/记录 - 每个结构/记录都包含相应表格的相应条目

我认为这是你出于所有实际原因的最佳选择