BigQuery select data within a time interval

前端 未结 2 489
余生分开走
余生分开走 2021-01-24 21:34

my data looks like

name| From | To_City | Date of request

Andy| Paris | London| 08/21/2014 12:00

Lena | Koln | Berlin | 08/22/2014 18:00

<
2条回答
  •  别那么骄傲
    2021-01-24 22:27

    One way to do it is to use window functions with the RANGE window. In order to do that, first dates need to be converted to days because RANGE requires the sorting column to be sequential numbers. PARTITION BY clause is similar to GROUP BY - it lists the columns that define "identical" drive requests (in your case - name, from and to). Then you can simply use COUNT(*) to count number of days within such window.

    select name, f, to, date, count(*) 
      over(partition by name, f, to
           order by day
           range between 1 preceding and 1 following) from (
    select name, f, to, date, integer(timestamp(date)/1000000/60/60/24) day from
    (select 'Andy' as name,'Paris' as f,'London' as to, '2014-08-21 12:00' as date),
    (select 'Lena' as name,'Koln' as f,'Berlin' as to, '2014-08-22 18:00' as date),
    (select 'Andy' as name,'Paris' as f,'London' as to, '2014-08-22 06:00' as date),
    (select 'Lisa' as name,'Rome' as f,'Neapel' as to, '2014-08-25 18:00' as date),
    (select 'Lena' as name,'Rome' as f,'London' as to, '2014-08-21 20:00' as date),
    (select 'Lisa' as name,'Rome' as f,'Neapel' as to, '2014-08-24 18:00' as date),
    (select 'Andy' as name,'Paris' as f,'London' as to, '2014-08-25 12:00' as date))
    

提交回复
热议问题