Is there a SQL function to expand table?

后端 未结 4 706
面向向阳花
面向向阳花 2021-01-24 21:48

I vaguely remember there being a function that does this, but I think I may be going crazy.

Say I have a datatable, call it table1. It has three columns: column

4条回答
  •  臣服心动
    2021-01-24 22:16

    Below is for BigQuery Standard SQL

    I think below is close enough to what "got you crazy" o)

    #standardSQL
    SELECT copy.*
    FROM `project.dataset.tabel1` t, UNNEST(FN.EXPAND(t, 3)) copy
    

    To be able to do so, you can leverage recently announced support for persistent standard SQL UDFs, namely - you need to create FN.EXPAND() function as in below example (note: you need to have FN dataset in your project - or use existing dataset in which case you should use YOUR_DATASET.EXPAND() reference

    #standardSQL
    CREATE FUNCTION FN.EXPAND(s ANY TYPE, dups INT64) AS ( 
      ARRAY (
      SELECT s FROM UNNEST(GENERATE_ARRAY(1, dups)) 
      )
    ); 
    

    Finally, if you don't want to create persistent UDF - you can use temp UDF as in below example

    #standardSQL
    CREATE TEMP FUNCTION EXPAND(s ANY TYPE, dups INT64) AS ( ARRAY(
      SELECT s FROM UNNEST(GENERATE_ARRAY(1, dups)) 
    )); 
    SELECT copy.*
    FROM `project.dataset.tabel1` t, UNNEST(EXPAND(t, 3)) copy
    

提交回复
热议问题