2016-08-19 2 views
2

나는 하나의 테이블처럼이의 BigQuery 쿼리 회전 테이블 주위

UserId   email   weight  
User1  [email protected]  55 
User2  [email protected] 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 

더 많은 필드를 얻으려고하면 쿼리가 작동하지 않거나 오랜 시간이 걸립니다.

답변

1

SELECT 
    UserId, 
    MAX(IF(user_properties.user_properties_key="email", user_properties.user_properties_value, NULL)) AS email, 
    MAX(IF(user_properties.user_properties_key="weight", user_properties.user_properties_value, NULL)) AS weight 
FROM [YourTable] 
GROUP BY UserId 

또는

그것은 당신의 질문에서 명확하지 않다
SELECT 
    UserId, 
    MAX(IF(user_properties.user_properties_key="email", user_properties.user_properties_value, NULL)) WITHIN RECORD AS email, 
    MAX(IF(user_properties.user_properties_key="weight", user_properties.user_properties_value, NULL)) WITHIN RECORD AS weight 
FROM [YourTable] 

아래 시도, 그래서 테이블도

enter image description here

참조 Pivot Repeated fields in BigQuery

다음과 같다 가정
+0

감사합니다. 그것은 작동합니다! =)하지만 한 가지 문제가 있습니다. 한 사용자가 여러 값을 가질 수 있습니다 (결과 '필드'weight '옵션'(32) '또는 그와 유사한 방식으로 결과를 얻는 방법과이 문제의 최종 값을 얻을 수있는 방법). – KeylorNavas

+0

구체적인 예를 당신은 그렇지 않다는 질문에 충분히 불투명합니다. –

+0

데이터를 지속적으로 업데이트합니다. 마지막으로 기록 된 값을 얻으려면 어떻게해야합니까? – KeylorNavas

0

이것은 일반적으로 피벗 테이블 작업이라고합니다. 이전 질문 및 대답은 예제를 제공합니다 : How to simulate a pivot table with BigQuery?

을 사용하는 경우, 예제의 일부 기능과 구문을 변경해야하지만 원칙은 동일합니다.