How to transpose rows to columns with large amount of the data in BigQuery/SQL?

后端 未结 2 666
别跟我提以往
别跟我提以往 2020-11-30 15:23

I have a problem in transposing a large amount of data table in BigQuery (1.5 billion rows) from rows to columns. I could figure out how to do it with small amount of data w

相关标签:
2条回答
  • 2020-11-30 15:48
    STEP #1
    

    In below query replace yourTable with real name of your table and execute/run it

    SELECT 'SELECT CustomerID, ' + 
       GROUP_CONCAT_UNQUOTED(
          'MAX(IF(Feature = "' + STRING(Feature) + '", Value, NULL))'
       ) 
       + ' FROM yourTable GROUP BY CustomerID'
    FROM (SELECT Feature FROM yourTable GROUP BY Feature) 
    

    As a result you will get some string to be used in next step!

    STEP #2
    

    Take string you got from Step 1 and just execute it as a query
    The output is a Pivot you asked in question

    0 讨论(0)
  • 2020-11-30 16:12

    Hi @Jade I posted a very similar question before. And got a very helpful (and similar) answer from @MikhailBerlyant. For what it's worth, I had about 4000 features to dummify in my case and also ran into "Resources exceeded during query execution" error.

    I think that this type of large-scale data transformation (rather than query) is better left for other tools more suitable for this task (such as Spark).

    0 讨论(0)
提交回复
热议问题