2016-08-11 1 views
1

사용자가 Google BigQuery의 단일 데이터 표에서 TV 프로그램을 시청하는 시작 시간과 종료 시간을 일치 시키려고하지만 '테이블 이름을 사용할 수 없습니다.'라는 오류 메시지가 계속 표시되기 때문에이를 수행하는 방법을 모르겠습니다. 데이터 세트 이름이 누락되었습니다. "동일한 표에서 가장 가까운 매칭 시간을 찾으려면 어떻게합니까?

이벤트 테이블

user_id show_id event_type logtime 
------- -------- ---------- ----------------------- 
john  123  start  2016-08-01 06:00:00 UTC 
john  123  start  2016-08-01 06:15:00 UTC 
john  123  end  2016-08-01 06:10:00 UTC 
john  123  end  2016-08-01 06:16:00 UTC 

원하는 결과

user_id show_id start_time    end_time 
------- -------- ----------------------- ----------------------- 
john  123  2016-08-01 06:00:00 UTC 2016-08-01 06:10:00 UTC 
john  123  2016-08-01 06:15:00 UTC 2016-08-01 06:16:00 UTC 

이 지금까지 내 쿼리입니다 :

SELECT user_id, show_id, st.logtime AS start_time, et.logtime AS end_time 
    FROM 
    (SELECT user_id, show_id, logtime FROM events WHERE event_type = 'start') AS st 
    JOIN 
    (SELECT user_id, show_id, logtime FROM events WHERE event_type = 'end') AS et 
    ON 
    st.logtime = (SELECT min(logtime) FROM events WHERE event_type = 'end') 
     AND st.user_id = et.user_id AND st.show_id = et.show_id 
미하일의 대답은 몇 가지 예를 검증 한 후 최선을 작동하는 것 같다

하지만, ...

SELECT 
    user_id, show_id, 
    logtime AS start_time, 
    next_logtime AS end_time 
FROM (
    SELECT 
    user_id, show_id, event_type, logtime, 
    LEAD(logtime) OVER(PARTITION BY user_id, show_id ORDER BY logtime) AS next_logtime, 
    LEAD(event_type) OVER(PARTITION BY user_id, show_id ORDER BY logtime) AS next_event_type 
    FROM events 
) 
WHERE event_type = 'start' 
AND next_event_type = 'end' 

동일한 event_type의 연속 인스턴스를 처리하는 논리를 통합하는 방법을 모르겠습니다. 예를 들어 :

event_type logtime 
---------- ------------------------ 
start  2016-08-01 09:20:00 UTC 
start  2016-08-01 09:23:00 UTC 
start  2016-08-01 09:24:00 UTC 
end  2016-08-01 09:24:00 UTC 
end  2016-08-01 09:24:00 UTC 

이 시나리오에서는, 나는 가장 빠른 시작 시간, 9시 20분 및 초기 종료 시간을 유지하고 싶습니다 9시 24분는 (I ...이 말이 생각).

+0

오류 메시지에 중점을 둡니다. 이것을 시도하면 어떻게됩니까? 'select count (1) x 1 = 2' 이벤트에서 선택 하시겠습니까? –

+0

이 쿼리를 말 그대로 실행해야합니까? 결과적으로 "0"을 얻었습니다. – dnaeye

+0

예.그것은 문제가'events'라는 테이블이 아니라는 것을 당신에게 빨리 말했습니다. 오류의 원인을 찾을 때까지 하위 쿼리로이 접근 방식을 계속하십시오. –

답변

1

SELECT 
    user_id, show_id, 
    logtime AS start_time, 
    next_logtime AS end_time 
FROM (
    SELECT 
    user_id, show_id, event_type, logtime, 
    LEAD(logtime) OVER(PARTITION BY user_id, show_id ORDER BY logtime) AS next_logtime 
    FROM events 
) 
WHERE event_type = 'start' 
불행하게도

아래 시도, 데이터 꽤 더러운, 그래서 예 시작을 무시 아래 시작 시간하지만 종료 시간과 그 반대의 경우도 마찬가지

이있을 수 있습니다 이벤트가있다 끝이 없으면 그 반대가 될 수 있습니다.
은 사용자가 염두에두고있는 논리에 맞게 조정할 수 있습니다.

나는이 두 선택 쿼리의 카티 제품을 만들 것입니다

SELECT 
    user_id, show_id, 
    MIN(start_time) AS start_time, 
    MAX(end_time) AS end_time 
FROM (
    SELECT 
    user_id, show_id, 
    logtime AS start_time, 
    next_logtime AS end_time, 
    SUM(event_type <> next_event_type) OVER(PARTITION BY user_id, show_id ORDER BY logtime ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) AS grp 
    FROM (
    SELECT 
     user_id, show_id, event_type, logtime, 
     LEAD(logtime) OVER(PARTITION BY user_id, show_id ORDER BY logtime) AS next_logtime, 
     LEAD(event_type) OVER(PARTITION BY user_id, show_id ORDER BY logtime) AS next_event_type, 
    FROM events 
) 
    WHERE event_type = 'start' 
) 
GROUP BY user_id, show_id, grp 
+0

event_type이 "end"인 log_imes에 end_time을 할당하는 방법이 표시되지 않습니다. – dnaeye

+0

각 시작 이벤트에 해당 종료 이벤트가 있다고 가정합니다! 귀하의 단순화 된 예에서와 마찬가지입니다. 그렇지 않은 경우 - 그러한 경우를 다루는 방법의 논리를 정의해야합니다. 두 번의 연속 시작이있을 때해야할 일 –

+0

누락 된 시작/끝 쌍을 처리하는 또 다른 예제가 추가되었습니다 –

0

데이터 정말 라인까지, 당신은 시작 시간과 종료 시간을 열거 할 수 및 통합을 위해 그것을 사용하는 경우 :

select user_id, show_id, , 
     max(case when event_type = 'start' then logtime end) as logtime_start, 
     max(case when event_type = 'end' then logtime end) as logtime_end 
from (select e.*, 
      row_number() over (partition by user_id, show_id, event_type orer by logtime) as seqnum 
     from events e 
    ) e 
group by user_id, show_id, seqnum; 

이이 문제의 데이터를 사용할 수 있습니다. 이벤트가 제대로 페어링되면 잘 작동합니다.

+0

불행히도, 데이터가 꽤 더럽습니다. 따라서 시작 시간은 있지만 종료 시간은없는 이벤트가 있고 그 반대도 마찬가지입니다. – dnaeye

0
SELECT user_id, show_id, st.logtime AS start_time, MIN(et.logtime) AS end_time 
FROM 
(SELECT user_id, show_id, time AS logtime FROM events WHERE event_type = 'start') AS st , 
(SELECT user_id, show_id, time AS logtime FROM events WHERE event_type = 'end') AS et 
WHERE st.logtime < et.logtime, st.user_id = et.user_id, st.show_id = et.show_id 
GROUP BY st.logtime 
  1. 가장 빠른 시작 시간, 9시 20분 및 초기 종료 시간을 유지하고 싶습니다 은 logtime을 시작하고 logtime을 종료합니다.
  2. 값을 필터링하십시오. 여기서 시작 로그 시간 <은 logtime을 종료합니다.
  3. 그룹 시작 행 logtime과 종료 logtime이 동일한 그룹 행.
+0

BigQuery에서 작동하는 방식이 아닙니다. –

관련 문제