大查询DML插入 - 选择重复表

时间:2016-11-01 01:42:43

标签: google-bigquery

我有一个表结构(表名:Recalled_transaction),如下所示:

TypedArray handle = res.obtainTypedArray(id); 
String engName = handle.getString(0); 
String sanName = handle.getString(1);
Drawable pic = handle.getDrawable(2);
String description = handle.getString(3);
String steps = handle.getString(4);

我想使用insert-select as(标准sql中的DML)从一组常规表插入到此结构中。有人做过吗任何帮助表示赞赏。

由于

1 个答案:

答案 0 :(得分:4)

我创建了一个具有相同模式的表,并将一个示例查询放在一起以插入其中。在您的特定情况下,由于您有两个表,因此您可能需要JOIN个表,然后使用GROUP BY

INSERT mydataset.SampleDmlTable
  (STR_NBR, RGSTR_NBR, POS_TRANS_ID, SLS_DT, TRANS_ORIG_SRC, RECALLED_TXN)
WITH T AS (
  SELECT CAST(x AS STRING) AS STR_NBR,
    10 - x AS RGSTR_NBR,
    x AS POS_TRANS_ID,
    DATE_SUB(CURRENT_DATE(), INTERVAL x DAY) AS SLS_DT,
    CONCAT('foo_', CAST(x AS STRING)) AS POS_APPL_TYP_CD,
    CAST(x AS STRING) AS USER_ID,
    [CONCAT('bar_', CAST(x AS STRING)), 'baz'] AS POS_SEQ_NBR,
    CAST(10 - x AS STRING) AS SUB_SYS_CD
  FROM UNNEST([1, 1, 0, 3, 2, 2, 2]) AS x
)
SELECT
  STR_NBR,
  RGSTR_NBR,
  POS_TRANS_ID,
  SLS_DT,
  ARRAY_AGG(STRUCT(POS_APPL_TYP_CD, USER_ID)) AS TRANS_ORIG_SRC,
  ARRAY_AGG(STRUCT(POS_SEQ_NBR, SUB_SYS_CD)) AS RECALLED_TXN
FROM T
GROUP BY 1, 2, 3, 4;