BigQuery, заполните пропущенное значение предыдущим непустым значением - PullRequest
0 голосов
/ 29 февраля 2020

Я ищу запрос, который заполнит нули в столбце "rownum" предыдущим непустым значением.

Afterword рассматривает "rownum" как аргумент группировки и вычисляет min и максимальное время на группу.

Первая «настоящая» группа появляется первой в строке 54, и ее время начала / окончания: 2020-02-25T09: 17: 31 - 2020-02-25T09: 27: 32 .

Вот пример данных.

create table activity
select "2020-02-25T00:06:10" as start,  "2020-02-25T00:06:13" as end, 1 as rownum union all 
select "2020-02-25T00:07:32" as start,  "2020-02-25T00:25:54" as end, 2 as rownum union all 
select "2020-02-25T00:27:32" as start,  "2020-02-25T00:36:13" as end, 3 as rownum union all 
select "2020-02-25T00:37:32" as start,  "2020-02-25T00:46:13" as end, 4 as rownum union all 
select "2020-02-25T00:47:33" as start,  "2020-02-25T00:56:13" as end, 5 as rownum union all 
select "2020-02-25T00:57:32" as start,  "2020-02-25T01:06:13" as end, 6 as rownum union all 
select "2020-02-25T01:07:32" as start,  "2020-02-25T01:16:13" as end, 7 as rownum union all 
select "2020-02-25T01:17:32" as start,  "2020-02-25T01:26:13" as end, 8 as rownum union all 
select "2020-02-25T01:27:32" as start,  "2020-02-25T01:36:13" as end, 9 as rownum union all 
select "2020-02-25T01:37:32" as start,  "2020-02-25T01:46:09" as end, 10 as rownum union all 
select "2020-02-25T01:47:32" as start,  "2020-02-25T01:56:05" as end, 11 as rownum union all 
select "2020-02-25T01:57:32" as start,  "2020-02-25T02:06:09" as end, 12 as rownum union all 
select "2020-02-25T02:07:32" as start,  "2020-02-25T02:16:09" as end, 13 as rownum union all 
select "2020-02-25T02:17:32" as start,  "2020-02-25T02:26:13" as end, 14 as rownum union all 
select "2020-02-25T02:27:32" as start,  "2020-02-25T02:36:13" as end, 15 as rownum union all 
select "2020-02-25T02:37:32" as start,  "2020-02-25T02:46:13" as end, 16 as rownum union all 
select "2020-02-25T02:47:32" as start,  "2020-02-25T02:56:05" as end, 17 as rownum union all 
select "2020-02-25T02:57:32" as start,  "2020-02-25T03:06:13" as end, 18 as rownum union all 
select "2020-02-25T03:07:32" as start,  "2020-02-25T03:16:13" as end, 19 as rownum union all 
select "2020-02-25T03:17:32" as start,  "2020-02-25T03:26:13" as end, 20 as rownum union all 
select "2020-02-25T03:27:32" as start,  "2020-02-25T03:45:58" as end, 21 as rownum union all 
select "2020-02-25T03:47:32" as start,  "2020-02-25T03:56:13" as end, 22 as rownum union all 
select "2020-02-25T03:57:32" as start,  "2020-02-25T04:06:13" as end, 23 as rownum union all 
select "2020-02-25T04:07:32" as start,  "2020-02-25T04:16:09" as end, 24 as rownum union all 
select "2020-02-25T04:17:32" as start,  "2020-02-25T04:26:05" as end, 25 as rownum union all 
select "2020-02-25T04:27:32" as start,  "2020-02-25T04:36:13" as end, 26 as rownum union all 
select "2020-02-25T04:37:32" as start,  "2020-02-25T04:46:09" as end, 27 as rownum union all 
select "2020-02-25T04:47:32" as start,  "2020-02-25T04:56:09" as end, 28 as rownum union all 
select "2020-02-25T04:57:32" as start,  "2020-02-25T05:06:09" as end, 29 as rownum union all 
select "2020-02-25T05:07:32" as start,  "2020-02-25T05:16:05" as end, 30 as rownum union all 
select "2020-02-25T05:17:32" as start,  "2020-02-25T05:26:13" as end, 31 as rownum union all 
select "2020-02-25T05:27:32" as start,  "2020-02-25T05:36:13" as end, 32 as rownum union all 
select "2020-02-25T05:37:32" as start,  "2020-02-25T05:46:08" as end, 33 as rownum union all 
select "2020-02-25T05:47:32" as start,  "2020-02-25T05:56:08" as end, 34 as rownum union all 
select "2020-02-25T05:57:32" as start,  "2020-02-25T06:06:04" as end, 35 as rownum union all 
select "2020-02-25T06:07:32" as start,  "2020-02-25T06:16:12" as end, 36 as rownum union all 
select "2020-02-25T06:17:32" as start,  "2020-02-25T06:26:12" as end, 37 as rownum union all 
select "2020-02-25T06:27:32" as start,  "2020-02-25T06:36:12" as end, 38 as rownum union all 
select "2020-02-25T06:37:32" as start,  "2020-02-25T06:55:57" as end, 39 as rownum union all 
select "2020-02-25T06:57:32" as start,  "2020-02-25T07:06:12" as end, 40 as rownum union all 
select "2020-02-25T07:07:32" as start,  "2020-02-25T07:16:08" as end, 41 as rownum union all 
select "2020-02-25T07:17:32" as start,  "2020-02-25T07:26:12" as end, 42 as rownum union all 
select "2020-02-25T07:27:32" as start,  "2020-02-25T07:36:12" as end, 43 as rownum union all 
select "2020-02-25T07:37:31" as start,  "2020-02-25T07:46:12" as end, 44 as rownum union all 
select "2020-02-25T07:47:32" as start,  "2020-02-25T07:56:04" as end, 45 as rownum union all 
select "2020-02-25T07:57:32" as start,  "2020-02-25T08:06:12" as end, 46 as rownum union all 
select "2020-02-25T08:07:31" as start,  "2020-02-25T08:16:08" as end, 47 as rownum union all 
select "2020-02-25T08:17:31" as start,  "2020-02-25T08:26:12" as end, 48 as rownum union all 
select "2020-02-25T08:27:31" as start,  "2020-02-25T08:36:12" as end, 49 as rownum union all 
select "2020-02-25T08:37:31" as start,  "2020-02-25T08:46:12" as end, 50 as rownum union all 
select "2020-02-25T08:47:31" as start,  "2020-02-25T08:56:12" as end, 51 as rownum union all 
select "2020-02-25T08:57:31" as start,  "2020-02-25T09:06:12" as end, 52 as rownum union all 
select "2020-02-25T09:07:31" as start,  "2020-02-25T09:16:08" as end, 53 as rownum union all 
select "2020-02-25T09:17:31" as start,  "2020-02-25T09:26:12" as end, 54 as rownum union all 
select "2020-02-25T09:27:31" as start,  "2020-02-25T09:27:32" as end, 0 as rownum union all 
select "2020-02-25T09:32:41" as start,  "2020-02-25T09:35:57" as end, 56 as rownum union all 
select "2020-02-25T09:37:31" as start,  "2020-02-25T09:49:23" as end, 57 as rownum union all 
select "2020-02-25T09:51:16" as start,  "2020-02-25T09:55:37" as end, 58 as rownum union all 
select "2020-02-25T10:03:46" as start,  "2020-02-25T10:03:47" as end, 0 as rownum union all 
select "2020-02-25T10:05:57" as start,  "2020-02-25T10:05:58" as end, 0 as rownum union all 
select "2020-02-25T10:07:31" as start,  "2020-02-25T10:07:32" as end, 0 as rownum union all 
select "2020-02-25T10:10:22" as start,  "2020-02-25T10:10:23" as end, 0 as rownum union all 
select "2020-02-25T10:12:55" as start,  "2020-02-25T10:12:56" as end, 0 as rownum union all 
select "2020-02-25T10:20:17" as start,  "2020-02-25T10:20:18" as end, 0 as rownum union all 
select "2020-02-25T10:27:40" as start,  "2020-02-25T10:27:41" as end, 0 as rownum union all 
select "2020-02-25T10:39:51" as start,  "2020-02-25T10:43:03" as end, 66 as rownum union all 
select "2020-02-25T10:44:42" as start,  "2020-02-25T10:45:48" as end, 0 as rownum union all 
select "2020-02-25T10:47:31" as start,  "2020-02-25T10:56:12" as end, 68 as rownum union all 
select "2020-02-25T10:57:32" as start,  "2020-02-25T10:58:33" as end, 0 as rownum union all 
select "2020-02-25T11:28:53" as start,  "2020-02-25T11:28:54" as end, 0 as rownum union all 
select "2020-02-25T11:32:48" as start,  "2020-02-25T11:32:49" as end, 0 as rownum union all 
select "2020-02-25T12:02:25" as start,  "2020-02-25T12:02:26" as end, 0 as rownum union all 
select "2020-02-25T12:08:21" as start,  "2020-02-25T12:09:27" as end, 0 as rownum union all 
select "2020-02-25T12:11:11" as start,  "2020-02-25T12:11:12" as end, 0 as rownum union all 
select "2020-02-25T12:12:39" as start,  "2020-02-25T12:13:45" as end, 0 as rownum union all 
select "2020-02-25T12:17:31" as start,  "2020-02-25T12:20:42" as end, 76 as rownum union all 
select "2020-02-25T12:22:29" as start,  "2020-02-25T12:22:30" as end, 0 as rownum union all 
select "2020-02-25T12:23:49" as start,  "2020-02-25T12:23:50" as end, 0 as rownum union all 
select "2020-02-25T12:25:27" as start,  "2020-02-25T12:28:35" as end, 79 as rownum union all 
select "2020-02-25T12:30:47" as start,  "2020-02-25T12:30:48" as end, 0 as rownum union all 
select "2020-02-25T12:32:08" as start,  "2020-02-25T12:32:09" as end, 0 as rownum union all 
select "2020-02-25T12:35:23" as start,  "2020-02-25T12:38:39" as end, 82 as rownum union all 
select "2020-02-25T12:40:12" as start,  "2020-02-25T12:40:13" as end, 0 as rownum union all 
select "2020-02-25T12:42:03" as start,  "2020-02-25T12:42:04" as end, 0 as rownum union all 
select "2020-02-25T12:43:44" as start,  "2020-02-25T12:44:50" as end, 0 as rownum union all 
select "2020-02-25T12:45:56" as start,  "2020-02-25T12:45:57" as end, 0 as rownum union all 
select "2020-02-25T12:50:59" as start,  "2020-02-25T12:51:00" as end, 0 as rownum union all 
select "2020-02-25T12:52:07" as start,  "2020-02-25T12:52:08" as end, 0 as rownum union all 
select "2020-02-25T12:55:20" as start,  "2020-02-25T12:56:22" as end, 0 as rownum union all 
select "2020-02-25T12:57:31" as start,  "2020-02-25T13:16:27" as end, 90 as rownum union all 
select "2020-02-25T13:29:18" as start,  "2020-02-25T13:29:19" as end, 0 as rownum union all 
select "2020-02-25T13:36:52" as start,  "2020-02-25T14:01:18" as end, 92 as rownum union all 
select "2020-02-25T14:06:05" as start,  "2020-02-25T14:06:05" as end, 0 as rownum union all 
select "2020-02-25T14:14:32" as start,  "2020-02-25T14:14:33" as end, 0 as rownum union all 
select "2020-02-25T14:19:41" as start,  "2020-02-25T14:19:42" as end, 0 as rownum union all 
select "2020-02-25T14:28:45" as start,  "2020-02-25T14:35:12" as end, 96 as rownum union all 
select "2020-02-25T14:48:57" as start,  "2020-02-25T15:06:06" as end, 97 as rownum union all 
select "2020-02-25T15:07:31" as start,  "2020-02-25T15:16:12" as end, 98 as rownum union all 
select "2020-02-25T15:17:31" as start,  "2020-02-25T15:21:52" as end, 99 as rownum union all 
select "2020-02-25T15:49:33" as start,  "2020-02-25T15:56:00" as end, 100 as rownum union all 
select "2020-02-25T15:57:31" as start,  "2020-02-25T15:58:37" as end, 0 as rownum union all 
select "2020-02-25T16:18:15" as start,  "2020-02-25T16:18:16" as end, 0 as rownum union all 
select "2020-02-25T16:41:13" as start,  "2020-02-25T16:45:34" as end, 103 as rownum union all 
select "2020-02-25T16:47:31" as start,  "2020-02-25T16:56:03" as end, 104 as rownum union all 
select "2020-02-25T16:57:31" as start,  "2020-02-25T16:58:37" as end, 0 as rownum union all 
select "2020-02-25T17:04:17" as start,  "2020-02-25T17:06:23" as end, 106 as rownum union all 
select "2020-02-25T17:07:31" as start,  "2020-02-25T17:16:00" as end, 107 as rownum union all 
select "2020-02-25T17:17:31" as start,  "2020-02-25T17:26:08" as end, 108 as rownum union all 
select "2020-02-25T17:27:31" as start,  "2020-02-25T17:36:12" as end, 109 as rownum union all 
select "2020-02-25T17:37:31" as start,  "2020-02-25T17:45:59" as end, 110 as rownum union all 
select "2020-02-25T17:47:31" as start,  "2020-02-25T17:56:07" as end, 111 as rownum union all 
select "2020-02-25T17:57:31" as start,  "2020-02-25T18:06:07" as end, 112 as rownum union all 
select "2020-02-25T18:07:31" as start,  "2020-02-25T18:12:56" as end, 113 as rownum union all 
select "2020-02-25T18:14:24" as start,  "2020-02-25T18:15:29" as end, 0 as rownum union all 
select "2020-02-25T18:17:31" as start,  "2020-02-25T18:26:07" as end, 115 as rownum union all 
select "2020-02-25T18:27:31" as start,  "2020-02-25T18:36:11" as end, 116 as rownum union all 
select "2020-02-25T18:37:31" as start,  "2020-02-25T18:46:07" as end, 117 as rownum union all 
select "2020-02-25T18:47:31" as start,  "2020-02-25T18:55:06" as end, 118 as rownum union all 
select "2020-02-25T18:56:12" as start,  "2020-02-25T18:56:12" as end, 0 as rownum union all 
select "2020-02-25T18:57:32" as start,  "2020-02-25T18:57:32" as end, 0 as rownum union all 
select "2020-02-25T18:58:42" as start,  "2020-02-25T18:58:42" as end, 0 as rownum union all 
select "2020-02-25T18:59:48" as start,  "2020-02-25T19:06:14" as end, 122 as rownum union all 
select "2020-02-25T19:07:33" as start,  "2020-02-25T19:09:39" as end, 123 as rownum union all 
select "2020-02-25T19:10:45" as start,  "2020-02-25T19:15:05" as end, 124 as rownum union all 
select "2020-02-25T19:18:50" as start,  "2020-02-25T19:18:50" as end, 0 as rownum union all 
select "2020-02-25T19:20:23" as start,  "2020-02-25T19:20:23" as end, 0 as rownum union all 
select "2020-02-25T19:25:54" as start,  "2020-02-25T19:25:54" as end, 0 as rownum union all 
select "2020-02-25T19:28:16" as start,  "2020-02-25T19:28:17" as end, 0 as rownum union all 
select "2020-02-25T19:30:00" as start,  "2020-02-25T19:33:15" as end, 129 as rownum union all 
select "2020-02-25T19:35:04" as start,  "2020-02-25T19:35:04" as end, 0 as rownum union all 
select "2020-02-25T19:37:33" as start,  "2020-02-25T19:46:13" as end, 131 as rownum union all 
select "2020-02-25T19:47:32" as start,  "2020-02-25T19:56:13" as end, 132 as rownum union all 
select "2020-02-25T19:57:32" as start,  "2020-02-25T20:06:13" as end, 133 as rownum union all 
select "2020-02-25T20:07:32" as start,  "2020-02-25T20:16:13" as end, 134 as rownum union all 
select "2020-02-25T20:17:32" as start,  "2020-02-25T20:26:13" as end, 135 as rownum union all 
select "2020-02-25T20:27:32" as start,  "2020-02-25T20:36:13" as end, 136 as rownum union all 
select "2020-02-25T20:37:32" as start,  "2020-02-25T20:46:13" as end, 137 as rownum union all 
select "2020-02-25T20:47:32" as start,  "2020-02-25T20:56:14" as end, 138 as rownum union all 
select "2020-02-25T20:57:33" as start,  "2020-02-25T21:06:13" as end, 139 as rownum union all 
select "2020-02-25T21:07:32" as start,  "2020-02-25T21:16:13" as end, 140 as rownum union all 
select "2020-02-25T21:17:32" as start,  "2020-02-25T21:26:13" as end, 141 as rownum union all 
select "2020-02-25T21:27:32" as start,  "2020-02-25T21:36:13" as end, 142 as rownum union all 
select "2020-02-25T21:37:32" as start,  "2020-02-25T21:46:13" as end, 143 as rownum union all 
select "2020-02-25T21:47:32" as start,  "2020-02-25T21:56:13" as end, 144 as rownum union all 
select "2020-02-25T21:57:32" as start,  "2020-02-25T22:06:13" as end, 145 as rownum union all 
select "2020-02-25T22:07:32" as start,  "2020-02-25T22:16:13" as end, 146 as rownum union all 
select "2020-02-25T22:17:32" as start,  "2020-02-25T22:26:13" as end, 147 as rownum union all 
select "2020-02-25T22:27:32" as start,  "2020-02-25T22:36:13" as end, 148 as rownum union all 
select "2020-02-25T22:37:32" as start,  "2020-02-25T22:46:13" as end, 149 as rownum union all 
select "2020-02-25T22:47:32" as start,  "2020-02-25T22:56:13" as end, 150 as rownum union all 
select "2020-02-25T22:57:32" as start,  "2020-02-25T23:06:13" as end, 151 as rownum union all 
select "2020-02-25T23:07:32" as start,  "2020-02-25T23:16:13" as end, 152 as rownum union all 
select "2020-02-25T23:17:32" as start,  "2020-02-25T23:26:13" as end, 153 as rownum union all 
select "2020-02-25T23:27:33" as start,  "2020-02-25T23:36:13" as end, 154 as rownum union all 
select "2020-02-25T23:37:32" as start,  "2020-02-25T23:46:13" as end, 155 as rownum union all 
select "2020-02-25T23:47:32" as start,  "2020-02-25T23:56:13" as end, 156 as rownum;

1 Ответ

2 голосов
/ 29 февраля 2020

Ваш row_num увеличивается, поэтому вы можете использовать кумулятивный максимум:

select s.*,
       max(row_num) over (order by start) as new_row_num
from s;

В обновлении это будет выглядеть так:

update sample s
    set row_num = ss.new_row_num
    from (select s.*,
                 max(row_num) over (order by start) as new_row_num
          from s
         ) ss
     where s.row_num = 0 and ss.start = s.start;

То есть Я, вероятно, просто воссоздать таблицу с правильными номерами строк. Это проще и, вероятно, быстрее, чем обновление таблицы.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...