Is there a SQL function to expand table?

后端 未结 4 701
面向向阳花
面向向阳花 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:27

    In BigQuery, I would recommend a CROSS JOIN:

    SELECT t1.*
    FROM table1 CROSS JOIN
         (SELECT 1 as n UNION ALL SELECT 2 UNION ALL SELECT 3) n;
    

    This can get cumbersome for lots of copies, but you can simplify this by generating the numbers:

    SELECT t1.*
    FROM table1 CROSS JOIN
         UNNEST(GENERATE_ARRAY(1, 3)) n
    

    This creates an array with three elements and unnests it into rows.

    In both these cases, you can include n in the SELECT to distinguish the copies.

提交回复
热议问题