作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
数据必须按 id 和 pageview_date 进行分区。因此,对于每个相应的 id - 代码应该在 edited_date
列中查找不晚于 pageview_date
字段本身的最新日期。但它必须查找在 pageview_date
之前可用的所有值,而不仅仅是每个给定日期的记录。这是数据和代码:
with sample as (
select 'a' as id, DATE('2022-02-27') as pageview_date, DATE('2022-01-28') as edited_date
UNION ALL
select 'a' as id, DATE('2022-02-27') as pageview_date, DATE('2022-03-01') as edited_date
UNION ALL
select 'a' as id, DATE('2022-03-01') as pageview_date, DATE('2022-03-28') as edited_date
UNION ALL
select 'a' as id, DATE('2022-03-01') as pageview_date, DATE('2022-01-28') as edited_date
UNION ALL
select 'a' as id, DATE('2022-03-05') as pageview_date, DATE('2017-02-28') as edited_date
)
SELECT
id,
pageview_date,
MAX(IF(edited_date <= pageview_date, edited_date, null)) OVER (PARTITION BY pageview_date, id) as new_edited_date
FROM sample
期望的输出是:
id pageview_date new_edited_date
a 2022-02-27 2022-01-28
a 2022-02-27 2022-01-28
a 2022-03-01 2022-03-01
a 2022-03-01 2022-03-01
a 2022-03-05 2022-03-01
最佳答案
table_1 AS (
SELECT
id,
pageview_date,
ARRAY_AGG(edited_date) OVER (PARTITION BY id) AS edited_dates
FROM sample
)
SELECT
id,
pageview_date,
(SELECT MAX(d) FROM UNNEST(edited_dates) AS d WHERE d <= pageview_date) AS new_edited_date
FROM table_1
ORDER BY pageview_date
输出:
关于sql - 按日期分区一段时间 SQL BigQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72561964/
我是一名优秀的程序员,十分优秀!