Bigquery查询转表

时间:2016-08-19 13:02:00

标签: sql google-bigquery

我有这样的单桌:

UserID    user_properties_key    user_properties_value(String) 
User1      email                     user_email@gmail.com 
           weight                      55   

我希望得到这样的结果:

UserId           email           weight    
User1     user_email@gmail.com     55 
User2     user2_email@email.com    155

我当前的查询显示:

SELECT
  T1.UserId as UserId,
  T1.user_properties_value AS email,
  T2.user_properties_value AS weight,
FROM (FLATTEN([database20160814], user_properties_key )) AS T1
JOIN 
(FLATTEN([database20160814], user_properties_key )) AS T2
ON
  T1.userId = T2.userId
WHERE
  T1.user_properties_key="email"
  AND T2.user_properties_key="weight"
GROUP BY
  V0,
  V1,
  V2

如果我想要获取更多字段,查询不起作用或需要很长时间

2 个答案:

答案 0 :(得分:1)

尝试以下

def backward_list(n):
    numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    if n == 0 or n == 1:
        x = numbers.index(n) 
    else:
        x = (numbers.index(n)-10)
    return [numbers[x-2],numbers[x-1],numbers[x],numbers[x+1],numbers[x+2]]

OR

In [1]: for i in range(10):
.....:     print backward_list(i)
.....:     
[8, 9, 0, 1, 2]
[9, 0, 1, 2, 3]
[0, 1, 2, 3, 4]
[1, 2, 3, 4, 5]
[2, 3, 4, 5, 6]
[3, 4, 5, 6, 7]
[4, 5, 6, 7, 8]
[5, 6, 7, 8, 9]
[6, 7, 8, 9, 0]
[7, 8, 9, 0, 1]

您的问题并不清楚,所以我假设您的表格如下

enter image description here

另见Pivot Repeated fields in BigQuery

答案 1 :(得分:0)

这通常称为数据透视表操作。之前的问题和答案举例说明:How to simulate a pivot table with BigQuery?

如果您使用standard SQL,则需要更改示例中的某些函数和语法,但原理是相同的。