创建具有相对于 SQL 中其他列的时间范围的列

Create column with timeframe relative to other column in SQL

假设我有以下 table t_1 其中每一行代表一天:

+------+------------+-------+
| week |    date    | val   |
+------+------------+-------+
|    1 | 2022-02-07 |     1 | <- Monday
|    1 | 2022-02-08 |     2 |
|    1 | 2022-02-09 |     3 |
|    1 | 2022-02-10 |     4 | <- Thursday
|    1 | 2022-02-11 |     5 |
|    1 | 2022-02-12 |     6 |
|    1 | 2022-02-13 |     7 |
|    2 | 2022-02-14 |     8 | <- Monday
|    2 | 2022-02-15 |     9 |
|    2 | 2022-02-16 |    10 |
|    2 | 2022-02-17 |    11 | <- Thursday
|    2 | 2022-02-18 |    12 | 
|    2 | 2022-02-19 |    13 |
|    2 | 2022-02-20 |    14 |
+------+------------+-------+

如何从 t1 创建以下 table t2

+------------+------------+-----------+------------+
| date_start |  date_end  | val_cur.  | val_prev   |
+------------+------------+-----------+------------+
| 2022-01-14 | 2022-01-17 |        38 |         10 |
+------------+------------+-----------+------------+

此处 val_cur 定义为当前时间范围内的值之和(即 date_start 和 date_end 之间的值之和),而 val_prev 定义为前一个时间范围值的总和(即当前时间范围减去一周)。

-- Bigquery Standard SQL
WITH t_1 AS
  (SELECT 1 AS week, '2022-02-07' AS date, 1 AS val UNION ALL
    SELECT 1, '2022-02-08', 2 UNION ALL
    SELECT 1, '2022-02-09', 3 UNION ALL
    SELECT 1, '2022-02-10', 4 UNION ALL
    SELECT 1, '2022-02-11', 5 UNION ALL
    SELECT 1, '2022-02-12', 6 UNION ALL
    SELECT 1, '2022-02-13', 7 UNION ALL
    SELECT 2, '2022-02-14', 8 UNION ALL
    SELECT 2, '2022-02-15', 9 UNION ALL
    SELECT 2, '2022-02-16', 10 UNION ALL
    SELECT 2, '2022-02-17', 11 UNION ALL
    SELECT 2, '2022-02-18', 12 UNION ALL
    SELECT 2, '2022-02-19', 13 UNION ALL
    SELECT 2, '2022-02-20', 14)
SELECT '2022-02-14' AS date_start, '2022-02-17' AS date_stop, sum(val) AS val_cur
FROM t_1
WHERE date >= '2022-02-14' AND date <= '2022-02-17'

输出:

+-----+------------+------------+---------+
| Row | date_start | date_stop  | val_cur |
+-----+------------+------------+---------+
|   1 | 2022-02-14 | 2022-02-17 |      38 |
+-----+------------+------------+---------+

但是如何获取最后一列?

考虑以下方法

with your_table as (
  select 1 as week, date '2022-02-07' as date, 1 as val union all
  select 1, '2022-02-08', 2 union all
  select 1, '2022-02-09', 3 union all
  select 1, '2022-02-10', 4 union all
  select 1, '2022-02-11', 5 union all
  select 1, '2022-02-12', 6 union all
  select 1, '2022-02-13', 7 union all
  select 2, '2022-02-14', 8 union all
  select 2, '2022-02-15', 9 union all
  select 2, '2022-02-16', 10 union all
  select 2, '2022-02-17', 11 union all
  select 2, '2022-02-18', 12 union all
  select 2, '2022-02-19', 13 union all
  select 2, '2022-02-20', 14
), timeframe as (
  select date '2022-02-14' as date_start, date '2022-02-17' as date_stop     
)
select date_start, date_stop, 
  sum(if(date between date_start and date_stop,val, 0)) as val_cur,
  sum(if(date between date_start - 7 and date_stop - 7,val, 0)) as val_prev
from your_table, timeframe
group by date_start, date_stop       

有输出